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 context: $crate::current_context(),
22 };
23
24 logger.log(&record);
25 }
26 }};
27}
28
29/// Logs a message at the TRACE level.
30///
31/// # Examples
32///
33/// ```
34/// use traccia::{init_default, trace};
35///
36/// init_default();
37/// let item_id = 42;
38/// trace!("Processing item: {}", item_id);
39/// ```
40#[macro_export]
41macro_rules! trace {
42 ($($arg:tt)*) => {
43 $crate::log!($crate::LogLevel::Trace, $($arg)*)
44 };
45}
46
47/// Logs a message at the DEBUG level.
48///
49/// # Examples
50///
51/// ```
52/// use traccia::{init_default, debug};
53///
54/// init_default();
55/// let conn_id = 123;
56/// debug!("Connection established: {}", conn_id);
57/// ```
58#[macro_export]
59macro_rules! debug {
60 ($($arg:tt)*) => {
61 $crate::log!($crate::LogLevel::Debug, $($arg)*)
62 };
63}
64
65/// Logs a message at the INFO level.
66///
67/// # Examples
68///
69/// ```
70/// use traccia::{init_default, info};
71///
72/// init_default();
73/// info!("Application started");
74/// ```
75#[macro_export]
76macro_rules! info {
77 ($($arg:tt)*) => {
78 $crate::log!($crate::LogLevel::Info, $($arg)*)
79 };
80}
81
82/// Logs a message at the WARN level.
83///
84/// # Examples
85///
86/// ```
87/// use traccia::{init_default, warn};
88///
89/// init_default();
90/// let usage = 85;
91/// warn!("Resource usage high: {}%", usage);
92/// ```
93#[macro_export]
94macro_rules! warn {
95 ($($arg:tt)*) => {
96 $crate::log!($crate::LogLevel::Warn, $($arg)*)
97 };
98}
99
100/// Logs a message at the ERROR level.
101///
102/// # Examples
103///
104/// ```
105/// use traccia::{init_default, error};
106///
107/// init_default();
108/// let err = "timeout";
109/// error!("Failed to connect: {}", err);
110/// ```
111#[macro_export]
112macro_rules! error {
113 ($($arg:tt)*) => {
114 $crate::log!($crate::LogLevel::Error, $($arg)*)
115 };
116}
117
118/// Logs a message at the FATAL level.
119///
120/// # Examples
121///
122/// ```
123/// use traccia::{init_default, fatal};
124///
125/// init_default();
126/// let err = "configuration error";
127/// fatal!("Failed to start application: {}", err);
128/// ```
129#[macro_export]
130macro_rules! fatal {
131 ($($arg:tt)*) => {
132 $crate::log!($crate::LogLevel::Fatal, $($arg)*)
133 };
134}
135
136/// Creates a new span with the given name and fields.
137///
138/// The span is active until the returned guard is dropped. All log messages
139/// within the span will include the span's context information.
140///
141/// # Syntax
142///
143/// ```ignore
144/// span!(name)
145/// span!(name, key1 => value1)
146/// span!(name, key1 => value1, key2 => value2, ...)
147/// ```
148///
149/// # Examples
150///
151/// ```
152/// use traccia::{span, info, init_default};
153///
154/// init_default();
155///
156/// fn process_request(user_id: u64) {
157/// let _span = span!("request", "user_id" => user_id);
158/// info!("Processing request");
159/// // Logs: [INFO] Processing request [request: user_id=42]
160/// }
161/// ```
162///
163/// ```
164/// use traccia::{span, info, init_default};
165///
166/// init_default();
167///
168/// fn handle_connection(conn_id: u32, ip: &str) {
169/// let _span = span!("connection", "id" => conn_id, "ip" => ip);
170/// info!("Connection established");
171/// // Logs: [INFO] Connection established [connection: id=123, ip=127.0.0.1]
172/// }
173/// ```
174#[macro_export]
175macro_rules! span {
176 ($name:expr) => {
177 $crate::enter($name, vec![])
178 };
179 ($name:expr, $($key:expr => $value:expr),+ $(,)?) => {
180 $crate::enter($name, vec![$(
181 ($key.to_string(), $value.to_string())
182 ),+])
183 };
184}