use std::path::PathBuf;
use tracing_subscriber::{
fmt::{self, format::FmtSpan},
layer::SubscriberExt,
util::SubscriberInitExt,
EnvFilter,
};
use super::paths;
pub fn init_cli() {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
EnvFilter::new("debugger=info,warn")
});
tracing_subscriber::registry()
.with(filter)
.with(
fmt::layer()
.with_target(true)
.with_thread_ids(false)
.with_file(false)
.with_line_number(false)
.compact(),
)
.init();
}
pub fn init_daemon() -> Option<PathBuf> {
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
EnvFilter::new("debugger=trace,info")
});
let log_path = if let Some(log_dir) = paths::log_dir() {
if std::fs::create_dir_all(&log_dir).is_ok() {
let log_file = log_dir.join("daemon.log");
match std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&log_file)
{
Ok(file) => {
let file_layer = fmt::layer()
.with_writer(file)
.with_ansi(false)
.with_target(true)
.with_thread_ids(true)
.with_file(true)
.with_line_number(true)
.with_span_events(FmtSpan::ENTER | FmtSpan::EXIT);
let stderr_layer = fmt::layer()
.with_writer(std::io::stderr)
.with_target(true)
.with_thread_ids(false)
.with_file(false)
.compact();
tracing_subscriber::registry()
.with(filter)
.with(file_layer)
.with(stderr_layer)
.init();
return Some(log_file);
}
Err(e) => {
eprintln!("Warning: Could not open log file: {}", e);
}
}
}
None
} else {
None
};
tracing_subscriber::registry()
.with(filter)
.with(
fmt::layer()
.with_writer(std::io::stderr)
.with_target(true)
.with_thread_ids(true)
.with_file(true)
.with_line_number(true),
)
.init();
log_path
}
pub fn daemon_log_path() -> Option<PathBuf> {
paths::log_dir().map(|d| d.join("daemon.log"))
}
pub fn truncate_daemon_log() -> std::io::Result<()> {
if let Some(path) = daemon_log_path() {
if path.exists() {
std::fs::write(&path, "")?;
}
}
Ok(())
}