hotshot_utils/anytrace/
macros.rs

1#[macro_export]
2/// Print the file and line number of the location this macro is invoked
3///
4/// Note: temporarily prints only a null string to reduce verbosity of logging
5macro_rules! line_info {
6    () => {
7        format!("")
8    };
9}
10pub use line_info;
11
12#[macro_export]
13/// Create an error at the trace level.
14///
15/// The argument can be either:
16///   - an expression implementing `Display`
17///   - a string literal
18///   - a format string, similar to the `format!()` macro
19macro_rules! trace {
20  ($message:literal) => {
21      Error {
22        level: Level::Trace,
23        message: format!("{}: {}", line_info!(), format!($message))
24      }
25  };
26  ($error:expr) => {
27      Error {
28        level: Level::Trace,
29        message: format!("{}: {}", line_info!(), $error)
30      }
31  };
32  ($fmt:expr, $($arg:tt)*) => {
33      Error {
34        level: Level::Trace,
35        message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
36      }
37  };
38}
39pub use trace;
40
41#[macro_export]
42/// Create an error at the debug level.
43///
44/// The argument can be either:
45///   - an expression implementing `Display`
46///   - a string literal
47///   - a format string, similar to the `format!()` macro
48macro_rules! debug {
49  ($message:literal) => {
50      Error {
51        level: Level::Debug,
52        message: format!("{}: {}", line_info!(), format!($message))
53      }
54  };
55  ($error:expr) => {
56      Error {
57        level: Level::Debug,
58        message: format!("{}: {}", line_info!(), $error)
59      }
60  };
61  ($fmt:expr, $($arg:tt)*) => {
62      Error {
63        level: Level::Debug,
64        message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
65      }
66  };
67}
68pub use debug;
69
70#[macro_export]
71/// Create an error at the info level.
72///
73/// The argument can be either:
74///   - an expression implementing `Display`
75///   - a string literal
76///   - a format string, similar to the `format!()` macro
77macro_rules! info {
78  ($message:literal) => {
79      Error {
80        level: Level::Info,
81        message: format!("{}: {}", line_info!(), format!($message))
82      }
83  };
84  ($error:expr) => {
85      Error {
86        level: Level::Info,
87        message: format!("{}: {}", line_info!(), $error)
88      }
89  };
90  ($fmt:expr, $($arg:tt)*) => {
91      Error {
92        level: Level::Info,
93        message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
94      }
95  };
96}
97pub use info;
98
99#[macro_export]
100/// Create an error at the warn level.
101///
102/// The argument can be either:
103///   - an expression implementing `Display`
104///   - a string literal
105///   - a format string, similar to the `format!()` macro
106macro_rules! warn {
107  ($message:literal) => {
108      Error {
109        level: Level::Warn,
110        message: format!("{}: {}", line_info!(), format!($message))
111      }
112  };
113  ($error:expr) => {
114      Error {
115        level: Level::Warn,
116        message: format!("{}: {}", line_info!(), $error)
117      }
118  };
119  ($fmt:expr, $($arg:tt)*) => {
120      Error {
121        level: Level::Warn,
122        message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
123      }
124  };
125}
126pub use crate::warn;
127
128#[macro_export]
129/// Create an error at the error level.
130///
131/// The argument can be either:
132///   - an expression implementing `Display`
133///   - a string literal
134///   - a format string, similar to the `format!()` macro
135macro_rules! error {
136  ($message:literal) => {
137      Error {
138        level: Level::Error,
139        message: format!("{}: {}", line_info!(), format!($message))
140      }
141  };
142  ($error:expr) => {
143      Error {
144        level: Level::Error,
145        message: format!("{}: {}", line_info!(), $error)
146      }
147  };
148  ($fmt:expr, $($arg:tt)*) => {
149      Error {
150        level: Level::Error,
151        message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
152      }
153  };
154}
155pub use error;
156
157#[macro_export]
158/// Log a `anytrace::Error` at the corresponding level.
159macro_rules! log {
160    ($result:expr) => {
161        if let Err(ref error) = $result {
162            let mut error_level = error.level;
163            if error_level == Level::Unspecified {
164                error_level = DEFAULT_LOG_LEVEL;
165            }
166
167            match error_level {
168                Level::Trace => {
169                    tracing::trace!("{}", error.message);
170                },
171                Level::Debug => {
172                    tracing::debug!("{}", error.message);
173                },
174                Level::Info => {
175                    tracing::info!("{}", error.message);
176                },
177                Level::Warn => {
178                    tracing::warn!("{}", error.message);
179                },
180                Level::Error => {
181                    tracing::error!("{}", error.message);
182                },
183                // impossible
184                Level::Unspecified => {},
185            }
186        }
187    };
188}
189pub use log;
190
191#[macro_export]
192/// Check that the given condition holds, otherwise return an error.
193///
194/// The argument can be either:
195///   - a condition, in which case a generic error is logged at the `Unspecified` level.
196///   - a condition and a string literal, in which case the provided literal is logged at the `Unspecified` level.
197///   - a condition and a format expression, in which case the message is formatted and logged at the `Unspecified` level.
198///   - a condition and an `Error`, in which case the given error is logged unchanged.
199macro_rules! ensure {
200  ($condition:expr) => {
201      if !$condition {
202        let result = Err(Error {
203          level: Level::Unspecified,
204          message: format!("{}: condition '{}' failed.", line_info!(), stringify!($condition))
205        });
206
207        log!(result);
208
209        return result;
210     }
211  };
212  ($condition:expr, $message:literal) => {
213      if !$condition {
214        let result = Err(Error {
215          level: Level::Unspecified,
216          message: format!("{}: {}", line_info!(), format!($message))
217        });
218
219        log!(result);
220
221        return result;
222      }
223  };
224  ($condition:expr, $fmt:expr, $($arg:tt)*) => {
225      if !$condition {
226        let result = Err(Error {
227          level: Level::Unspecified,
228          message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
229        });
230
231        log!(result);
232
233        return result;
234      }
235  };
236  ($condition:expr, $error:expr) => {
237      if !$condition {
238        let result = Err($error);
239
240        log!(result);
241
242        return result;
243      }
244  };
245}
246pub use ensure;
247
248#[macro_export]
249/// Return an error.
250///
251/// The argument can be either:
252///   - nothing, in which case a generic message is logged at the `Unspecified` level.
253///   - a string literal, in which case the provided literal is logged at the `Unspecified` level.
254///   - a format expression, in which case the message is formatted and logged at the `Unspecified` level.
255///   - an `Error`, in which case the given error is logged unchanged.
256macro_rules! bail {
257  () => {
258      let result = Err(Error {
259        level: Level::Unspecified,
260        message: format!("{}: bailed.", line_info!()),
261      });
262
263      log!(result);
264
265      return result;
266  };
267  ($message:literal) => {
268      let result = Err(Error {
269        level: Level::Unspecified,
270        message: format!("{}: {}", line_info!(), format!($message))
271      });
272
273      log!(result);
274
275      return result;
276  };
277  ($fmt:expr, $($arg:tt)*) => {
278      let result = Err(Error {
279        level: Level::Unspecified,
280        message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
281      });
282
283      log!(result);
284
285      return result;
286  };
287  ($error:expr) => {
288      let result = Err($error);
289
290      log!(result);
291
292      return result;
293  };
294}
295pub use bail;