Skip to content

Commit 32506e7

Browse files
authored
Remove unnecessary logging synchronisation (#381)
* Revert 89e1140 - simplelog is already thread safe - see Drakulix/simplelog.rs#146 * Write changelog * Fix import for MacOS
1 parent 1997bce commit 32506e7

File tree

5 files changed

+14
-38
lines changed

5 files changed

+14
-38
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
99
## Fixed
1010
* Remove redundant imports #377 - @cyqsimon
1111
* CI: use GitHub API to exempt dependabot from changelog requirement #378 - @cyqsimon
12+
* Remove unnecessary logging synchronisation #381 - @cyqsimon
1213

1314
## Added
1415
* CI: include generated assets in release archive #359 - @cyqsimon

src/display/ui_state.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use std::{
55
net::{IpAddr, Ipv4Addr, Ipv6Addr},
66
};
77

8+
use log::warn;
9+
810
use crate::{
911
display::BandwidthUnitFamily,
10-
mt_log,
1112
network::{Connection, LocalSocket, Utilization},
1213
os::ProcessInfo,
1314
};
@@ -163,15 +164,14 @@ impl UIState {
163164
.map(|conn| (conn, info))
164165
}) {
165166
Some((lookalike, proc_info)) => {
166-
mt_log!(
167-
warn,
167+
warn!(
168168
r#""{0}" owns a similar looking connection, but its local ip doesn't match."#,
169169
proc_info.name
170170
);
171-
mt_log!(warn, "Looking for: {connection:?}; found: {lookalike:?}");
171+
warn!("Looking for: {connection:?}; found: {lookalike:?}");
172172
}
173173
None => {
174-
mt_log!(warn, "Cannot determine which process owns {connection:?}");
174+
warn!("Cannot determine which process owns {connection:?}");
175175
}
176176
};
177177
}

src/main.rs

-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use network::{
2828
dns::{self, IpTable},
2929
LocalSocket, Sniffer, Utilization,
3030
};
31-
use once_cell::sync::Lazy;
3231
use pnet::datalink::{DataLinkReceiver, NetworkInterface};
3332
use ratatui::backend::{Backend, CrosstermBackend};
3433
use simplelog::WriteLogger;
@@ -38,21 +37,6 @@ use crate::os::ProcessInfo;
3837

3938
const DISPLAY_DELTA: Duration = Duration::from_millis(1000);
4039

41-
/// Lock guard to prevent races during logging.
42-
static LOG_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
43-
44-
/// Thread-safe log macro with a global Mutex guard.
45-
#[macro_export]
46-
macro_rules! mt_log {
47-
($log_macro: ident, $($fmt_args:expr),*) => {{
48-
let guard = $crate::LOG_LOCK
49-
.lock()
50-
.unwrap_or_else(|poisoned| poisoned.into_inner());
51-
log::$log_macro!($($fmt_args,)*);
52-
drop(guard);
53-
}};
54-
}
55-
5640
fn main() -> anyhow::Result<()> {
5741
let opts = Opt::parse();
5842

src/os/lsof_utils.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::{ffi::OsStr, net::IpAddr, process::Command};
22

3+
use log::warn;
34
use once_cell::sync::Lazy;
45
use regex::Regex;
56

67
use crate::{
7-
mt_log,
88
network::{LocalSocket, Protocol},
99
os::ProcessInfo,
1010
};
@@ -123,24 +123,15 @@ impl RawConnection {
123123
let process = &self.proc_info.name;
124124

125125
let Some(ip) = self.get_local_ip() else {
126-
mt_log!(
127-
warn,
128-
r#"Failed to get the local IP of a connection belonging to "{process}"."#
129-
);
126+
warn!(r#"Failed to get the local IP of a connection belonging to "{process}"."#);
130127
return None;
131128
};
132129
let Some(port) = self.get_local_port() else {
133-
mt_log!(
134-
warn,
135-
r#"Failed to get the local port of a connection belonging to "{process}"."#
136-
);
130+
warn!(r#"Failed to get the local port of a connection belonging to "{process}"."#);
137131
return None;
138132
};
139133
let Some(protocol) = self.get_protocol() else {
140-
mt_log!(
141-
warn,
142-
r#"Failed to get the protocol of a connection belonging to "{process}"."#
143-
);
134+
warn!(r#"Failed to get the protocol of a connection belonging to "{process}"."#);
144135
return None;
145136
};
146137

src/os/shared.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use std::{
77
use anyhow::{anyhow, bail};
88
use crossterm::event::{read, Event};
99
use itertools::Itertools;
10+
use log::{debug, warn};
1011
use pnet::datalink::{self, Channel::Ethernet, Config, DataLinkReceiver, NetworkInterface};
1112
use tokio::runtime::Runtime;
1213

13-
use crate::{mt_log, network::dns, os::errors::GetInterfaceError, OsInputOutput};
14+
use crate::{network::dns, os::errors::GetInterfaceError, OsInputOutput};
1415

1516
#[cfg(target_os = "linux")]
1617
use crate::os::linux::get_open_sockets;
@@ -112,7 +113,7 @@ pub fn get_input(
112113
interface.is_up() && !interface.ips.is_empty()
113114
};
114115
if !keep {
115-
mt_log!(debug, "{} is down. Skipping it.", interface.name);
116+
debug!("{} is down. Skipping it.", interface.name);
116117
}
117118
keep
118119
})
@@ -137,8 +138,7 @@ pub fn get_input(
137138
.iter()
138139
.filter_map(|(interface, frames_res)| frames_res.as_ref().err().map(|err| (interface, err)))
139140
.for_each(|(interface, err)| {
140-
mt_log!(
141-
warn,
141+
warn!(
142142
"Failed to acquire a frame receiver for {}: {err}",
143143
interface.name
144144
)

0 commit comments

Comments
 (0)