Files
javis_bot/app/src/log.rs
javis-bot 390f6f0b86 feat(app): M5 step 1, the admin backend under the management app
- datadir: the data folder layout, the location file (JARVIS_DATA_DIR wins),
  free space for the wizard.
- settings_edit: the fields the five settings screens edit; apply writes only
  non-defaults, keeps unknown keys and writes nothing unless the app's own
  check passes. A memory section reaches the worker's hello.
- secrets: config/secrets.dat, DPAPI for the Windows user; the services take
  the bot token from it before JARVIS_DISCORD_TOKEN.
- logs: the log hub behind the log screen (tabs, daily files kept 14 days,
  the recent error report, a zip under 10 MB, secrets and token-shaped text
  masked); the log macros and the worker's output feed it.

Spec: app/src/admin.spec.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 13:36:02 +09:00

73 lines
2.4 KiB
Rust

//! 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<bool> = 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");
}
}