1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use anyhow::Result;
use std::path::PathBuf;
use std::sync::OnceLock;
use tracing_subscriber::filter::EnvFilter;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
const DEFAULT_LOG_FILE: &str = "ghostscope.log";
static INIT_GUARD: OnceLock<()> = OnceLock::new();
/// Initialize logging with enhanced configuration options
pub fn initialize_logging_with_config(
log_file_path: Option<&str>,
enable_logging: bool,
enable_console_logging: bool,
log_level: crate::config::LogLevel,
_tui_mode: bool,
) -> Result<()> {
if INIT_GUARD.set(()).is_err() {
// Already initialized elsewhere; do nothing and succeed
return Ok(());
}
// If logging is disabled, set up a minimal subscriber that discards everything
if !enable_logging {
let init_res = tracing_subscriber::registry()
.with(tracing_subscriber::filter::LevelFilter::OFF)
.try_init();
let _ = init_res;
return Ok(());
}
// Initialize log to tracing adapter to capture aya's log:: output
// Initialize LogTracer but ignore 'already set' errors to avoid noisy output
let _ = tracing_log::LogTracer::init();
// Build EnvFilter from RUST_LOG if present; otherwise fall back to configured log_level
// This enables module-level filtering like: RUST_LOG="info,ghostscope_loader=debug,ghostscope_protocol=debug"
let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(log_level.to_string()));
let event_format = tracing_subscriber::fmt::format()
.with_target(true)
.with_level(true)
.with_source_location(true)
.compact();
// Determine log file path: use provided path or default to current directory
let log_path = match log_file_path {
Some(path) => PathBuf::from(path),
None => std::env::current_dir()?.join(DEFAULT_LOG_FILE),
};
// Try to create log file, but continue if it fails
let maybe_log_file = std::fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&log_path);
match maybe_log_file {
Ok(log_file) => {
// Configure file output
let file_layer = tracing_subscriber::fmt::layer()
.event_format(event_format.clone())
.with_writer(log_file)
.with_ansi(false);
if enable_console_logging {
// Console logging enabled: dual output to file and stdout with level filter
let stdout_layer = tracing_subscriber::fmt::layer()
.event_format(event_format.clone())
.with_writer(std::io::stdout)
.with_ansi(true);
let init_res = tracing_subscriber::registry()
.with(env_filter)
.with(file_layer)
.with(stdout_layer)
.try_init();
let _ = init_res;
} else {
// Console logging disabled: only log to file
let init_res = tracing_subscriber::registry()
.with(env_filter)
.with(file_layer)
.try_init();
let _ = init_res; // ignore AlreadyInit errors silently
}
}
Err(_) => {
// Fallback to stdout only if file creation fails and console logging is enabled
if enable_console_logging {
let stdout_layer = tracing_subscriber::fmt::layer()
.event_format(event_format)
.with_writer(std::io::stdout);
let init_res = tracing_subscriber::registry()
.with(env_filter)
.with(stdout_layer)
.try_init();
let _ = init_res;
} else {
// No file and no console logging - set up minimal subscriber that discards everything
let init_res = tracing_subscriber::registry()
.with(tracing_subscriber::filter::LevelFilter::OFF)
.try_init();
let _ = init_res;
}
}
}
Ok(())
}