Skip to content

Commit 8c0aae4

Browse files
committed
Add a filter these logs INFO gotatun::device::uapi: New UAPI connection on unix socket
1 parent 49a989e commit 8c0aae4

2 files changed

Lines changed: 51 additions & 14 deletions

File tree

src/logging.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::cell::RefCell;
22
use std::fs::{File, OpenOptions};
33
use std::io::Write;
4+
use std::sync::atomic::{AtomicUsize, Ordering};
45
use std::sync::{Arc, Once};
56

67
use time::macros::format_description;
@@ -10,6 +11,33 @@ use tracing_subscriber::fmt::time::UtcTime;
1011
const LOG_TIMESTAMP_FORMAT: &[time::format_description::FormatItem<'static>] =
1112
format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]Z");
1213
const DEBUG_ENV: &str = "TUNMUX_DEBUG";
14+
const GOTATUN_UAPI_CONNECTION_TARGET: &str = "gotatun::device::uapi";
15+
const GOTATUN_UAPI_CONNECTION_MESSAGE: &str = "New UAPI connection on unix socket";
16+
17+
static SUPPRESS_GOTATUN_UAPI_CONNECTION_LOGS: AtomicUsize = AtomicUsize::new(0);
18+
19+
pub struct GotatunUapiConnectionLogSuppression;
20+
21+
impl Drop for GotatunUapiConnectionLogSuppression {
22+
fn drop(&mut self) {
23+
SUPPRESS_GOTATUN_UAPI_CONNECTION_LOGS.fetch_sub(1, Ordering::Relaxed);
24+
}
25+
}
26+
27+
pub fn suppress_gotatun_uapi_connection_logs() -> GotatunUapiConnectionLogSuppression {
28+
SUPPRESS_GOTATUN_UAPI_CONNECTION_LOGS.fetch_add(1, Ordering::Relaxed);
29+
GotatunUapiConnectionLogSuppression
30+
}
31+
32+
fn should_suppress_log_write(buf: &[u8]) -> bool {
33+
if SUPPRESS_GOTATUN_UAPI_CONNECTION_LOGS.load(Ordering::Relaxed) == 0 {
34+
return false;
35+
}
36+
let Ok(line) = std::str::from_utf8(buf) else {
37+
return false;
38+
};
39+
line.contains(GOTATUN_UAPI_CONNECTION_TARGET) && line.contains(GOTATUN_UAPI_CONNECTION_MESSAGE)
40+
}
1341

1442
fn level_from_env_or_default(default: LevelFilter) -> LevelFilter {
1543
if debug_enabled() {
@@ -141,6 +169,9 @@ struct ServiceWriter;
141169

142170
impl Write for ServiceWriter {
143171
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
172+
if should_suppress_log_write(buf) {
173+
return Ok(buf.len());
174+
}
144175
let _ = std::io::stderr().write_all(buf);
145176
LOG_CAPTURE.with(|cell| {
146177
if let Some(capture) = cell.borrow_mut().as_mut() {
@@ -187,6 +218,9 @@ struct SharedFileWriter(Arc<File>);
187218

188219
impl Write for SharedFileWriter {
189220
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
221+
if should_suppress_log_write(buf) {
222+
return Ok(buf.len());
223+
}
190224
(&*self.0).write(buf)
191225
}
192226

src/userspace_helper.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -612,20 +612,23 @@ async fn read_wg_transfer_bytes(interface: &str) -> anyhow::Result<Option<(u64,
612612
// the task that `wg` is waiting on). Run it on a blocking thread, bounded by a timeout, so
613613
// the runtime stays free to answer the UAPI request and a stuck `wg` can never wedge us.
614614
let owned_interface = interface.to_string();
615-
let output = match tokio::time::timeout(
616-
Duration::from_secs(4),
617-
tokio::task::spawn_blocking(move || {
618-
Command::new("wg")
619-
.args(["show", &owned_interface, "transfer"])
620-
.output()
621-
}),
622-
)
623-
.await
624-
{
625-
Ok(join_result) => join_result
626-
.context("wg show transfer task panicked")?
627-
.context("failed to run wg show transfer")?,
628-
Err(_) => anyhow::bail!("wg show {} transfer timed out", interface),
615+
let output = {
616+
let _suppress_probe_uapi_log = crate::logging::suppress_gotatun_uapi_connection_logs();
617+
match tokio::time::timeout(
618+
Duration::from_secs(4),
619+
tokio::task::spawn_blocking(move || {
620+
Command::new("wg")
621+
.args(["show", &owned_interface, "transfer"])
622+
.output()
623+
}),
624+
)
625+
.await
626+
{
627+
Ok(join_result) => join_result
628+
.context("wg show transfer task panicked")?
629+
.context("failed to run wg show transfer")?,
630+
Err(_) => anyhow::bail!("wg show {} transfer timed out", interface),
631+
}
629632
};
630633
if !output.status.success() {
631634
anyhow::bail!("wg show {} transfer failed", interface);

0 commit comments

Comments
 (0)