Skip to content

Commit

Permalink
- remove default trace level and make log lvl mandatory on span! macro
Browse files Browse the repository at this point in the history
- add trace_span!, debug_span!, info_span!, warn_span! and error_span!
macros that behave as span! macro, but with defined log levels
  • Loading branch information
jxs committed Mar 31, 2019
1 parent cb91dd2 commit 0e92f19
Show file tree
Hide file tree
Showing 12 changed files with 773 additions and 171 deletions.
3 changes: 2 additions & 1 deletion tokio-trace/benches/no_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use test::Bencher;
#[bench]
fn bench_span_no_subscriber(b: &mut Bencher) {
b.iter(|| {
span!("span");
span!(tokio_trace::Level::TRACE, "span");
});
}

Expand All @@ -24,6 +24,7 @@ fn bench_log_no_logger(b: &mut Bencher) {
fn bench_costly_field_no_subscriber(b: &mut Bencher) {
b.iter(|| {
span!(
tokio_trace::Level::TRACE,
"span",
foo = tokio_trace::field::display(format!("bar {:?}", 2))
);
Expand Down
10 changes: 7 additions & 3 deletions tokio-trace/benches/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,23 @@ const N_SPANS: usize = 100;

#[bench]
fn span_no_fields(b: &mut Bencher) {
tokio_trace::subscriber::with_default(EnabledSubscriber, || b.iter(|| span!("span")));
tokio_trace::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| span!(tokio_trace::Level::TRACE, "span"))
});
}

#[bench]
fn enter_span(b: &mut Bencher) {
tokio_trace::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| test::black_box(span!("span").enter(|| {})))
b.iter(|| test::black_box(span!(tokio_trace::Level::TRACE, "span").enter(|| {})))
});
}

#[bench]
fn span_repeatedly(b: &mut Bencher) {
#[inline]
fn mk_span(i: u64) -> tokio_trace::Span {
span!("span", i = i)
span!(tokio_trace::Level::TRACE, "span", i = i)
}

let n = test::black_box(N_SPANS);
Expand All @@ -125,6 +127,7 @@ fn span_with_fields(b: &mut Bencher) {
tokio_trace::subscriber::with_default(EnabledSubscriber, || {
b.iter(|| {
span!(
tokio_trace::Level::TRACE,
"span",
foo = "foo",
bar = "bar",
Expand All @@ -141,6 +144,7 @@ fn span_with_fields_record(b: &mut Bencher) {
tokio_trace::subscriber::with_default(subscriber, || {
b.iter(|| {
span!(
tokio_trace::Level::TRACE,
"span",
foo = "foo",
bar = "bar",
Expand Down
10 changes: 8 additions & 2 deletions tokio-trace/examples/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ fn main() {

tokio_trace::subscriber::with_default(subscriber, || {
let mut foo: u64 = 2;
span!("my_great_span", foo_count = &foo).enter(|| {
span!(tokio_trace::Level::TRACE, "my_great_span", foo_count = &foo).enter(|| {
foo += 1;
info!({ yak_shaved = true, yak_count = 1 }, "hi from inside my span");
span!("my other span", foo_count = &foo, baz_count = 5).enter(|| {
span!(
tokio_trace::Level::TRACE,
"my other span",
foo_count = &foo,
baz_count = 5
)
.enter(|| {
warn!({ yak_shaved = false, yak_count = -1 }, "failed to shave yak");
});
});
Expand Down
29 changes: 25 additions & 4 deletions tokio-trace/examples/sloggish/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,37 @@ fn main() {
let subscriber = SloggishSubscriber::new(2);

tokio_trace::subscriber::with_default(subscriber, || {
span!("", version = &field::display(5.0)).enter(|| {
span!("server", host = "localhost", port = 8080).enter(|| {
span!(
tokio_trace::Level::TRACE,
"",
version = &field::display(5.0)
)
.enter(|| {
span!(
tokio_trace::Level::TRACE,
"server",
host = "localhost",
port = 8080
)
.enter(|| {
info!("starting");
info!("listening");
let mut peer1 = span!("conn", peer_addr = "82.9.9.9", port = 42381);
let mut peer1 = span!(
tokio_trace::Level::TRACE,
"conn",
peer_addr = "82.9.9.9",
port = 42381
);
peer1.enter(|| {
debug!("connected");
debug!({ length = 2 }, "message received");
});
let mut peer2 = span!("conn", peer_addr = "8.8.8.8", port = 18230);
let mut peer2 = span!(
tokio_trace::Level::TRACE,
"conn",
peer_addr = "8.8.8.8",
port = 18230
);
peer2.enter(|| {
debug!("connected");
});
Expand Down
10 changes: 5 additions & 5 deletions tokio-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
//! # #[macro_use] extern crate tokio_trace;
//! # fn main() {
//! # let n = 1;
//! span!("my loop").enter(|| {
//! span!(tokio_trace::Level::TRACE, "my loop").enter(|| {
//! for i in 0..n {
//! # let _ = i;
//! // ...
Expand All @@ -73,7 +73,7 @@
//! # let n = 1u64;
//! for i in 0..n {
//! # let _ = i;
//! span!("my loop", iteration = i).enter(|| {
//! span!(tokio_trace::Level::TRACE, "my loop", iteration = i).enter(|| {
//! // ...
//! })
//! }
Expand Down Expand Up @@ -155,8 +155,8 @@
//! # #[macro_use]
//! # extern crate tokio_trace;
//! # fn main() {
//! // Construct a new span named "my span".
//! let mut span = span!("my span");
//! // Construct a new span named "my span" with trace log level.
//! let mut span = span!(tokio_trace::Level::TRACE, "my span");
//! span.enter(|| {
//! // Any trace events in this closure or code called by it will occur within
//! // the span.
Expand Down Expand Up @@ -196,7 +196,7 @@
//! pub fn shave_the_yak(yak: &mut Yak) {
//! // Create a new span for this invocation of `shave_the_yak`, annotated
//! // with the yak being shaved as a *field* on the span.
//! span!("shave_the_yak", yak = field::debug(&yak)).enter(|| {
//! span!(tokio_trace::Level::TRACE, "shave_the_yak", yak = field::debug(&yak)).enter(|| {
//! // Since the span is annotated with the yak, it is part of the context
//! // for everything happening inside the span. Therefore, we don't need
//! // to add it to the message for this event, as the `log` crate does.
Expand Down
Loading

0 comments on commit 0e92f19

Please sign in to comment.