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