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            tracing::error!(debug_fatal = true, $($arg)*);
20            let location = concat!(file!(), ':', line!());
21            if let Some(metrics) = iota_metrics::get_metrics() {
22                metrics.system_invariant_violations.with_label_values(&[location]).inc();
23            }
24        }
25    }};
26}
27
28mod tests {
29    #[test]
30    #[should_panic]
31    fn test_fatal() {
32        fatal!("This is a fatal error");
33    }
34
35    #[test]
36    #[should_panic]
37    fn test_debug_fatal() {
38        if cfg!(debug_assertions) {
39            debug_fatal!("This is a debug fatal error");
40        } else {
41            // pass in release mode as well
42            fatal!("This is a fatal error");
43        }
44    }
45
46    #[cfg(not(debug_assertions))]
47    #[test]
48    fn test_debug_fatal_release_mode() {
49        debug_fatal!("This is a debug fatal error");
50    }
51}