Skip to main content

libdonut_rs/utils/
log_wrapper.rs

1#[cfg(feature = "std")]
2use az_logger::{Color, LogFormatStyle, LogFormatStyles, Logger, LoggerOptions, Style};
3/// Logger handle re-export (when `logging` is enabled).
4#[cfg(feature = "logging")]
5pub use azathoth_logger::LOG;
6
7#[cfg(feature = "std")]
8/// Initialize logging (temporary API).
9///
10/// This configures `az_logger` with custom styles. Subject to removal in a future release.
11pub fn init_log(no_console: bool) -> crate::errors::DonutResult<()> {
12    let custom_log_styles = LogFormatStyles {
13        error: LogFormatStyle {
14            fg: Some(Color::BrightRed),
15            bg: None,
16            style: Style::default().bold(),
17        },
18        warn: LogFormatStyle {
19            fg: Some(Color::Yellow),
20            bg: None,
21            style: Style::default(),
22        },
23        info: LogFormatStyle {
24            fg: Some(Color::BrightCyan),
25            bg: None,
26            style: Style::default(),
27        },
28        debug: LogFormatStyle {
29            fg: Some(Color::Magenta),
30            bg: None,
31            style: Style::default().bold(),
32        },
33        success: LogFormatStyle {
34            fg: Some(Color::Green),
35            bg: None,
36            style: Style::default(),
37        },
38        critical: LogFormatStyle {
39            fg: Some(Color::Black),
40            bg: Some(Color::Cyan),
41            style: Style::default().bold().underline(),
42        },
43    };
44
45    let opts = LoggerOptions {
46        truncate_previous_logs: true,
47        custom_log_styles: Some(custom_log_styles),
48        log_dir: None,
49        no_console,
50        ..Default::default()
51    };
52
53    Logger::init(None::<String>, opts).map_err(|e| crate::errors::DonutError::Io(e.to_string()))?;
54    Ok(())
55}
56
57
58/// Debug logging helper.
59///
60/// On Windows uses `azathoth_logger` (when enabled). On Linux uses a direct `write` call.
61#[inline(always)]
62#[unsafe(link_section = ".text")]
63pub fn log(_msg: &str) {
64    #[cfg(target_os = "windows")]
65    {
66        #[cfg(feature = "logging")]
67        {
68            use azathoth_logger::Logger;
69            LOG.log(_msg);
70        }
71    }
72
73    #[cfg(not(target_os = "windows"))]
74    {
75        #[cfg(feature = "logging")]
76        {
77            crate::platform::linux::logging::write_fd(1, _msg.as_bytes());
78        }
79    }
80}
81
82/// Error logging helper.
83///
84/// On Linux writes to `STDERR` via `write`. On Windows delegates to [`log`].
85#[inline(always)]
86#[unsafe(link_section = ".text")]
87pub fn elog(_msg: &str) {
88    #[cfg(target_os = "windows")]
89    {
90        log(_msg);
91    }
92
93    #[cfg(not(target_os = "windows"))]
94    {
95        #[cfg(feature = "logging")]
96        {
97            crate::platform::linux::logging::write_fd(2, _msg.as_bytes());
98        }
99    }
100}
101
102/// Logging macro wrapper module to allow disabling logging at compile time.
103pub mod logwrapper {
104    /// Internal forwarding macro (do not use directly).
105    #[doc(hidden)]
106    #[macro_export]
107    macro_rules! __log_forward {
108        ($name:ident, $($arg:tt)*) => {{
109            #[cfg(feature = "logging")]
110            { ::azathoth_logger::$name!($($arg)*); }
111
112            #[cfg(not(feature = "logging"))]
113            { }
114        }};
115    }
116
117    /// Info-level log
118    #[macro_export]
119    macro_rules! info {
120        ($($arg:tt)*) => { $crate::__log_forward!(info, $($arg)*); };
121    }
122
123    /// Error-level log
124    #[macro_export]
125    macro_rules! error {
126        ($($arg:tt)*) => { $crate::__log_forward!(error, $($arg)*); };
127    }
128
129    /// Warning-level log
130    #[macro_export]
131    macro_rules! warn {
132        ($($arg:tt)*) => { $crate::__log_forward!(warn, $($arg)*); };
133    }
134
135    /// Success-level log
136    #[macro_export]
137    macro_rules! success {
138        ($($arg:tt)*) => { $crate::__log_forward!(success, $($arg)*); };
139    }
140
141    /// Critical-level log
142    #[macro_export]
143    macro_rules! critical {
144        ($($arg:tt)*) => { $crate::__log_forward!(critical, $($arg)*); };
145    }
146
147    /// Debug-level log (compiled out in release by default).
148    #[macro_export]
149    macro_rules! debug {
150        ($($arg:tt)*) => {{
151            #[cfg(all(feature = "logging", debug_assertions))]
152            { ::azathoth_logger::debug!($($arg)*); }
153
154            #[cfg(not(all(feature = "logging", debug_assertions)))]
155            { }
156        }};
157    }
158}