Skip to content

Commit

Permalink
Remove unnecessary logging synchronisation (#381)
Browse files Browse the repository at this point in the history
* Revert 89e1140

- simplelog is already thread safe
- see Drakulix/simplelog.rs#146

* Write changelog

* Fix import for MacOS
  • Loading branch information
cyqsimon authored Mar 25, 2024
1 parent 1997bce commit 32506e7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/display/ui_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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:?}");
}
};
}
Expand Down
16 changes: 0 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down
17 changes: 4 additions & 13 deletions src/os/lsof_utils.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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;
};

Expand Down
8 changes: 4 additions & 4 deletions src/os/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
})
Expand All @@ -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
)
Expand Down

0 comments on commit 32506e7

Please sign in to comment.