2024-01-02 02:22:35 +08:00
|
|
|
use crate::flog::log_extra_to_flog_file;
|
2023-10-09 05:22:27 +08:00
|
|
|
use crate::parser::Parser;
|
2023-03-28 23:59:51 +08:00
|
|
|
use crate::{
|
2023-04-22 06:49:16 +08:00
|
|
|
common::escape,
|
2024-01-02 02:22:35 +08:00
|
|
|
ffi::{wcharz_t, wcstring_list_ffi_t},
|
2023-03-28 23:59:51 +08:00
|
|
|
global_safety::RelaxedAtomicBool,
|
2023-08-09 06:16:04 +08:00
|
|
|
wchar::prelude::*,
|
2024-01-02 02:22:35 +08:00
|
|
|
wchar_ffi::WCharFromFFI,
|
2023-03-28 23:59:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cxx::bridge]
|
|
|
|
mod trace_ffi {
|
|
|
|
extern "C++" {
|
|
|
|
include!("wutil.h");
|
|
|
|
include!("parser.h");
|
2023-04-16 09:15:37 +08:00
|
|
|
type wcstring_list_ffi_t = super::wcstring_list_ffi_t;
|
2023-03-28 23:59:51 +08:00
|
|
|
type wcharz_t = super::wcharz_t;
|
2023-10-09 05:22:27 +08:00
|
|
|
type Parser = crate::parser::Parser;
|
2023-03-28 23:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "Rust" {
|
|
|
|
fn trace_set_enabled(do_enable: bool);
|
2023-10-09 05:10:05 +08:00
|
|
|
fn trace_enabled(parser: &Parser) -> bool;
|
2023-03-28 23:59:51 +08:00
|
|
|
#[cxx_name = "trace_argv"]
|
2023-10-09 05:10:05 +08:00
|
|
|
fn trace_argv_ffi(parser: &Parser, command: wcharz_t, args: &wcstring_list_ffi_t);
|
2023-03-28 23:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static DO_TRACE: RelaxedAtomicBool = RelaxedAtomicBool::new(false);
|
|
|
|
|
|
|
|
pub fn trace_set_enabled(do_enable: bool) {
|
|
|
|
DO_TRACE.store(do_enable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return whether tracing is enabled.
|
2023-10-09 05:10:05 +08:00
|
|
|
pub fn trace_enabled(parser: &Parser) -> bool {
|
2023-10-09 05:22:27 +08:00
|
|
|
let ld = &parser.libdata().pods;
|
2023-03-28 23:59:51 +08:00
|
|
|
if ld.suppress_fish_trace {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DO_TRACE.load()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trace an "argv": a list of arguments where the first is the command.
|
|
|
|
// Allow the `&Vec` parameter as this function only exists temporarily for the FFI
|
|
|
|
#[allow(clippy::ptr_arg)]
|
2023-10-09 05:10:05 +08:00
|
|
|
fn trace_argv_ffi(parser: &Parser, command: wcharz_t, args: &wcstring_list_ffi_t) {
|
2023-08-09 06:16:04 +08:00
|
|
|
let command: WString = command.into();
|
|
|
|
let args: Vec<WString> = args.from_ffi();
|
|
|
|
let args_ref: Vec<&wstr> = args.iter().map(WString::as_utfstr).collect();
|
2023-03-28 23:59:51 +08:00
|
|
|
trace_argv(parser, command.as_utfstr(), &args_ref);
|
|
|
|
}
|
|
|
|
|
2023-10-09 05:10:05 +08:00
|
|
|
pub fn trace_argv<S: AsRef<wstr>>(parser: &Parser, command: &wstr, args: &[S]) {
|
2023-03-28 23:59:51 +08:00
|
|
|
// Format into a string to prevent interleaving with flog in other threads.
|
|
|
|
// Add the + prefix.
|
|
|
|
let mut trace_text = L!("-").repeat(parser.blocks_size() - 1);
|
|
|
|
trace_text.push('>');
|
|
|
|
|
|
|
|
if !command.is_empty() {
|
|
|
|
trace_text.push(' ');
|
|
|
|
trace_text.push_utfstr(command);
|
|
|
|
}
|
|
|
|
for arg in args {
|
|
|
|
trace_text.push(' ');
|
2023-04-23 02:10:08 +08:00
|
|
|
trace_text.push_utfstr(&escape(arg.as_ref()));
|
2023-03-28 23:59:51 +08:00
|
|
|
}
|
|
|
|
trace_text.push('\n');
|
2024-01-02 02:22:35 +08:00
|
|
|
log_extra_to_flog_file(&trace_text);
|
2023-03-28 23:59:51 +08:00
|
|
|
}
|
|
|
|
|
2023-10-09 05:22:27 +08:00
|
|
|
pub fn trace_if_enabled_ffi<S: AsRef<wstr>>(parser: &Parser, command: &wstr, args: &[S]) {
|
|
|
|
if trace_enabled(parser) {
|
|
|
|
trace_argv(parser, command, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience helper to trace a single command if tracing is enabled.
|
|
|
|
pub fn trace_if_enabled(parser: &Parser, command: &wstr) {
|
|
|
|
if trace_enabled(parser) {
|
|
|
|
let argv: &[&'static wstr] = &[];
|
|
|
|
trace_argv(parser, command, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Convenience helper to trace a single command and arguments if tracing is enabled.
|
|
|
|
pub fn trace_if_enabled_with_args<S: AsRef<wstr>>(parser: &Parser, command: &wstr, args: &[S]) {
|
2023-03-28 23:59:51 +08:00
|
|
|
if trace_enabled(parser) {
|
|
|
|
trace_argv(parser, command, args);
|
|
|
|
}
|
|
|
|
}
|