mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +02:00
trace: Add static level filtering (#987)
## Motivation `tokio-trace` should have static verbosity level filtering, like the `log` crate. The static max verbosity level should be controlled at compile time with a set of features. It should be possible to set a separate max level for release and debug mode builds. ## Solution We can do this fairly similarly to how the `log` crate does it: `tokio-trace` should export a constant whose value is set based on the static max level feature flags. Then, we add an if statement to the `span!` and `event!` macros which tests if that event or span's level is enabled. Closes #959
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#[macro_use]
|
||||
extern crate tokio_trace;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio_trace::span::{Attributes, Record};
|
||||
use tokio_trace::{span, Event, Id, Level, Metadata, Subscriber};
|
||||
|
||||
struct State {
|
||||
last_level: Mutex<Option<Level>>,
|
||||
}
|
||||
|
||||
struct TestSubscriber(Arc<State>);
|
||||
|
||||
impl Subscriber for TestSubscriber {
|
||||
fn enabled(&self, _: &Metadata) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn new_span(&self, _span: &Attributes) -> Id {
|
||||
span::Id::from_u64(42)
|
||||
}
|
||||
|
||||
fn record(&self, _span: &Id, _values: &Record) {}
|
||||
|
||||
fn record_follows_from(&self, _span: &Id, _follows: &Id) {}
|
||||
|
||||
fn event(&self, event: &Event) {
|
||||
*self.0.last_level.lock().unwrap() = Some(event.metadata().level().clone());
|
||||
}
|
||||
|
||||
fn enter(&self, _span: &Id) {}
|
||||
|
||||
fn exit(&self, _span: &Id) {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn test_static_max_level_features() {
|
||||
let me = Arc::new(State {
|
||||
last_level: Mutex::new(None),
|
||||
});
|
||||
let a = me.clone();
|
||||
tokio_trace::subscriber::with_default(TestSubscriber(me), || {
|
||||
error!("");
|
||||
last(&a, Some(Level::ERROR));
|
||||
warn!("");
|
||||
last(&a, Some(Level::WARN));
|
||||
info!("");
|
||||
last(&a, Some(Level::INFO));
|
||||
debug!("");
|
||||
last(&a, Some(Level::DEBUG));
|
||||
trace!("");
|
||||
last(&a, None);
|
||||
|
||||
span!(level: Level::ERROR, "");
|
||||
last(&a, None);
|
||||
span!(level: Level::WARN, "");
|
||||
last(&a, None);
|
||||
span!(level: Level::INFO, "");
|
||||
last(&a, None);
|
||||
span!(level: Level::DEBUG, "");
|
||||
last(&a, None);
|
||||
span!(level: Level::TRACE, "");
|
||||
last(&a, None);
|
||||
});
|
||||
}
|
||||
|
||||
fn last(state: &State, expected: Option<Level>) {
|
||||
let mut lvl = state.last_level.lock().unwrap();
|
||||
assert_eq!(*lvl, expected);
|
||||
*lvl = None;
|
||||
}
|
||||
Reference in New Issue
Block a user