Skip to content

Commit 85466be

Browse files
committed
implement fully_enabled
1 parent 2c3555b commit 85466be

File tree

8 files changed

+144
-27
lines changed

8 files changed

+144
-27
lines changed

tracing-core/src/dispatcher.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,12 @@ impl Dispatch {
504504
self.subscriber.enabled(metadata)
505505
}
506506

507+
#[inline]
508+
#[doc(hidden)]
509+
pub fn fully_enabled(&self, metadata: &Metadata<'_>) -> bool {
510+
self.subscriber.fully_enabled(metadata)
511+
}
512+
507513
/// Records that an [`Event`] has occurred.
508514
///
509515
/// This calls the [`event`] function on the [`Subscriber`] that this

tracing-core/src/subscriber.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ pub trait Subscriber: 'static {
171171
/// [`register_callsite`]: #method.register_callsite
172172
fn enabled(&self, metadata: &Metadata<'_>) -> bool;
173173

174+
#[doc(hidden)]
175+
fn fully_enabled(&self, metadata: &Metadata<'_>) -> bool {
176+
self.enabled(metadata)
177+
}
178+
174179
/// Returns the highest [verbosity level][level] that this `Subscriber` will
175180
/// enable, or `None`, if the subscriber does not implement level-based
176181
/// filtering or chooses not to implement this method.
@@ -606,6 +611,11 @@ impl Subscriber for Box<dyn Subscriber + Send + Sync + 'static> {
606611
self.as_ref().enabled(metadata)
607612
}
608613

614+
#[inline]
615+
fn fully_enabled(&self, metadata: &Metadata<'_>) -> bool {
616+
self.as_ref().fully_enabled(metadata)
617+
}
618+
609619
#[inline]
610620
fn max_level_hint(&self) -> Option<LevelFilter> {
611621
self.as_ref().max_level_hint()
@@ -683,6 +693,11 @@ impl Subscriber for Arc<dyn Subscriber + Send + Sync + 'static> {
683693
self.as_ref().enabled(metadata)
684694
}
685695

696+
#[inline]
697+
fn fully_enabled(&self, metadata: &Metadata<'_>) -> bool {
698+
self.as_ref().fully_enabled(metadata)
699+
}
700+
686701
#[inline]
687702
fn max_level_hint(&self) -> Option<LevelFilter> {
688703
self.as_ref().max_level_hint()

tracing-subscriber/src/filter/layer_filters/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,22 @@ where
543543
}
544544
}
545545

546+
fn fully_enabled(&self, metadata: &Metadata<'_>, cx: Context<'_, S>) -> bool {
547+
let cx = cx.with_filter(self.id());
548+
let enabled = self.filter.enabled(metadata, &cx);
549+
FILTERING.with(|filtering| filtering.set(self.id(), enabled));
550+
551+
println!("GUS: {}", enabled);
552+
if enabled {
553+
// If the filter enabled this metadata, ask the wrapped layer if
554+
// _it_ wants it --- it might have a global filter.
555+
self.layer.fully_enabled(metadata, cx)
556+
} else {
557+
// ORDERED
558+
false
559+
}
560+
}
561+
546562
fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, cx: Context<'_, S>) {
547563
self.did_enable(|| {
548564
self.layer.on_new_span(attrs, id, cx.with_filter(self.id()));

tracing-subscriber/src/layer/layered.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ where
9191
}
9292
}
9393

94+
#[doc(hidden)]
95+
fn fully_enabled(&self, metadata: &Metadata<'_>) -> bool {
96+
println!("Layered::Subscriber::fully_enabled");
97+
if self.layer.fully_enabled(metadata, self.ctx()) {
98+
// if the outer layer enables the callsite metadata, ask the subscriber.
99+
self.inner.fully_enabled(metadata)
100+
} else {
101+
// otherwise, the callsite is disabled by the layer
102+
103+
// If per-layer filters are in use, and we are short-circuiting
104+
// (rather than calling into the inner type), clear the current
105+
// per-layer filter `enabled` state.
106+
#[cfg(feature = "registry")]
107+
filter::FilterState::clear_enabled();
108+
109+
false
110+
}
111+
}
112+
94113
fn max_level_hint(&self) -> Option<LevelFilter> {
95114
self.pick_level_hint(self.layer.max_level_hint(), self.inner.max_level_hint())
96115
}
@@ -228,6 +247,18 @@ where
228247
}
229248
}
230249

250+
#[doc(hidden)]
251+
fn fully_enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool {
252+
println!("Layered::Layer::fully_enabled");
253+
if self.layer.fully_enabled(metadata, ctx.clone()) {
254+
// if the outer subscriber enables the callsite metadata, ask the inner layer.
255+
self.inner.fully_enabled(metadata, ctx)
256+
} else {
257+
// otherwise, the callsite is disabled by this layer
258+
false
259+
}
260+
}
261+
231262
fn max_level_hint(&self) -> Option<LevelFilter> {
232263
self.pick_level_hint(self.layer.max_level_hint(), self.inner.max_level_hint())
233264
}

tracing-subscriber/src/layer/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,11 @@ where
648648
true
649649
}
650650

651+
#[doc(hidden)]
652+
fn fully_enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool {
653+
self.enabled(metadata, ctx)
654+
}
655+
651656
/// Notifies this layer that a new span was constructed with the given
652657
/// `Attributes` and `Id`.
653658
fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) {

tracing-subscriber/tests/layer_filters/combinators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use tracing_subscriber::{
3-
filter::{filter_fn, FilterExt, LevelFilter},
3+
filter::{FilterExt, LevelFilter},
44
prelude::*,
55
};
66

tracing-subscriber/tests/layer_filters/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
mod support;
44
use self::support::*;
55
mod boxed;
6+
mod combinators;
67
mod downcast_raw;
78
mod filter_scopes;
9+
mod fully_enabled;
810
mod targets;
911
mod trees;
1012

tracing/src/macros.rs

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -868,32 +868,9 @@ macro_rules! event {
868868
///
869869
#[macro_export]
870870
macro_rules! enabled {
871-
(target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> ({
872-
if $crate::level_enabled!($lvl) {
873-
use $crate::__macro_support::Callsite as _;
874-
static CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! {
875-
name: concat!(
876-
"enabled ",
877-
file!(),
878-
":",
879-
line!()
880-
),
881-
kind: $crate::metadata::Kind::HINT,
882-
target: $target,
883-
level: $lvl,
884-
fields: $($fields)*
885-
};
886-
let interest = CALLSITE.interest();
887-
if !interest.is_never() && CALLSITE.is_enabled(interest) {
888-
let meta = CALLSITE.metadata();
889-
$crate::dispatcher::get_default(|current| current.enabled(meta))
890-
} else {
891-
false
892-
}
893-
} else {
894-
false
895-
}
896-
});
871+
(target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> (
872+
$crate::_enabled!(enabled, target: $target, $lvl, { $($fields:tt)* } )
873+
);
897874
// Just target and level
898875
(target: $target:expr, $lvl:expr ) => (
899876
$crate::enabled!(target: $target, $lvl, { })
@@ -921,6 +898,71 @@ macro_rules! enabled {
921898
);
922899
}
923900

901+
/// TODO
902+
#[macro_export]
903+
macro_rules! fully_enabled {
904+
(target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> (
905+
$crate::_enabled!(fully_enabled, target: $target, $lvl, { $($fields:tt)* } )
906+
);
907+
// Just target and level
908+
(target: $target:expr, $lvl:expr ) => (
909+
$crate::fully_enabled!(target: $target, $lvl, { })
910+
);
911+
912+
// These two cases handle fields with no values
913+
(target: $target:expr, $lvl:expr, $($field:tt)*) => (
914+
$crate::fully_enabled!(
915+
target: $target,
916+
$lvl,
917+
{ $($field)*}
918+
)
919+
);
920+
($lvl:expr, $($field:tt)*) => (
921+
$crate::fully_enabled!(
922+
target: module_path!(),
923+
$lvl,
924+
{ $($field)*}
925+
)
926+
);
927+
928+
// Simplest `enabled!` case
929+
( $lvl:expr ) => (
930+
$crate::fully_enabled!(target: module_path!(), $lvl, { })
931+
);
932+
}
933+
934+
/// TODO
935+
#[doc(hidden)]
936+
#[macro_export]
937+
macro_rules! _enabled {
938+
($style:tt, target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> ({
939+
if $crate::level_enabled!($lvl) {
940+
use $crate::__macro_support::Callsite as _;
941+
static CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! {
942+
name: concat!(
943+
"enabled ",
944+
file!(),
945+
":",
946+
line!()
947+
),
948+
kind: $crate::metadata::Kind::HINT,
949+
target: $target,
950+
level: $lvl,
951+
fields: $($fields)*
952+
};
953+
let interest = CALLSITE.interest();
954+
if !interest.is_never() && CALLSITE.is_enabled(interest) {
955+
let meta = CALLSITE.metadata();
956+
$crate::dispatcher::get_default(|current| current.$style(meta))
957+
} else {
958+
false
959+
}
960+
} else {
961+
false
962+
}
963+
});
964+
}
965+
924966
/// Constructs an event at the trace level.
925967
///
926968
/// This functions similarly to the [`event!`] macro. See [the top-level

0 commit comments

Comments
 (0)