summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcyqsimon <28627918+cyqsimon@users.noreply.github.com>2024-03-25 13:20:12 +0800
committerGitHub <noreply@github.com>2024-03-25 13:20:12 +0800
commit32506e7f245171b43b22df1bda7fbcfcc7717f3e (patch)
treed1f4704b9653b8bbecd52cdc442a8cabf896f049
parent1997bce393313d7ba685ee127d743e3a310530ac (diff)
Remove unnecessary logging synchronisation (#381)
* Revert 89e1140 - simplelog is already thread safe - see https://github.com/Drakulix/simplelog.rs/issues/146 * Write changelog * Fix import for MacOS
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/display/ui_state.rs10
-rw-r--r--src/main.rs16
-rw-r--r--src/os/lsof_utils.rs17
-rw-r--r--src/os/shared.rs8
5 files changed, 14 insertions, 38 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 38e910f..d2b68a2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## Fixed
* Remove redundant imports #377 - @cyqsimon
* CI: use GitHub API to exempt dependabot from changelog requirement #378 - @cyqsimon
+* Remove unnecessary logging synchronisation #381 - @cyqsimon
## Added
* CI: include generated assets in release archive #359 - @cyqsimon
diff --git a/src/display/ui_state.rs b/src/display/ui_state.rs
index 20085d0..e01bbf3 100644
--- a/src/display/ui_state.rs
+++ b/src/display/ui_state.rs
@@ -5,9 +5,10 @@ use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr},
};
+use log::warn;
+
use crate::{
display::BandwidthUnitFamily,
- mt_log,
network::{Connection, LocalSocket, Utilization},
os::ProcessInfo,
};
@@ -163,15 +164,14 @@ impl UIState {
.map(|conn| (conn, info))
}) {
Some((lookalike, proc_info)) => {
- mt_log!(
- warn,
+ warn!(
r#""{0}" owns a similar looking connection, but its local ip doesn't match."#,
proc_info.name
);
- mt_log!(warn, "Looking for: {connection:?}; found: {lookalike:?}");
+ warn!("Looking for: {connection:?}; found: {lookalike:?}");
}
None => {
- mt_log!(warn, "Cannot determine which process owns {connection:?}");
+ warn!("Cannot determine which process owns {connection:?}");
}
};
}
diff --git a/src/main.rs b/src/main.rs
index 756e46d..3434adb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -28,7 +28,6 @@ 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;
@@ -38,21 +37,6 @@ use crate::os::ProcessInfo;
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();
diff --git a/src/os/lsof_utils.rs b/src/os/lsof_utils.rs
index 6fd61f8..4a61c67 100644
--- a/src/os/lsof_utils.rs
+++ b/src/os/lsof_utils.rs
@@ -1,10 +1,10 @@
use std::{ffi::OsStr, net::IpAddr, process::Command};
+use log::warn;
use once_cell::sync::Lazy;
use regex::Regex;
use crate::{
- mt_log,
network::{LocalSocket, Protocol},
os::ProcessInfo,
};
@@ -123,24 +123,15 @@ impl RawConnection {
let process = &self.proc_info.name;
let Some(ip) = self.get_local_ip() else {
- mt_log!(
- warn,
- r#"Failed to get the local IP of a connection belonging to "{process}"."#
- );
+ warn!(r#"Failed to get the local IP of a connection belonging to "{process}"."#);
return None;
};
let Some(port) = self.get_local_port() else {
- mt_log!(
- warn,
- r#"Failed to get the local port of a connection belonging to "{process}"."#
- );
+ warn!(r#"Failed to get the local port of a connection belonging to "{process}"."#);
return None;
};
let Some(protocol) = self.get_protocol() else {
- mt_log!(
- warn,
- r#"Failed to get the protocol of a connection belonging to "{process}"."#
- );
+ warn!(r#"Failed to get the protocol of a connection belonging to "{process}"."#);
return None;
};
diff --git a/src/os/shared.rs b/src/os/shared.rs
index d8dd477..fbe3559 100644
--- a/src/os/shared.rs
+++ b/src/os/shared.rs
@@ -7,10 +7,11 @@ use std::{
use anyhow::{anyhow, bail};
use crossterm::event::{read, Event};
use itertools::Itertools;
+use log::{debug, warn};
use pnet::datalink::{self, Channel::Ethernet, Config, DataLinkReceiver, NetworkInterface};
use tokio::runtime::Runtime;
-use crate::{mt_log, network::dns, os::errors::GetInterfaceError, OsInputOutput};
+use crate::{network::dns, os::errors::GetInterfaceError, OsInputOutput};
#[cfg(target_os = "linux")]
use crate::os::linux::get_open_sockets;
@@ -112,7 +113,7 @@ pub fn get_input(
interface.is_up() && !interface.ips.is_empty()
};
if !keep {
- mt_log!(debug, "{} is down. Skipping it.", interface.name);
+ debug!("{} is down. Skipping it.", interface.name);
}
keep
})
@@ -137,8 +138,7 @@ pub fn get_input(
.iter()
.filter_map(|(interface, frames_res)| frames_res.as_ref().err().map(|err| (interface, err)))
.for_each(|(interface, err)| {
- mt_log!(
- warn,
+ warn!(
"Failed to acquire a frame receiver for {}: {err}",
interface.name
)