diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index 72d4528..87c5cf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,7 @@ use network::{ dns::{self, IpTable}, LocalSocket, Sniffer, Utilization, }; +use once_cell::sync::Lazy; use pnet::datalink::{DataLinkReceiver, NetworkInterface}; use ratatui::backend::{Backend, CrosstermBackend}; use simplelog::WriteLogger; @@ -36,6 +37,21 @@ use crate::cli::Opt; const DISPLAY_DELTA: Duration = Duration::from_millis(1000); +/// Lock guard to prevent races during logging. +static LOG_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(())); + +/// Thread-safe log macro with a global Mutex guard. +#[macro_export] +macro_rules! mt_log { + ($log_macro: ident, $($fmt_args:expr),*) => {{ + let guard = $crate::LOG_LOCK + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()); + log::$log_macro!($($fmt_args,)*); + drop(guard); + }}; +} + fn main() -> anyhow::Result<()> { let opts = Opt::parse(); |