From cde6396a6c09be7d73905b08314a915d44a2181c Mon Sep 17 00:00:00 2001 From: Zicklag Date: Thu, 11 Mar 2021 12:45:24 -0600 Subject: [PATCH] subscriber: change `FmtSpan` to a combinable bitflag (#1277) This backports #1277 from `master`. Fixes #1136. Allows arbitrarily combining different FmtSpan events to listen to. Motivation ---------- The idea is to allow any combination of `FmtSpan` options, such as the currently unachievable combination of `FmtSpan::NEW | FmtSpan::CLOSE`. Solution -------- Make `FmtSpan` behave like a bitflag that can be combined using the bitwise or operator ( `|` ) while maintaining backward compatibility by keeping the same names for all existing presets and keeping the implementation details hidden. --- tracing-subscriber/src/fmt/fmt_layer.rs | 27 +++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/tracing-subscriber/src/fmt/fmt_layer.rs b/tracing-subscriber/src/fmt/fmt_layer.rs index d350e8150d..e2040b4a91 100644 --- a/tracing-subscriber/src/fmt/fmt_layer.rs +++ b/tracing-subscriber/src/fmt/fmt_layer.rs @@ -259,18 +259,33 @@ where /// - `FmtSpan::NONE`: No events will be synthesized when spans are /// created, entered, exited, or closed. Data from spans will still be /// included as the context for formatted events. This is the default. - /// - `FmtSpan::ACTIVE`: Events will be synthesized when spans are entered - /// or exited. + /// - `FmtSpan::NEW`: An event will be synthesized when spans are created. + /// - `FmtSpan::ENTER`: An event will be synthesized when spans are entered. + /// - `FmtSpan::EXIT`: An event will be synthesized when spans are exited. /// - `FmtSpan::CLOSE`: An event will be synthesized when a span closes. If /// [timestamps are enabled][time] for this formatter, the generated /// event will contain fields with the span's _busy time_ (the total /// time for which it was entered) and _idle time_ (the total time that /// the span existed but was not entered). + /// - `FmtSpan::ACTIVE`: Events will be synthesized when spans are entered + /// or exited. /// - `FmtSpan::FULL`: Events will be synthesized whenever a span is /// created, entered, exited, or closed. If timestamps are enabled, the /// close event will contain the span's busy and idle time, as /// described above. /// + /// The options can be enabled in any combination. For instance, the following + /// will synthesize events whenever spans are created and closed: + /// + /// ```rust + /// use tracing_subscriber::fmt; + /// use tracing_subscriber::fmt::format::FmtSpan; + /// + /// let subscriber = fmt() + /// .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) + /// .finish(); + /// ``` + /// /// Note that the generated events will only be part of the log output by /// this formatter; they will not be recorded by other `Subscriber`s or by /// `Layer`s added to this subscriber. @@ -627,7 +642,7 @@ where } fn on_enter(&self, id: &Id, ctx: Context<'_, S>) { - if self.fmt_span.trace_active() || self.fmt_span.trace_close() && self.fmt_span.fmt_timing { + if self.fmt_span.trace_enter() || self.fmt_span.trace_close() && self.fmt_span.fmt_timing { let span = ctx.span(id).expect("Span not found, this is a bug"); let mut extensions = span.extensions_mut(); if let Some(timings) = extensions.get_mut::() { @@ -636,7 +651,7 @@ where timings.last = now; } - if self.fmt_span.trace_active() { + if self.fmt_span.trace_enter() { with_event_from_span!(id, span, "message" = "enter", |event| { drop(extensions); drop(span); @@ -647,7 +662,7 @@ where } fn on_exit(&self, id: &Id, ctx: Context<'_, S>) { - if self.fmt_span.trace_active() || self.fmt_span.trace_close() && self.fmt_span.fmt_timing { + if self.fmt_span.trace_exit() || self.fmt_span.trace_close() && self.fmt_span.fmt_timing { let span = ctx.span(id).expect("Span not found, this is a bug"); let mut extensions = span.extensions_mut(); if let Some(timings) = extensions.get_mut::() { @@ -656,7 +671,7 @@ where timings.last = now; } - if self.fmt_span.trace_active() { + if self.fmt_span.trace_exit() { with_event_from_span!(id, span, "message" = "exit", |event| { drop(extensions); drop(span);