|
33 | 33 | //! debugging, you can make changes inside those crates and quickly run main.rs |
34 | 34 | //! to read the debug logs. |
35 | 35 |
|
| 36 | +use std::cell::Cell; |
36 | 37 | use std::env::{self, VarError}; |
37 | 38 | use std::fmt::{self, Display}; |
38 | 39 | use std::fs::File; |
39 | 40 | use std::io::{self, IsTerminal}; |
40 | 41 | use std::sync::Mutex; |
41 | 42 |
|
42 | 43 | use tracing::dispatcher::SetGlobalDefaultError; |
43 | | -use tracing::{Event, Subscriber}; |
| 44 | +use tracing::{Event, Subscriber, span}; |
44 | 45 | use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter}; |
45 | 46 | use tracing_subscriber::fmt::FmtContext; |
46 | 47 | use tracing_subscriber::fmt::format::{self, FmtSpan, FormatEvent, FormatFields}; |
47 | 48 | use tracing_subscriber::fmt::writer::BoxMakeWriter; |
48 | | -use tracing_subscriber::layer::SubscriberExt; |
| 49 | +use tracing_subscriber::layer::{Context, SubscriberExt}; |
49 | 50 | use tracing_subscriber::{Layer, Registry}; |
50 | 51 | // Re-export tracing |
51 | 52 | pub use {tracing, tracing_core, tracing_subscriber}; |
@@ -175,6 +176,7 @@ where |
175 | 176 | .with_thread_ids(verbose_thread_ids) |
176 | 177 | .with_thread_names(verbose_thread_ids) |
177 | 178 | .with_span_events(FmtSpan::ACTIVE); |
| 179 | + let fmt_layer = NonrecursiveLayer { inner: fmt_layer }; |
178 | 180 | Layer::boxed(fmt_layer) |
179 | 181 | } else { |
180 | 182 | let mut layer = tracing_tree::HierarchicalLayer::default() |
@@ -286,3 +288,105 @@ impl From<SetGlobalDefaultError> for Error { |
286 | 288 | Error::AlreadyInit(tracing_error) |
287 | 289 | } |
288 | 290 | } |
| 291 | + |
| 292 | +thread_local! { |
| 293 | + static NONRECURSIVE_GUARD_LOCK: Cell<bool> = const { Cell::new(false) }; |
| 294 | +} |
| 295 | + |
| 296 | +struct NonrecursiveGuard; |
| 297 | + |
| 298 | +impl NonrecursiveGuard { |
| 299 | + fn lock() -> Option<NonrecursiveGuard> { |
| 300 | + if !NONRECURSIVE_GUARD_LOCK.replace(true) { Some(NonrecursiveGuard) } else { None } |
| 301 | + } |
| 302 | +} |
| 303 | + |
| 304 | +impl Drop for NonrecursiveGuard { |
| 305 | + fn drop(&mut self) { |
| 306 | + NONRECURSIVE_GUARD_LOCK.set(false); |
| 307 | + } |
| 308 | +} |
| 309 | + |
| 310 | +/// Many debug messages that rustc emits produce additional debug messages when formatting the |
| 311 | +/// arguments to the original debug message. [`tracing_tree::HierarchicalLayer`] (used by the |
| 312 | +/// default output format) filters these out, but [`tracing_subscriber::fmt::format::Json`] (used by |
| 313 | +/// `RUSTC_LOG_FORMAT_JSON`) does not. So, implement a simple recursion check to filter these |
| 314 | +/// messages out. |
| 315 | +struct NonrecursiveLayer<S> { |
| 316 | + inner: S, |
| 317 | +} |
| 318 | + |
| 319 | +impl<S: Subscriber, L: Layer<S>> Layer<S> for NonrecursiveLayer<L> { |
| 320 | + fn on_register_dispatch(&self, subscriber: &tracing::Dispatch) { |
| 321 | + self.inner.on_register_dispatch(subscriber) |
| 322 | + } |
| 323 | + |
| 324 | + fn on_layer(&mut self, subscriber: &mut S) { |
| 325 | + self.inner.on_layer(subscriber) |
| 326 | + } |
| 327 | + |
| 328 | + fn register_callsite( |
| 329 | + &self, |
| 330 | + metadata: &'static tracing::Metadata<'static>, |
| 331 | + ) -> tracing_core::Interest { |
| 332 | + self.inner.register_callsite(metadata) |
| 333 | + } |
| 334 | + |
| 335 | + fn enabled(&self, metadata: &tracing::Metadata<'_>, ctx: Context<'_, S>) -> bool { |
| 336 | + self.inner.enabled(metadata, ctx) |
| 337 | + } |
| 338 | + |
| 339 | + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { |
| 340 | + if let Some(_) = NonrecursiveGuard::lock() { |
| 341 | + self.inner.on_new_span(attrs, id, ctx) |
| 342 | + } |
| 343 | + } |
| 344 | + |
| 345 | + fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: Context<'_, S>) { |
| 346 | + if let Some(_) = NonrecursiveGuard::lock() { |
| 347 | + self.inner.on_record(span, values, ctx) |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + fn on_follows_from(&self, span: &span::Id, follows: &span::Id, ctx: Context<'_, S>) { |
| 352 | + if let Some(_) = NonrecursiveGuard::lock() { |
| 353 | + self.inner.on_follows_from(span, follows, ctx) |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | + fn event_enabled(&self, event: &Event<'_>, ctx: Context<'_, S>) -> bool { |
| 358 | + if let Some(_) = NonrecursiveGuard::lock() { |
| 359 | + self.inner.event_enabled(event, ctx) |
| 360 | + } else { |
| 361 | + false |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) { |
| 366 | + if let Some(_) = NonrecursiveGuard::lock() { |
| 367 | + self.inner.on_event(event, ctx) |
| 368 | + } |
| 369 | + } |
| 370 | + |
| 371 | + fn on_enter(&self, id: &span::Id, ctx: Context<'_, S>) { |
| 372 | + if let Some(_) = NonrecursiveGuard::lock() { |
| 373 | + self.inner.on_enter(id, ctx) |
| 374 | + } |
| 375 | + } |
| 376 | + |
| 377 | + fn on_exit(&self, id: &span::Id, ctx: Context<'_, S>) { |
| 378 | + if let Some(_) = NonrecursiveGuard::lock() { |
| 379 | + self.inner.on_exit(id, ctx) |
| 380 | + } |
| 381 | + } |
| 382 | + |
| 383 | + fn on_close(&self, id: span::Id, ctx: Context<'_, S>) { |
| 384 | + if let Some(_) = NonrecursiveGuard::lock() { |
| 385 | + self.inner.on_close(id, ctx) |
| 386 | + } |
| 387 | + } |
| 388 | + |
| 389 | + fn on_id_change(&self, old: &span::Id, new: &span::Id, ctx: Context<'_, S>) { |
| 390 | + self.inner.on_id_change(old, new, ctx) |
| 391 | + } |
| 392 | +} |
0 commit comments