2026-11-23 01:15:26 +01:00
|
|
|
//! One tracing setup for every binary.
|
|
|
|
|
//!
|
|
|
|
|
//! Filter with `VICTORY_LOG` (falls back to `RUST_LOG`), e.g.
|
2026-11-23 23:10:01 +01:00
|
|
|
//! `VICTORY_LOG=victory_loader=trace,info`. Debug and trace output exist
|
|
|
|
|
//! only in builds with the `diagnostics` feature; a plain release build
|
|
|
|
|
//! logs info and above whatever the filter asks for.
|
2026-11-26 21:28:35 -08:00
|
|
|
//!
|
|
|
|
|
//! `VICTORY_LOG_FILE=<path>`: every line is added to that file too, without
|
|
|
|
|
//! colours (`victory play` names one for each start).
|
2026-11-23 01:15:26 +01:00
|
|
|
|
|
|
|
|
use tracing_subscriber::EnvFilter;
|
2026-11-23 23:10:01 +01:00
|
|
|
use tracing_subscriber::filter::LevelFilter;
|
|
|
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
|
|
|
use tracing_subscriber::util::SubscriberInitExt;
|
|
|
|
|
|
|
|
|
|
/// The most verbose level this build can log.
|
|
|
|
|
const CAP: LevelFilter = if cfg!(feature = "diagnostics") {
|
|
|
|
|
LevelFilter::TRACE
|
|
|
|
|
} else {
|
|
|
|
|
LevelFilter::INFO
|
|
|
|
|
};
|
2026-11-23 01:15:26 +01:00
|
|
|
|
|
|
|
|
pub fn init(default_level: &str) {
|
|
|
|
|
let filter = std::env::var("VICTORY_LOG")
|
|
|
|
|
.or_else(|_| std::env::var("RUST_LOG"))
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|v| EnvFilter::try_new(v).ok())
|
|
|
|
|
.unwrap_or_else(|| EnvFilter::new(default_level));
|
2026-11-26 21:28:35 -08:00
|
|
|
// (Opened again right after it was made: a virus scanner may hold a new
|
|
|
|
|
// file for a moment, so a refusal is tried again a few times.)
|
|
|
|
|
let open = |path: &std::ffi::OsString| {
|
|
|
|
|
let mut tries = 0;
|
|
|
|
|
loop {
|
|
|
|
|
match std::fs::OpenOptions::new().create(true).append(true).open(path) {
|
|
|
|
|
Ok(file) => return Some(file),
|
|
|
|
|
Err(_) if tries < 20 => {
|
|
|
|
|
tries += 1;
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(50));
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("VICTORY_LOG_FILE: no log file ({e}); this run logs to this window only");
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let file = std::env::var_os("VICTORY_LOG_FILE")
|
|
|
|
|
.filter(|path| !path.is_empty())
|
|
|
|
|
.and_then(|path| open(&path))
|
|
|
|
|
.map(|file| {
|
|
|
|
|
tracing_subscriber::fmt::layer()
|
|
|
|
|
.with_target(true)
|
|
|
|
|
.with_ansi(false)
|
|
|
|
|
.with_writer(std::sync::Mutex::new(file))
|
|
|
|
|
});
|
2026-11-23 23:10:01 +01:00
|
|
|
let _ = tracing_subscriber::registry()
|
|
|
|
|
.with(CAP)
|
|
|
|
|
.with(filter)
|
|
|
|
|
.with(
|
|
|
|
|
tracing_subscriber::fmt::layer()
|
|
|
|
|
.with_target(true)
|
|
|
|
|
.with_writer(std::io::stderr),
|
|
|
|
|
)
|
2026-11-26 21:28:35 -08:00
|
|
|
.with(file)
|
2026-11-23 01:15:26 +01:00
|
|
|
.try_init();
|
|
|
|
|
}
|