iota_common/
logging.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5#[macro_export]
6macro_rules! fatal {
7    ($($arg:tt)*) => {{
8        tracing::error!(fatal = true, $($arg)*);
9        panic!($($arg)*);
10    }};
11}
12
13#[macro_export]
14macro_rules! debug_fatal {
15    ($($arg:tt)*) => {{
16        if cfg!(debug_assertions) {
17            $crate::fatal!($($arg)*);
18        } else {
19            // TODO: Export invariant metric for alerting
20            tracing::error!(debug_fatal = true, $($arg)*);
21        }
22    }};
23}
24
25mod tests {
26    #[test]
27    #[should_panic]
28    fn test_fatal() {
29        fatal!("This is a fatal error");
30    }
31
32    #[test]
33    #[should_panic]
34    fn test_debug_fatal() {
35        if cfg!(debug_assertions) {
36            debug_fatal!("This is a debug fatal error");
37        } else {
38            // pass in release mode as well
39            fatal!("This is a fatal error");
40        }
41    }
42
43    #[cfg(not(debug_assertions))]
44    #[test]
45    fn test_debug_fatal_release_mode() {
46        debug_fatal!("This is a debug fatal error");
47    }
48}