//! Debug logging, the Rust side of `src/jarvis/debug.py`. //! //! On only when `JARVIS_DEBUG=1`. Messages carry ids, sizes, counts and timings, never what anyone //! said or wrote (no transcripts, no chat text, no reply text). use std::sync::OnceLock; pub fn debug_enabled() -> bool { static ON: OnceLock = OnceLock::new(); *ON.get_or_init(|| std::env::var("JARVIS_DEBUG").as_deref() == Ok("1")) } /// `[ category ] message`, the category centred in 10 columns like the Python `debug_log`. pub fn line(category: &str, message: &str) -> String { format!("[{category:^10}] {message}") } /// Print a line and give it to the log hub when one is installed (`app/src/admin.spec.md`). pub fn emit(level: crate::logs::Level, category: &str, message: &str) { match level { crate::logs::Level::Debug => eprintln!("{}", line(category, message)), crate::logs::Level::Info => eprintln!("[{category}] {message}"), crate::logs::Level::Warn => eprintln!("[{category}] WARN {message}"), crate::logs::Level::Error => eprintln!("[{category}] ERROR {message}"), } if let Some(hub) = crate::logs::installed() { hub.push(level, category, message); } } /// Only when `JARVIS_DEBUG=1`. #[macro_export] macro_rules! debug_log { ($cat:expr, $($arg:tt)*) => { if $crate::log::debug_enabled() { $crate::log::emit($crate::logs::Level::Debug, $cat, &format!($($arg)*)); } }; } /// Always logged: events the log screen shows without debug logging (start, stop, joins). #[macro_export] macro_rules! info_log { ($cat:expr, $($arg:tt)*) => { $crate::log::emit($crate::logs::Level::Info, $cat, &format!($($arg)*)); }; } #[macro_export] macro_rules! warn_log { ($cat:expr, $($arg:tt)*) => { $crate::log::emit($crate::logs::Level::Warn, $cat, &format!($($arg)*)); }; } /// Errors are always printed; the same rule applies: no transcript or chat text in the message. #[macro_export] macro_rules! error_log { ($cat:expr, $($arg:tt)*) => { $crate::log::emit($crate::logs::Level::Error, $cat, &format!($($arg)*)); }; } #[cfg(test)] mod tests { #[test] fn category_is_centred_like_python() { // Python: f"[{'voice':^10}]" == "[ voice ]" assert_eq!(super::line("voice", "x"), "[ voice ] x"); assert_eq!(super::line("worker", "x"), "[ worker ] x"); } }