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, and with the `log` feature anywhere, messages go through the `log`
7//! crate (on Android to logcat under tag `SerialPlugin`, see `android_log`).
8//! Otherwise they use stdout/stderr (the default).
9
10/// Logs an error message if the current log level permits
11#[cfg(any(feature = "log", target_os = "android"))]
12#[macro_export]
13macro_rules! log_error {
14    ($($arg:tt)*) => {
15        if $crate::state::get_log_level().should_log_error() {
16            $crate::__log::error!($($arg)*);
17        }
18    };
19}
20
21/// Logs an error message if the current log level permits
22#[cfg(not(any(feature = "log", target_os = "android")))]
23#[macro_export]
24macro_rules! log_error {
25    ($($arg:tt)*) => {
26        if $crate::state::get_log_level().should_log_error() {
27            eprintln!($($arg)*);
28        }
29    };
30}
31
32/// Logs a warning message if the current log level permits
33#[cfg(any(feature = "log", target_os = "android"))]
34#[macro_export]
35macro_rules! log_warn {
36    ($($arg:tt)*) => {
37        if $crate::state::get_log_level().should_log_warn() {
38            $crate::__log::warn!($($arg)*);
39        }
40    };
41}
42
43/// Logs a warning message if the current log level permits
44#[cfg(not(any(feature = "log", target_os = "android")))]
45#[macro_export]
46macro_rules! log_warn {
47    ($($arg:tt)*) => {
48        if $crate::state::get_log_level().should_log_warn() {
49            println!($($arg)*);
50        }
51    };
52}
53
54/// Logs an info message if the current log level permits
55#[cfg(any(feature = "log", target_os = "android"))]
56#[macro_export]
57macro_rules! log_info {
58    ($($arg:tt)*) => {
59        if $crate::state::get_log_level().should_log_info() {
60            $crate::__log::info!($($arg)*);
61        }
62    };
63}
64
65/// Logs an info message if the current log level permits
66#[cfg(not(any(feature = "log", target_os = "android")))]
67#[macro_export]
68macro_rules! log_info {
69    ($($arg:tt)*) => {
70        if $crate::state::get_log_level().should_log_info() {
71            println!($($arg)*);
72        }
73    };
74}
75
76/// Logs a debug message if the current log level permits
77#[cfg(any(feature = "log", target_os = "android"))]
78#[macro_export]
79macro_rules! log_debug {
80    ($($arg:tt)*) => {
81        if $crate::state::get_log_level().should_log_debug() {
82            $crate::__log::debug!($($arg)*);
83        }
84    };
85}
86
87/// Logs a debug message if the current log level permits
88#[cfg(not(any(feature = "log", target_os = "android")))]
89#[macro_export]
90macro_rules! log_debug {
91    ($($arg:tt)*) => {
92        if $crate::state::get_log_level().should_log_debug() {
93            println!($($arg)*);
94        }
95    };
96}
97
98#[cfg(test)]
99mod tests {
100    use crate::state::{set_log_level, LogLevel};
101
102    #[test]
103    fn test_log_level_none() {
104        set_log_level(LogLevel::None);
105        // These should not panic, just not print anything
106        log_error!("This should not be printed");
107        log_warn!("This should not be printed");
108        log_info!("This should not be printed");
109        log_debug!("This should not be printed");
110    }
111
112    #[test]
113    fn test_log_level_error() {
114        set_log_level(LogLevel::Error);
115        log_error!("Error message");
116        // Warn, Info, Debug should not print
117    }
118
119    #[test]
120    fn test_log_level_debug() {
121        set_log_level(LogLevel::Debug);
122        log_error!("Error message");
123        log_warn!("Warning message");
124        log_info!("Info message");
125        log_debug!("Debug message");
126    }
127}