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::{get_log_level, set_log_level, LogLevel};
101
102    /// One test: the level is global, parallel tests would race on it.
103    #[test]
104    fn log_level_is_set_and_read_back() {
105        for level in [
106            LogLevel::None,
107            LogLevel::Error,
108            LogLevel::Warn,
109            LogLevel::Debug,
110            LogLevel::Info,
111        ] {
112            set_log_level(level);
113            assert_eq!(get_log_level(), level);
114            // Must not panic at any level.
115            log_error!("error at {:?}", level);
116            log_warn!("warn at {:?}", level);
117            log_info!("info at {:?}", level);
118            log_debug!("debug at {:?}", level);
119        }
120    }
121}