1#[macro_export]
17macro_rules! trace {
18 ($($arg:tt)*) => {
19 $crate::log(&$crate::Record::new(
20 $crate::LogLevel::Trace,
21 format!($($arg)*),
22 Some(module_path!().to_string()),
23 Some(file!().to_string()),
24 Some(line!()),
25 ))
26 };
27}
28
29#[macro_export]
40macro_rules! debug {
41 ($($arg:tt)*) => {
42 $crate::log(&$crate::Record::new(
43 $crate::LogLevel::Debug,
44 format!($($arg)*),
45 Some(module_path!().to_string()),
46 Some(file!().to_string()),
47 Some(line!()),
48 ))
49 };
50}
51
52#[macro_export]
63macro_rules! info {
64 ($($arg:tt)*) => {
65 $crate::log(&$crate::Record::new(
66 $crate::LogLevel::Info,
67 format!($($arg)*),
68 Some(module_path!().to_string()),
69 Some(file!().to_string()),
70 Some(line!()),
71 ))
72 };
73}
74
75#[macro_export]
86macro_rules! success {
87 ($($arg:tt)*) => {
88 $crate::log(&$crate::Record::new(
89 $crate::LogLevel::Success,
90 format!($($arg)*),
91 Some(module_path!().to_string()),
92 Some(file!().to_string()),
93 Some(line!()),
94 ))
95 };
96}
97
98#[macro_export]
109macro_rules! warn {
110 ($($arg:tt)*) => {
111 $crate::log(&$crate::Record::new(
112 $crate::LogLevel::Warning,
113 format!($($arg)*),
114 Some(module_path!().to_string()),
115 Some(file!().to_string()),
116 Some(line!()),
117 ))
118 };
119}
120
121#[macro_export]
132macro_rules! error {
133 ($($arg:tt)*) => {
134 $crate::log(&$crate::Record::new(
135 $crate::LogLevel::Error,
136 format!($($arg)*),
137 Some(module_path!().to_string()),
138 Some(file!().to_string()),
139 Some(line!()),
140 ))
141 };
142}
143
144#[macro_export]
155macro_rules! critical {
156 ($($arg:tt)*) => {
157 $crate::log(&$crate::Record::new(
158 $crate::LogLevel::Critical,
159 format!($($arg)*),
160 Some(module_path!().to_string()),
161 Some(file!().to_string()),
162 Some(line!()),
163 ))
164 };
165}
166
167#[macro_export]
178macro_rules! log_with_metadata {
179 ($level:expr, $($key:expr => $value:expr),+; $($arg:tt)*) => {
180 {
181 let mut record = $crate::Record::new(
182 $level,
183 format!($($arg)*),
184 Some(module_path!().to_string()),
185 Some(file!().to_string()),
186 Some(line!()),
187 );
188 $(
189 record = record.with_metadata($key, $value);
190 )+
191 let result = $crate::log(&record);
192 println!("Log with metadata result: {}", result);
193 result
194 }
195 };
196}
197
198#[macro_export]
202macro_rules! push_context {
203 ( $( $key:expr => $val:expr ),* $(,)? ) => {
204 {
205 let mut ctx = ::std::collections::HashMap::new();
206 $( ctx.insert($key.to_string(), $crate::context::ContextValue::String($val.to_string())); )*
207 $crate::context::push_context(ctx);
208 }
209 };
210}
211
212#[macro_export]
214macro_rules! pop_context {
215 () => {
216 $crate::context::pop_context();
217 };
218}
219
220#[macro_export]
222macro_rules! set_context {
223 ($key:expr, $val:expr) => {
224 $crate::context::set_context_value(
225 $key,
226 $crate::context::ContextValue::String($val.to_string()),
227 );
228 };
229}
230
231#[macro_export]
233macro_rules! get_context {
234 ($key:expr) => {
235 $crate::context::get_context_value($key)
236 };
237}
238
239#[macro_export]
243macro_rules! scope {
244 ($name:expr) => {
245 $crate::scope::ScopeGuard::enter($name)
246 };
247}
248
249#[macro_export]
252macro_rules! scoped_info {
253 ($name:expr) => {{
254 $crate::info!("Entering scope: {}", $name);
255 let _guard = $crate::scope::ScopeGuard::enter($name);
256 struct ScopeLogger<'a> {
257 name: &'a str,
258 start: ::std::time::Instant,
259 }
260 impl<'a> ::std::ops::Drop for ScopeLogger<'a> {
261 fn drop(&mut self) {
262 let elapsed = self.start.elapsed();
263 $crate::info!("Exiting scope: {} (elapsed: {:?})", self.name, elapsed);
264 }
265 }
266 ScopeLogger {
267 name: $name,
268 start: ::std::time::Instant::now(),
269 }
270 }};
271}
272
273#[macro_export]
276macro_rules! log_error {
277 ($err:expr) => {
278 $crate::error!("{}", $err);
279 };
280 ($err:expr, $msg:expr) => {
281 $crate::error!("{}: {}", $msg, $err);
282 };
283}
284
285#[macro_export]
287macro_rules! log_error_with_context {
288 ($err:expr, $ctx:expr) => {
289 $crate::error!("{} (context: {:?})", $err, $ctx);
290 };
291}
292
293#[macro_export]
295macro_rules! try_log {
296 ($expr:expr, $msg:expr) => {{
297 match $expr {
298 Ok(val) => Ok(val),
299 Err(e) => {
300 $crate::error!("{}: {}", $msg, e);
301 Err(e)
302 }
303 }
304 }};
305 (option $expr:expr, $msg:expr) => {{
306 match $expr {
307 Some(val) => Some(val),
308 None => {
309 $crate::error!("{}: None value", $msg);
310 None
311 }
312 }
313 }};
314}
315
316#[macro_export]
320macro_rules! log_if_enabled {
321 ($level:expr, $($arg:tt)*) => {
322 if $level >= $crate::STATIC_LEVEL {
323 $crate::log(&$crate::Record::new(
324 $level,
325 format!($($arg)*),
326 Some(module_path!().to_string()),
327 Some(file!().to_string()),
328 Some(line!()),
329 ))
330 } else {
331 false
332 }
333 };
334}
335
336#[macro_export]
339macro_rules! info_kv {
340 ($msg:expr; $( $key:expr => $val:expr ),+ ) => {{
341 let mut record = $crate::Record::new(
342 $crate::LogLevel::Info,
343 $msg.to_string(),
344 Some(module_path!().to_string()),
345 Some(file!().to_string()),
346 Some(line!()),
347 );
348 $( record = record.with_metadata($key, $val); )+
349 $crate::log(&record)
350 }};
351}