Skip to main content

osvm/utils/
debug_logger.rs

1//! Debug logging utilities with configurable verbosity levels
2//!
3//! This module provides standardized debug output across the application
4//! with fine-grained control over verbosity levels.
5
6use std::sync::atomic::{AtomicU8, Ordering};
7
8/// Global verbosity level
9static VERBOSITY_LEVEL: AtomicU8 = AtomicU8::new(0);
10
11/// Debug verbosity levels
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
13pub enum VerbosityLevel {
14    /// No debug output
15    Silent = 0,
16    /// Basic operations and errors
17    Basic = 1,
18    /// Detailed operations
19    Detailed = 2,
20    /// Everything including internal state
21    Verbose = 3,
22}
23
24impl From<u8> for VerbosityLevel {
25    fn from(level: u8) -> Self {
26        match level {
27            0 => VerbosityLevel::Silent,
28            1 => VerbosityLevel::Basic,
29            2 => VerbosityLevel::Detailed,
30            _ => VerbosityLevel::Verbose,
31        }
32    }
33}
34
35/// Set global verbosity level
36pub fn set_verbosity(level: VerbosityLevel) {
37    VERBOSITY_LEVEL.store(level as u8, Ordering::Relaxed);
38}
39
40/// Get current verbosity level
41pub fn get_verbosity() -> VerbosityLevel {
42    VerbosityLevel::from(VERBOSITY_LEVEL.load(Ordering::Relaxed))
43}
44
45/// Check if a verbosity level should be logged
46pub fn should_log(level: VerbosityLevel) -> bool {
47    level <= get_verbosity()
48}
49
50/// Debug print macro with verbosity control
51#[macro_export]
52macro_rules! debug_print {
53    ($level:expr, $($arg:tt)*) => {
54        if $crate::utils::debug_logger::should_log($level) {
55            match $level {
56                $crate::utils::debug_logger::VerbosityLevel::Basic => {
57                    println!("🔍 {}", format!($($arg)*));
58                }
59                $crate::utils::debug_logger::VerbosityLevel::Detailed => {
60                    println!("📋 {}", format!($($arg)*));
61                }
62                $crate::utils::debug_logger::VerbosityLevel::Verbose => {
63                    println!("🔬 {}", format!($($arg)*));
64                }
65                _ => {}
66            }
67        }
68    };
69}
70
71/// Warning print macro
72#[macro_export]
73macro_rules! debug_warn {
74    ($($arg:tt)*) => {
75        if $crate::utils::debug_logger::should_log($crate::utils::debug_logger::VerbosityLevel::Basic) {
76            println!("⚠️  {}", format!($($arg)*));
77        }
78    };
79}
80
81/// Success print macro
82#[macro_export]
83macro_rules! debug_success {
84    ($($arg:tt)*) => {
85        if $crate::utils::debug_logger::should_log($crate::utils::debug_logger::VerbosityLevel::Basic) {
86            println!("✅ {}", format!($($arg)*));
87        }
88    };
89}
90
91/// Error print macro
92#[macro_export]
93macro_rules! debug_error {
94    ($($arg:tt)*) => {
95        if $crate::utils::debug_logger::should_log($crate::utils::debug_logger::VerbosityLevel::Basic) {
96            eprintln!("❌ {}", format!($($arg)*));
97        }
98    };
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn test_verbosity_control() {
107        set_verbosity(VerbosityLevel::Detailed);
108        assert!(should_log(VerbosityLevel::Basic));
109        assert!(should_log(VerbosityLevel::Detailed));
110        assert!(!should_log(VerbosityLevel::Verbose));
111
112        set_verbosity(VerbosityLevel::Silent);
113        assert!(!should_log(VerbosityLevel::Basic));
114    }
115}