traccia/
macros.rs

1/// Macro for logging messages at a specific level.
2///
3/// This is the core logging macro that other macros (`debug!`, `info!`, etc.) build upon.
4///
5/// # Arguments
6///
7/// * `$level` - The log level to use
8/// * `$arg` - Format string and arguments, similar to `format!` or `println!`
9#[macro_export]
10macro_rules! log {
11   ($level:expr, $($arg:tt)*) => {{
12        if let Ok(logger) = $crate::logger() {
13            let record = $crate::Record {
14                level: $level,
15                thread_id: std::thread::current().id(),
16                target: module_path!().to_string(),
17                message: format!($($arg)*),
18                module_path: Some(module_path!()),
19                file: Some(file!()),
20                line: Some(line!()),
21            };
22
23            use $crate::Logger;
24            logger.log(&record);
25        }
26    }};
27}
28
29/// Logs a message at the TRACE level.
30///
31/// # Examples
32///
33/// ```
34/// trace!("Processing item: {}", item_id);
35/// ```
36#[macro_export]
37macro_rules! trace {
38    ($($arg:tt)*) => {
39        $crate::log!($crate::LogLevel::Trace, $($arg)*)
40    };
41}
42
43/// Logs a message at the DEBUG level.
44///
45/// # Examples
46///
47/// ```
48/// debug!("Connection established: {}", conn_id);
49/// ```
50#[macro_export]
51macro_rules! debug {
52    ($($arg:tt)*) => {
53        $crate::log!($crate::LogLevel::Debug, $($arg)*)
54    };
55}
56
57/// Logs a message at the INFO level.
58///
59/// # Examples
60///
61/// ```
62/// info!("Application started");
63/// ```
64#[macro_export]
65macro_rules! info {
66    ($($arg:tt)*) => {
67        $crate::log!($crate::LogLevel::Info, $($arg)*)
68    };
69}
70
71/// Logs a message at the WARN level.
72///
73/// # Examples
74///
75/// ```
76/// warn!("Resource usage high: {}%", usage);
77/// ```
78#[macro_export]
79macro_rules! warn {
80    ($($arg:tt)*) => {
81        $crate::log!($crate::LogLevel::Warn, $($arg)*)
82    };
83}
84
85/// Logs a message at the ERROR level.
86///
87/// # Examples
88///
89/// ```
90/// error!("Failed to connect: {}", err);
91/// ```
92#[macro_export]
93macro_rules! error {
94    ($($arg:tt)*) => {
95        $crate::log!($crate::LogLevel::Error, $($arg)*)
96    };
97}
98
99/// Logs a message at the FATAL level.
100///
101/// # Examples
102///
103/// ```
104/// fatal!("Failed to start application: {}", err);
105/// ```
106#[macro_export]
107macro_rules! fatal {
108    ($($arg:tt)*) => {
109        $crate::log!($crate::LogLevel::Fatal, $($arg)*)
110    };
111}