sandbox_runtime/utils/
debug.rs

1//! Debug logging utilities.
2
3use std::sync::atomic::{AtomicBool, Ordering};
4
5use tracing_subscriber::EnvFilter;
6
7/// Global debug flag.
8static DEBUG_ENABLED: AtomicBool = AtomicBool::new(false);
9
10/// Environment variable for debug mode.
11pub const SRT_DEBUG_ENV: &str = "SRT_DEBUG";
12
13/// Initialize debug logging based on the SRT_DEBUG environment variable or explicit flag.
14pub fn init_debug_logging(force_debug: bool) {
15    let debug_enabled = force_debug || std::env::var(SRT_DEBUG_ENV).is_ok();
16    DEBUG_ENABLED.store(debug_enabled, Ordering::SeqCst);
17
18    let filter = if debug_enabled {
19        EnvFilter::new("sandbox_runtime=debug,warn")
20    } else {
21        EnvFilter::new("sandbox_runtime=info,warn")
22    };
23
24    tracing_subscriber::fmt()
25        .with_env_filter(filter)
26        .with_target(false)
27        .with_level(debug_enabled)
28        .with_ansi(true)
29        .try_init()
30        .ok();
31}
32
33/// Check if debug mode is enabled.
34pub fn is_debug_enabled() -> bool {
35    DEBUG_ENABLED.load(Ordering::SeqCst)
36}
37
38/// Log a debug message (only if debug is enabled).
39#[macro_export]
40macro_rules! debug_log {
41    ($($arg:tt)*) => {
42        if $crate::utils::debug::is_debug_enabled() {
43            tracing::debug!($($arg)*);
44        }
45    };
46}
47
48/// Log a verbose debug message.
49#[macro_export]
50macro_rules! trace_log {
51    ($($arg:tt)*) => {
52        if $crate::utils::debug::is_debug_enabled() {
53            tracing::trace!($($arg)*);
54        }
55    };
56}