Skip to main content

serialplugin_core/
logger.rs

1//! Centralized logging module for the serial plugin
2//!
3//! This module provides a unified logging interface with configurable log levels.
4//! All logging in the plugin should use these macros to respect the global log level setting.
5//!
6//! On Android, messages go to logcat under tag `SerialPlugin` (see `android_log`).
7//! Elsewhere they use stdout/stderr.
8
9/// Logs an error message if the current log level permits
10#[macro_export]
11macro_rules! log_error {
12    ($($arg:tt)*) => {
13        if $crate::state::get_log_level().should_log_error() {
14            #[cfg(target_os = "android")]
15            {
16                ::log::error!($($arg)*);
17            }
18            #[cfg(not(target_os = "android"))]
19            {
20                eprintln!($($arg)*);
21            }
22        }
23    };
24}
25
26/// Logs a warning message if the current log level permits
27#[macro_export]
28macro_rules! log_warn {
29    ($($arg:tt)*) => {
30        if $crate::state::get_log_level().should_log_warn() {
31            #[cfg(target_os = "android")]
32            {
33                ::log::warn!($($arg)*);
34            }
35            #[cfg(not(target_os = "android"))]
36            {
37                println!($($arg)*);
38            }
39        }
40    };
41}
42
43/// Logs an info message if the current log level permits
44#[macro_export]
45macro_rules! log_info {
46    ($($arg:tt)*) => {
47        if $crate::state::get_log_level().should_log_info() {
48            #[cfg(target_os = "android")]
49            {
50                ::log::info!($($arg)*);
51            }
52            #[cfg(not(target_os = "android"))]
53            {
54                println!($($arg)*);
55            }
56        }
57    };
58}
59
60/// Logs a debug message if the current log level permits
61#[macro_export]
62macro_rules! log_debug {
63    ($($arg:tt)*) => {
64        if $crate::state::get_log_level().should_log_debug() {
65            #[cfg(target_os = "android")]
66            {
67                ::log::debug!($($arg)*);
68            }
69            #[cfg(not(target_os = "android"))]
70            {
71                println!($($arg)*);
72            }
73        }
74    };
75}
76
77#[cfg(test)]
78mod tests {
79    use crate::state::{set_log_level, LogLevel};
80
81    #[test]
82    fn test_log_level_none() {
83        set_log_level(LogLevel::None);
84        // These should not panic, just not print anything
85        log_error!("This should not be printed");
86        log_warn!("This should not be printed");
87        log_info!("This should not be printed");
88        log_debug!("This should not be printed");
89    }
90
91    #[test]
92    fn test_log_level_error() {
93        set_log_level(LogLevel::Error);
94        log_error!("Error message");
95        // Warn, Info, Debug should not print
96    }
97
98    #[test]
99    fn test_log_level_debug() {
100        set_log_level(LogLevel::Debug);
101        log_error!("Error message");
102        log_warn!("Warning message");
103        log_info!("Info message");
104        log_debug!("Debug message");
105    }
106}