longshot/
logging.rs

1//! Logging utilities.
2
3use std::sync::atomic::AtomicBool;
4pub(crate) static TRACE_ENABLED: AtomicBool = AtomicBool::new(false);
5
6/// Enable tracing display to standard error.
7pub fn enable_tracing() {
8    TRACE_ENABLED.store(true, std::sync::atomic::Ordering::Relaxed);
9}
10
11/// Writes a trace of the given communication packet or event if [`enable_tracing`] has been called.
12#[macro_export]
13macro_rules! trace_packet {
14    ($($arg:tt)*) => {{
15        if $crate::logging::TRACE_ENABLED.load(std::sync::atomic::Ordering::Relaxed) {
16            $crate::display::log($crate::display::LogLevel::Trace, &format!("{}", std::format!($($arg)*)));
17        }
18    }};
19}
20
21/// Writes a trace of the given shutdown event if [`enable_tracing`] has been called.
22#[macro_export]
23macro_rules! trace_shutdown {
24    ($arg:literal) => {{
25        if $crate::logging::TRACE_ENABLED.load(std::sync::atomic::Ordering::Relaxed) {
26            $crate::display::log(
27                $crate::display::LogLevel::Trace,
28                &format!("[SHUTDOWN] {}", $arg),
29            );
30        }
31    }};
32}
33
34/// Writes a warning of the given event if [`enable_tracing`] has been called.
35#[macro_export]
36macro_rules! warning {
37    ($($arg:tt)*) => {{
38        if $crate::logging::TRACE_ENABLED.load(std::sync::atomic::Ordering::Relaxed) {
39            $crate::display::log($crate::display::LogLevel::Warning, &std::format!($($arg)*));
40        }
41    }};
42}
43
44/// Writes info text for the given event.
45#[macro_export]
46macro_rules! info {
47    ($($arg:tt)*) => {{
48        $crate::display::log($crate::display::LogLevel::Info, &std::format!($($arg)*));
49    }};
50}