Skip to content

Commit 244b44d

Browse files
mxindendjc
authored andcommitted
refactor(udp): introduce log facade
Add `log` facade that either dispatches to `tracing` or `log`, or to a no-op implementation.
1 parent 2ba4966 commit 244b44d

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

quinn-udp/src/lib.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ use std::{
3737
time::{Duration, Instant},
3838
};
3939

40-
#[cfg(all(feature = "direct-log", not(feature = "tracing")))]
41-
use log::warn;
42-
#[cfg(feature = "tracing")]
43-
use tracing::warn;
44-
4540
#[cfg(any(unix, windows))]
4641
mod cmsg;
4742

@@ -58,6 +53,29 @@ mod imp;
5853
#[path = "fallback.rs"]
5954
mod imp;
6055

56+
#[allow(unused_imports, unused_macros)]
57+
mod log {
58+
#[cfg(all(feature = "direct-log", not(feature = "tracing")))]
59+
pub(crate) use log::{debug, error, info, trace, warn};
60+
61+
#[cfg(feature = "tracing")]
62+
pub(crate) use tracing::{debug, error, info, trace, warn};
63+
64+
#[cfg(not(any(feature = "direct-log", feature = "tracing")))]
65+
mod no_op {
66+
macro_rules! trace ( ($($tt:tt)*) => {{}} );
67+
macro_rules! debug ( ($($tt:tt)*) => {{}} );
68+
macro_rules! info ( ($($tt:tt)*) => {{}} );
69+
macro_rules! log_warn ( ($($tt:tt)*) => {{}} );
70+
macro_rules! error ( ($($tt:tt)*) => {{}} );
71+
72+
pub(crate) use {debug, error, info, log_warn as warn, trace};
73+
}
74+
75+
#[cfg(not(any(feature = "direct-log", feature = "tracing")))]
76+
pub(crate) use no_op::*;
77+
}
78+
6179
pub use imp::UdpSocketState;
6280

6381
/// Number of UDP packets to send/receive at a time
@@ -139,7 +157,7 @@ fn log_sendmsg_error(
139157
let last_send_error = &mut *last_send_error.lock().expect("poisend lock");
140158
if now.saturating_duration_since(*last_send_error) > IO_ERROR_LOG_INTERVAL {
141159
*last_send_error = now;
142-
warn!(
160+
log::warn!(
143161
"sendmsg error: {:?}, Transmit: {{ destination: {:?}, src_ip: {:?}, enc: {:?}, len: {:?}, segment_size: {:?} }}",
144162
err, transmit.destination, transmit.src_ip, transmit.ecn, transmit.contents.len(), transmit.segment_size);
145163
}

quinn-udp/src/unix.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ use std::{
1212
time::Instant,
1313
};
1414

15-
#[cfg(all(feature = "direct-log", not(feature = "tracing")))]
16-
use log::{debug, error};
1715
use socket2::SockRef;
18-
#[cfg(feature = "tracing")]
19-
use tracing::{debug, error};
2016

2117
use super::{
22-
cmsg, log_sendmsg_error, EcnCodepoint, RecvMeta, Transmit, UdpSockRef, IO_ERROR_LOG_INTERVAL,
18+
cmsg, log::debug, log_sendmsg_error, EcnCodepoint, RecvMeta, Transmit, UdpSockRef,
19+
IO_ERROR_LOG_INTERVAL,
2320
};
2421

2522
// Defined in netinet6/in6.h on OpenBSD, this is not yet exported by the libc crate
@@ -89,11 +86,10 @@ impl UdpSocketState {
8986
// older macos versions also don't have the flag and will error out if we don't ignore it
9087
#[cfg(not(any(target_os = "openbsd", target_os = "netbsd")))]
9188
if is_ipv4 || !io.only_v6()? {
92-
#[allow(unused_variables)]
93-
if let Err(err) = set_socket_option(&*io, libc::IPPROTO_IP, libc::IP_RECVTOS, OPTION_ON)
89+
if let Err(_err) =
90+
set_socket_option(&*io, libc::IPPROTO_IP, libc::IP_RECVTOS, OPTION_ON)
9491
{
95-
#[cfg(any(feature = "tracing", feature = "direct-log"))]
96-
debug!("Ignoring error setting IP_RECVTOS on socket: {err:?}");
92+
debug!("Ignoring error setting IP_RECVTOS on socket: {_err:?}");
9793
}
9894
}
9995

@@ -289,8 +285,7 @@ fn send(
289285
// Prevent new transmits from being scheduled using GSO. Existing GSO transmits
290286
// may already be in the pipeline, so we need to tolerate additional failures.
291287
if state.max_gso_segments() > 1 {
292-
#[cfg(any(feature = "tracing", feature = "direct-log"))]
293-
error!("got transmit error, halting segmentation offload");
288+
crate::log::error!("got transmit error, halting segmentation offload");
294289
state
295290
.max_gso_segments
296291
.store(1, std::sync::atomic::Ordering::Relaxed);

quinn-udp/src/windows.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ use std::{
99
};
1010

1111
use libc::{c_int, c_uint};
12-
#[cfg(all(feature = "direct-log", not(feature = "tracing")))]
13-
use log::{debug, error};
1412
use once_cell::sync::Lazy;
15-
#[cfg(feature = "tracing")]
16-
use tracing::{debug, error};
1713
use windows_sys::Win32::Networking::WinSock;
1814

1915
use crate::{
2016
cmsg::{self, CMsgHdr},
17+
log::{debug, error},
2118
log_sendmsg_error, EcnCodepoint, RecvMeta, Transmit, UdpSockRef, IO_ERROR_LOG_INTERVAL,
2219
};
2320

@@ -64,7 +61,6 @@ impl UdpSocketState {
6461

6562
// We don't support old versions of Windows that do not enable access to `WSARecvMsg()`
6663
if WSARECVMSG_PTR.is_none() {
67-
#[cfg(any(feature = "tracing", feature = "direct-log"))]
6864
error!("network stack does not support WSARecvMsg function");
6965

7066
return Err(io::Error::from(io::ErrorKind::Unsupported));
@@ -392,7 +388,6 @@ const OPTION_ON: u32 = 1;
392388
static WSARECVMSG_PTR: Lazy<WinSock::LPFN_WSARECVMSG> = Lazy::new(|| {
393389
let s = unsafe { WinSock::socket(WinSock::AF_INET as _, WinSock::SOCK_DGRAM as _, 0) };
394390
if s == WinSock::INVALID_SOCKET {
395-
#[cfg(any(feature = "tracing", feature = "direct-log"))]
396391
debug!(
397392
"ignoring WSARecvMsg function pointer due to socket creation error: {}",
398393
io::Error::last_os_error()
@@ -422,13 +417,11 @@ static WSARECVMSG_PTR: Lazy<WinSock::LPFN_WSARECVMSG> = Lazy::new(|| {
422417
};
423418

424419
if rc == -1 {
425-
#[cfg(any(feature = "tracing", feature = "direct-log"))]
426420
debug!(
427421
"ignoring WSARecvMsg function pointer due to ioctl error: {}",
428422
io::Error::last_os_error()
429423
);
430424
} else if len as usize != mem::size_of::<WinSock::LPFN_WSARECVMSG>() {
431-
#[cfg(any(feature = "tracing", feature = "direct-log"))]
432425
debug!("ignoring WSARecvMsg function pointer due to pointer size mismatch");
433426
wsa_recvmsg_ptr = None;
434427
}

0 commit comments

Comments
 (0)