Skip to content

Commit cd4bbbc

Browse files
committed
fix: make tracing dependency optional
1 parent c88e22b commit cd4bbbc

File tree

8 files changed

+87
-11
lines changed

8 files changed

+87
-11
lines changed

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(polling_test_poll_backend)
2323
[features]
2424
# Enable calling of `tracing` functions from `Drop` handlers.
2525
# Enabling this can lead to unexpected double panics during panic unwindings.
26-
tracing_in_drop = []
26+
tracing_in_drop = ["tracing"]
2727

2828
[dependencies]
2929
cfg-if = "1"
30-
tracing = { version = "0.1.37", default-features = false }
30+
31+
[dependencies.tracing]
32+
version = "0.1.37"
33+
default-features = false
34+
optional = true
3135

3236
[target.'cfg(any(unix, target_os = "fuchsia", target_os = "vxworks"))'.dependencies.rustix]
3337
version = "1.0.5"

src/epoll.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl Poller {
7777
)?;
7878
}
7979

80+
#[cfg(feature = "tracing")]
8081
tracing::trace!(
8182
epoll_fd = ?poller.epoll_fd.as_raw_fd(),
8283
notifier = ?poller.notifier,
@@ -102,12 +103,14 @@ impl Poller {
102103
/// The `fd` must be a valid file descriptor. The usual condition of remaining registered in
103104
/// the `Poller` doesn't apply to `epoll`.
104105
pub unsafe fn add(&self, fd: RawFd, ev: Event, mode: PollMode) -> io::Result<()> {
106+
#[cfg(feature = "tracing")]
105107
let span = tracing::trace_span!(
106108
"add",
107109
epoll_fd = ?self.epoll_fd.as_raw_fd(),
108110
?fd,
109111
?ev,
110112
);
113+
#[cfg(feature = "tracing")]
111114
let _enter = span.enter();
112115

113116
epoll::add(
@@ -122,12 +125,14 @@ impl Poller {
122125

123126
/// Modifies an existing file descriptor.
124127
pub fn modify(&self, fd: BorrowedFd<'_>, ev: Event, mode: PollMode) -> io::Result<()> {
128+
#[cfg(feature = "tracing")]
125129
let span = tracing::trace_span!(
126130
"modify",
127131
epoll_fd = ?self.epoll_fd.as_raw_fd(),
128132
?fd,
129133
?ev,
130134
);
135+
#[cfg(feature = "tracing")]
131136
let _enter = span.enter();
132137

133138
epoll::modify(
@@ -146,24 +151,29 @@ impl Poller {
146151
}
147152

148153
/// Deletes a file descriptor.
154+
#[cfg_attr(not(feature = "tracing"), inline(always))]
149155
pub fn delete(&self, fd: BorrowedFd<'_>) -> io::Result<()> {
156+
#[cfg(feature = "tracing")]
150157
let span = tracing::trace_span!(
151158
"delete",
152159
epoll_fd = ?self.epoll_fd.as_raw_fd(),
153160
?fd,
154161
);
162+
#[cfg(feature = "tracing")]
155163
let _enter = span.enter();
156164
self.delete_intern(fd)
157165
}
158166

159167
/// Waits for I/O events with an optional timeout.
160168
#[allow(clippy::needless_update)]
161169
pub fn wait(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<()> {
170+
#[cfg(feature = "tracing")]
162171
let span = tracing::trace_span!(
163172
"wait",
164173
epoll_fd = ?self.epoll_fd.as_raw_fd(),
165174
?timeout,
166175
);
176+
#[cfg(feature = "tracing")]
167177
let _enter = span.enter();
168178

169179
#[cfg(not(target_os = "redox"))]
@@ -211,6 +221,7 @@ impl Poller {
211221
spare_capacity(&mut events.list),
212222
timeout.as_ref(),
213223
)?;
224+
#[cfg(feature = "tracing")]
214225
tracing::trace!(
215226
epoll_fd = ?self.epoll_fd.as_raw_fd(),
216227
res = ?events.list.len(),
@@ -229,11 +240,13 @@ impl Poller {
229240

230241
/// Sends a notification to wake up the current or next `wait()` call.
231242
pub fn notify(&self) -> io::Result<()> {
243+
#[cfg(feature = "tracing")]
232244
let span = tracing::trace_span!(
233245
"notify",
234246
epoll_fd = ?self.epoll_fd.as_raw_fd(),
235247
notifier = ?self.notifier,
236248
);
249+
#[cfg(feature = "tracing")]
237250
let _enter = span.enter();
238251

239252
self.notifier.notify();
@@ -438,14 +451,16 @@ impl Notifier {
438451
// Try to create an eventfd.
439452
match eventfd(0, EventfdFlags::CLOEXEC | EventfdFlags::NONBLOCK) {
440453
Ok(fd) => {
454+
#[cfg(feature = "tracing")]
441455
tracing::trace!("created eventfd for notifier");
442456
return Ok(Notifier::EventFd(fd));
443457
}
444458

445-
Err(err) => {
459+
Err(_err) => {
460+
#[cfg(feature = "tracing")]
446461
tracing::warn!(
447462
"eventfd() failed with error ({}), falling back to pipe",
448-
err
463+
_err
449464
);
450465
}
451466
}

src/iocp/afd.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ macro_rules! define_ntdll_import {
197197
let addr = match addr {
198198
Some(addr) => addr,
199199
None => {
200+
#[cfg(feature = "tracing")]
200201
tracing::error!("Failed to load ntdll function {}", NAME);
201202
return Err(io::Error::last_os_error());
202203
},
@@ -298,6 +299,7 @@ impl NtdllImports {
298299
let ntdll = GetModuleHandleW(NTDLL_NAME.as_ptr() as *const _);
299300

300301
if ntdll.is_null() {
302+
#[cfg(feature = "tracing")]
301303
tracing::error!("Failed to load ntdll.dll");
302304
return Err(io::Error::last_os_error());
303305
}

src/iocp/mod.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl Poller {
136136
})?;
137137

138138
let port = IoCompletionPort::new(0)?;
139+
#[cfg(feature = "tracing")]
139140
tracing::trace!(handle = ?port, "new");
140141

141142
Ok(Poller {
@@ -175,12 +176,14 @@ impl Poller {
175176
interest: Event,
176177
mode: PollMode,
177178
) -> io::Result<()> {
179+
#[cfg(feature = "tracing")]
178180
let span = tracing::trace_span!(
179181
"add",
180182
handle = ?self.port,
181183
sock = ?socket,
182184
ev = ?interest,
183185
);
186+
#[cfg(feature = "tracing")]
184187
let _enter = span.enter();
185188

186189
// We don't support edge-triggered events.
@@ -238,12 +241,14 @@ impl Poller {
238241
interest: Event,
239242
mode: PollMode,
240243
) -> io::Result<()> {
244+
#[cfg(feature = "tracing")]
241245
let span = tracing::trace_span!(
242246
"modify",
243247
handle = ?self.port,
244248
sock = ?socket,
245249
ev = ?interest,
246250
);
251+
#[cfg(feature = "tracing")]
247252
let _enter = span.enter();
248253

249254
// We don't support edge-triggered events.
@@ -275,11 +280,13 @@ impl Poller {
275280

276281
/// Delete a source from the poller.
277282
pub(super) fn delete(&self, socket: BorrowedSocket<'_>) -> io::Result<()> {
283+
#[cfg(feature = "tracing")]
278284
let span = tracing::trace_span!(
279285
"remove",
280286
handle = ?self.port,
281287
sock = ?socket,
282288
);
289+
#[cfg(feature = "tracing")]
283290
let _enter = span.enter();
284291

285292
// Remove the source from our associative map.
@@ -307,6 +314,7 @@ impl Poller {
307314
interest: Event,
308315
mode: PollMode,
309316
) -> io::Result<()> {
317+
#[cfg(feature = "tracing")]
310318
tracing::trace!(
311319
"add_waitable: handle={:?}, waitable={:p}, ev={:?}",
312320
self.port,
@@ -363,6 +371,7 @@ impl Poller {
363371
interest: Event,
364372
mode: PollMode,
365373
) -> io::Result<()> {
374+
#[cfg(feature = "tracing")]
366375
tracing::trace!(
367376
"modify_waitable: handle={:?}, waitable={:p}, ev={:?}",
368377
self.port,
@@ -398,6 +407,7 @@ impl Poller {
398407

399408
/// Delete a waitable from the poller.
400409
pub(super) fn remove_waitable(&self, waitable: RawHandle) -> io::Result<()> {
410+
#[cfg(feature = "tracing")]
401411
tracing::trace!("remove: handle={:?}, waitable={:p}", self.port, waitable);
402412

403413
// Get a reference to the source.
@@ -420,11 +430,13 @@ impl Poller {
420430

421431
/// Wait for events.
422432
pub(super) fn wait(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<()> {
433+
#[cfg(feature = "tracing")]
423434
let span = tracing::trace_span!(
424435
"wait",
425436
handle = ?self.port,
426437
?timeout,
427438
);
439+
#[cfg(feature = "tracing")]
428440
let _enter = span.enter();
429441

430442
// Make sure we have a consistent timeout.
@@ -451,10 +463,11 @@ impl Poller {
451463
let timeout = deadline.map(|t| t.saturating_duration_since(Instant::now()));
452464

453465
// Wait for I/O events.
454-
let len = self.port.wait(&mut events.completions, timeout)?;
466+
let _len = self.port.wait(&mut events.completions, timeout)?;
467+
#[cfg(feature = "tracing")]
455468
tracing::trace!(
456469
handle = ?self.port,
457-
res = ?len,
470+
res = ?_len,
458471
"new events");
459472

460473
// We are no longer polling.
@@ -484,6 +497,7 @@ impl Poller {
484497
break;
485498
}
486499

500+
#[cfg(feature = "tracing")]
487501
tracing::trace!("wait: no events found, re-entering polling loop");
488502
}
489503

@@ -860,8 +874,9 @@ impl PacketUnwrapped {
860874

861875
// Push this packet.
862876
drop(handle);
863-
if let Err(e) = iocp.post(0, 0, packet) {
864-
tracing::error!("failed to post completion packet: {}", e);
877+
if let Err(_e) = iocp.post(0, 0, packet) {
878+
#[cfg(feature = "tracing")]
879+
tracing::error!("failed to post completion packet: {}", _e);
865880
}
866881
},
867882
None,

src/kqueue.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl Poller {
6363
// Register the notification pipe.
6464
poller.notify.register(&poller)?;
6565

66+
#[cfg(feature = "tracing")]
6667
tracing::trace!(
6768
kqueue_fd = ?poller.kqueue_fd.as_raw_fd(),
6869
"new"
@@ -94,6 +95,7 @@ impl Poller {
9495

9596
/// Modifies an existing file descriptor.
9697
pub fn modify(&self, fd: BorrowedFd<'_>, ev: Event, mode: PollMode) -> io::Result<()> {
98+
#[cfg(feature = "tracing")]
9799
let span = if !self.notify.has_fd(fd) {
98100
let span = tracing::trace_span!(
99101
"add",
@@ -105,6 +107,7 @@ impl Poller {
105107
} else {
106108
None
107109
};
110+
#[cfg(feature = "tracing")]
108111
let _enter = span.as_ref().map(|s| s.enter());
109112

110113
self.has_source(SourceId::Fd(fd.as_raw_fd()))?;
@@ -233,11 +236,13 @@ impl Poller {
233236

234237
/// Waits for I/O events with an optional timeout.
235238
pub fn wait(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<()> {
239+
#[cfg(feature = "tracing")]
236240
let span = tracing::trace_span!(
237241
"wait",
238242
kqueue_fd = ?self.kqueue_fd.as_raw_fd(),
239243
?timeout,
240244
);
245+
#[cfg(feature = "tracing")]
241246
let _enter = span.enter();
242247

243248
// Timeout for kevent. In case of overflow, use no timeout.
@@ -248,7 +253,7 @@ impl Poller {
248253

249254
// Wait for I/O events.
250255
let changelist = [];
251-
let res = unsafe {
256+
let _res = unsafe {
252257
kqueue::kevent_timespec(
253258
&self.kqueue_fd,
254259
&changelist,
@@ -257,9 +262,10 @@ impl Poller {
257262
)?
258263
};
259264

265+
#[cfg(feature = "tracing")]
260266
tracing::trace!(
261267
kqueue_fd = ?self.kqueue_fd.as_raw_fd(),
262-
?res,
268+
res = ?_res,
263269
"new events",
264270
);
265271

@@ -271,10 +277,12 @@ impl Poller {
271277

272278
/// Sends a notification to wake up the current or next `wait()` call.
273279
pub fn notify(&self) -> io::Result<()> {
280+
#[cfg(feature = "tracing")]
274281
let span = tracing::trace_span!(
275282
"notify",
276283
kqueue_fd = ?self.kqueue_fd.as_raw_fd(),
277284
);
285+
#[cfg(feature = "tracing")]
278286
let _enter = span.enter();
279287

280288
self.notify.notify(self).ok();
@@ -423,6 +431,7 @@ mod notify {
423431
use super::Poller;
424432
use rustix::event::kqueue;
425433
use std::io;
434+
#[cfg(feature = "tracing")]
426435
use std::os::unix::io::BorrowedFd;
427436

428437
/// A notification pipe.
@@ -488,6 +497,7 @@ mod notify {
488497
}
489498

490499
/// Whether this raw file descriptor is associated with this pipe.
500+
#[cfg(feature = "tracing")]
491501
pub(super) fn has_fd(&self, _fd: BorrowedFd<'_>) -> bool {
492502
false
493503
}
@@ -503,8 +513,10 @@ mod notify {
503513
use super::Poller;
504514
use crate::{Event, PollMode, NOTIFY_KEY};
505515
use std::io::{self, prelude::*};
516+
#[cfg(feature = "tracing")]
517+
use std::os::unix::io::BorrowedFd;
506518
use std::os::unix::{
507-
io::{AsFd, AsRawFd, BorrowedFd},
519+
io::{AsFd, AsRawFd},
508520
net::UnixStream,
509521
};
510522

@@ -574,6 +586,7 @@ mod notify {
574586
}
575587

576588
/// Whether this raw file descriptor is associated with this pipe.
589+
#[cfg(feature = "tracing")]
577590
pub(super) fn has_fd(&self, fd: BorrowedFd<'_>) -> bool {
578591
self.read_stream.as_raw_fd() == fd.as_raw_fd()
579592
}

0 commit comments

Comments
 (0)