Skip to content

Commit a367813

Browse files
guswynnhawkwdavidbarsky
authored andcommitted
tracing: add enabled! macro (tokio-rs#1883)
Backports tokio-rs#1821 to `v0.1.x`. ## Motivation Closes: tokio-rs#1668 My usecase is different than the referenced "avoid doing something expensive to log": I want to guard turning on `debug` mode for an ffi'd library, based on some `target` that represents the "module" we care about. ## Solution The macro is very similar to `event!`, but adds a few simplistic cases, and generates ever so slightly different code (to return the correct value always. It also skips anything to do with `tracing-log`. I considered (and tried), to share the impl between `event!` and `enabled!`, but must confess I am not good at macros and got stuck. I think they are sufficiently different, where copied impls, is easier to read. We already manage Co-authored-by: Eliza Weisman <[email protected]> Co-authored-by: David Barsky <[email protected]>
1 parent 17c6502 commit a367813

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

tracing-core/src/metadata.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ impl Kind {
382382
/// full event with this [`Metadata`].
383383
pub const HINT: Kind = Kind(Self::HINT_BIT);
384384

385+
/// `enabled!` callsite. [`Subscriber`][`crate::subscriber::Subscriber`]s can assume
386+
/// this `Kind` means they will never recieve a
387+
/// full event with this [`Metadata`].
388+
pub const HINT: Kind = Kind(KindInner::Hint);
389+
385390
/// Return true if the callsite kind is `Span`
386391
pub fn is_span(&self) -> bool {
387392
self.0 & Self::SPAN_BIT == Self::SPAN_BIT
@@ -495,6 +500,11 @@ impl<'a> PartialEq for Metadata<'a> {
495500
&& lhs_kind == rhs_kind
496501
}
497502
}
503+
504+
/// Return true if the callsite kind is `Hint`
505+
pub fn is_hint(&self) -> bool {
506+
matches!(self, Kind(KindInner::Hint))
507+
}
498508
}
499509

500510
// ===== impl Level =====

0 commit comments

Comments
 (0)