Skip to content

Commit a1f4c0a

Browse files
authored
feat: Replace log with tracing (#140)
Replaces the `log` crate with `tracing`. See this comment for more info: smol-rs/blocking#31 (comment)
1 parent 6ec079e commit a1f4c0a

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ cfg-if = "1"
2828
concurrent-queue = "2.2.0"
2929
futures-io = { version = "0.3.28", default-features = false, features = ["std"] }
3030
futures-lite = { version = "1.11.0", default-features = false }
31-
log = "0.4.11"
3231
parking = "2.0.0"
3332
polling = "2.6.0"
3433
rustix = { version = "0.37.1", default-features = false, features = ["std", "fs"] }
3534
slab = "0.4.2"
3635
socket2 = { version = "0.5.3", features = ["all"] }
36+
tracing = { version = "0.1.37", default-features = false }
3737
waker-fn = "1.1.0"
3838

3939
[build-dependencies]

src/driver.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ pub(crate) fn init() {
4343

4444
/// The main loop for the "async-io" thread.
4545
fn main_loop(parker: parking::Parker) {
46+
let span = tracing::trace_span!("async_io::main_loop");
47+
let _enter = span.enter();
48+
4649
// The last observed reactor tick.
4750
let mut last_tick = 0;
4851
// Number of sleeps since this thread has called `react()`.
@@ -61,7 +64,7 @@ fn main_loop(parker: parking::Parker) {
6164
};
6265

6366
if let Some(mut reactor_lock) = reactor_lock {
64-
log::trace!("main_loop: waiting on I/O");
67+
tracing::trace!("waiting on I/O");
6568
reactor_lock.react(None).ok();
6669
last_tick = Reactor::get().ticker();
6770
sleeps = 0;
@@ -76,9 +79,9 @@ fn main_loop(parker: parking::Parker) {
7679
.get(sleeps as usize)
7780
.unwrap_or(&10_000);
7881

79-
log::trace!("main_loop: sleeping for {} us", delay_us);
82+
tracing::trace!("sleeping for {} us", delay_us);
8083
if parker.park_timeout(Duration::from_micros(*delay_us)) {
81-
log::trace!("main_loop: notified");
84+
tracing::trace!("notified");
8285

8386
// If notified before timeout, reset the last tick and the sleep counter.
8487
last_tick = Reactor::get().ticker();
@@ -105,7 +108,8 @@ fn main_loop(parker: parking::Parker) {
105108
/// });
106109
/// ```
107110
pub fn block_on<T>(future: impl Future<Output = T>) -> T {
108-
log::trace!("block_on()");
111+
let span = tracing::trace_span!("async_io::block_on");
112+
let _enter = span.enter();
109113

110114
// Increment `BLOCK_ON_COUNT` so that the "async-io" thread becomes less aggressive.
111115
BLOCK_ON_COUNT.fetch_add(1, Ordering::SeqCst);
@@ -144,13 +148,13 @@ pub fn block_on<T>(future: impl Future<Output = T>) -> T {
144148
loop {
145149
// Poll the future.
146150
if let Poll::Ready(t) = future.as_mut().poll(cx) {
147-
log::trace!("block_on: completed");
151+
tracing::trace!("completed");
148152
return t;
149153
}
150154

151155
// Check if a notification was received.
152156
if p.park_timeout(Duration::from_secs(0)) {
153-
log::trace!("block_on: notified");
157+
tracing::trace!("notified");
154158

155159
// Try grabbing a lock on the reactor to process I/O events.
156160
if let Some(mut reactor_lock) = Reactor::get().try_lock() {
@@ -183,23 +187,23 @@ pub fn block_on<T>(future: impl Future<Output = T>) -> T {
183187
// Check if a notification has been received before `io_blocked` was updated
184188
// because in that case the reactor won't receive a wakeup.
185189
if p.park_timeout(Duration::from_secs(0)) {
186-
log::trace!("block_on: notified");
190+
tracing::trace!("notified");
187191
break;
188192
}
189193

190194
// Wait for I/O events.
191-
log::trace!("block_on: waiting on I/O");
195+
tracing::trace!("waiting on I/O");
192196
reactor_lock.react(None).ok();
193197

194198
// Check if a notification has been received.
195199
if p.park_timeout(Duration::from_secs(0)) {
196-
log::trace!("block_on: notified");
200+
tracing::trace!("notified");
197201
break;
198202
}
199203

200204
// Check if this thread been handling I/O events for a long time.
201205
if start.elapsed() > Duration::from_micros(500) {
202-
log::trace!("block_on: stops hogging the reactor");
206+
tracing::trace!("stops hogging the reactor");
203207

204208
// This thread is clearly processing I/O events for some other threads
205209
// because it didn't get a notification yet. It's best to stop hogging the
@@ -218,7 +222,7 @@ pub fn block_on<T>(future: impl Future<Output = T>) -> T {
218222
}
219223
} else {
220224
// Wait for an actual notification.
221-
log::trace!("block_on: sleep until notification");
225+
tracing::trace!("sleep until notification");
222226
p.park();
223227
}
224228
}

src/reactor.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ impl Reactor {
199199
///
200200
/// Returns the duration until the next timer before this method was called.
201201
fn process_timers(&self, wakers: &mut Vec<Waker>) -> Option<Duration> {
202+
let span = tracing::trace_span!("process_timers");
203+
let _enter = span.enter();
204+
202205
let mut timers = self.timers.lock().unwrap();
203206
self.process_timer_ops(&mut timers);
204207

@@ -227,7 +230,8 @@ impl Reactor {
227230
drop(timers);
228231

229232
// Add wakers to the list.
230-
log::trace!("process_timers: {} ready wakers", ready.len());
233+
tracing::trace!("{} ready wakers", ready.len());
234+
231235
for (_, waker) in ready {
232236
wakers.push(waker);
233237
}
@@ -262,6 +266,9 @@ pub(crate) struct ReactorLock<'a> {
262266
impl ReactorLock<'_> {
263267
/// Processes new events, blocking until the first event or the timeout.
264268
pub(crate) fn react(&mut self, timeout: Option<Duration>) -> io::Result<()> {
269+
let span = tracing::trace_span!("react");
270+
let _enter = span.enter();
271+
265272
let mut wakers = Vec::new();
266273

267274
// Process ready timers.
@@ -339,7 +346,7 @@ impl ReactorLock<'_> {
339346
};
340347

341348
// Wake up ready tasks.
342-
log::trace!("react: {} ready wakers", wakers.len());
349+
tracing::trace!("{} ready wakers", wakers.len());
343350
for waker in wakers {
344351
// Don't let a panicking waker blow everything up.
345352
panic::catch_unwind(|| waker.wake()).ok();
@@ -502,7 +509,7 @@ impl<T> Future for Readable<'_, T> {
502509

503510
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
504511
ready!(Pin::new(&mut self.0).poll(cx))?;
505-
log::trace!("readable: fd={:?}", self.0.handle.source.registration);
512+
tracing::trace!(fd = ?self.0.handle.source.registration, "readable");
506513
Poll::Ready(Ok(()))
507514
}
508515
}
@@ -522,7 +529,7 @@ impl<T> Future for ReadableOwned<T> {
522529

523530
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
524531
ready!(Pin::new(&mut self.0).poll(cx))?;
525-
log::trace!("readable_owned: fd={:?}", self.0.handle.source.registration);
532+
tracing::trace!(fd = ?self.0.handle.source.registration, "readable_owned");
526533
Poll::Ready(Ok(()))
527534
}
528535
}
@@ -542,7 +549,7 @@ impl<T> Future for Writable<'_, T> {
542549

543550
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
544551
ready!(Pin::new(&mut self.0).poll(cx))?;
545-
log::trace!("writable: fd={:?}", self.0.handle.source.registration);
552+
tracing::trace!(fd = ?self.0.handle.source.registration, "writable");
546553
Poll::Ready(Ok(()))
547554
}
548555
}
@@ -562,7 +569,7 @@ impl<T> Future for WritableOwned<T> {
562569

563570
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
564571
ready!(Pin::new(&mut self.0).poll(cx))?;
565-
log::trace!("writable_owned: fd={:?}", self.0.handle.source.registration);
572+
tracing::trace!(fd = ?self.0.handle.source.registration, "writable_owned");
566573
Poll::Ready(Ok(()))
567574
}
568575
}

0 commit comments

Comments
 (0)