From 4b163eb18a4075792ccbe9d39fcd7dab3c13a2ab Mon Sep 17 00:00:00 2001 From: Kevin Leimkuhler Date: Tue, 12 Feb 2019 14:56:22 -0800 Subject: [PATCH] tokio-trace: Extend `span!` rules (#893) * tokio-trace: Extend `span!` rules Add the ability to define a target and log level for individual spans. `level: ...` must be included since both `$lvl` and `$name` are `expr`. The macro tests and documentation tests have been extended. Signed-off-by: Kevin Leimkuhler * Add additional `span!` test Change default log level of `span!` to `TRACE` to reflect the default level used in `callsite!`. Signed-off-by: Kevin Leimkuhler --- tokio-trace/src/lib.rs | 54 ++++++++++++++++++++++++++++++------- tokio-trace/tests/macros.rs | 16 +++++++++++ tokio-trace/tests/span.rs | 20 ++++++++++++++ 3 files changed, 81 insertions(+), 9 deletions(-) diff --git a/tokio-trace/src/lib.rs b/tokio-trace/src/lib.rs index 856390e31d7..631d0e3b678 100644 --- a/tokio-trace/src/lib.rs +++ b/tokio-trace/src/lib.rs @@ -434,7 +434,7 @@ macro_rules! callsite { /// # } /// ``` /// -/// Note that a trailing comma on the final field is valid. +/// Note that a trailing comma on the final field is valid: /// ``` /// # #[macro_use] /// # extern crate tokio_trace; @@ -443,9 +443,22 @@ macro_rules! callsite { /// "my span", /// foo = 2, /// bar = "a string", -/// ).enter(|| { -/// // do work inside the span... -/// }); +/// ); +/// # } +/// ``` +/// +/// Creating a span with custom target and log level: +/// ``` +/// # #[macro_use] +/// # extern crate tokio_trace; +/// # fn main() { +/// span!( +/// target: "app_span", +/// level: tokio_trace::Level::TRACE, +/// "my span", +/// foo = 3, +/// bar = "another string" +/// ); /// # } /// ``` /// @@ -475,15 +488,19 @@ macro_rules! callsite { /// ``` #[macro_export] macro_rules! span { - ($name:expr) => { span!($name,) }; - ($name:expr, $($k:ident $( = $val:expr )* ),*,) => { - span!($name, $($k $( = $val)* ),*) + (target: $target:expr, level: $lvl:expr, $name:expr, $($k:ident $( = $val:expr )* ),*,) => { + span!(target: $target, level: $lvl, $name, $($k $( = $val)*),*) }; - ($name:expr, $($k:ident $( = $val:expr )* ),*) => { + (target: $target:expr, level: $lvl:expr, $name:expr, $($k:ident $( = $val:expr )* ),*) => { { use $crate::{callsite, field::{Value, ValueSet, AsField}, Span}; use $crate::callsite::Callsite; - let callsite = callsite! { name: $name, fields: $( $k ),* }; + let callsite = callsite! { + name: $name, + target: $target, + level: $lvl, + fields: $($k),* + }; if is_enabled!(callsite) { let meta = callsite.metadata(); Span::new(meta, &valueset!(meta.fields(), $($k $( = $val)*),*)) @@ -492,6 +509,25 @@ macro_rules! span { } } }; + (target: $target:expr, level: $lvl:expr, $name:expr) => { + span!(target: $target, level: $lvl, $name,) + }; + (level: $lvl:expr, $name:expr, $($k:ident $( = $val:expr )* ),*,) => { + span!(target: module_path!(), level: $lvl, $name, $($k $( = $val)*),*) + }; + (level: $lvl:expr, $name:expr, $($k:ident $( = $val:expr )* ),*) => { + span!(target: module_path!(), level: $lvl, $name, $($k $( = $val)*),*) + }; + (level: $lvl:expr, $name:expr) => { + span!(target: module_path!(), level: $lvl, $name,) + }; + ($name:expr, $($k:ident $( = $val:expr)*),*,) => { + span!(target: module_path!(), level: $crate::Level::TRACE, $name, $($k $( = $val)*),*) + }; + ($name:expr, $($k:ident $( = $val:expr)*),*) => { + span!(target: module_path!(), level: $crate::Level::TRACE, $name, $($k $( = $val)*),*) + }; + ($name:expr) => { span!(target: module_path!(), level: $crate::Level::TRACE, $name,) }; } /// Constructs a new `Event`. diff --git a/tokio-trace/tests/macros.rs b/tokio-trace/tests/macros.rs index 38acd143631..002b787c68e 100644 --- a/tokio-trace/tests/macros.rs +++ b/tokio-trace/tests/macros.rs @@ -6,6 +6,22 @@ extern crate tokio_trace; // they're compile-time tests, so I want to get line numbers etc out of // failures, and producing them with a macro would muddy the waters a bit. +#[test] +fn span() { + span!(target: "foo_events", level: tokio_trace::Level::DEBUG, "foo", bar = 2, baz = 3); + span!(target: "foo_events", level: tokio_trace::Level::DEBUG, "foo", bar = 2, baz = 4,); + span!(target: "foo_events", level: tokio_trace::Level::DEBUG, "foo"); + span!(target: "foo_events", level: tokio_trace::Level::DEBUG, "bar",); + span!(level: tokio_trace::Level::DEBUG, "foo", bar = 2, baz = 3); + span!(level: tokio_trace::Level::DEBUG, "foo", bar = 2, baz = 4,); + span!(level: tokio_trace::Level::DEBUG, "foo"); + span!(level: tokio_trace::Level::DEBUG, "bar",); + span!("foo", bar = 2, baz = 3); + span!("foo", bar = 2, baz = 4,); + span!("foo"); + span!("bar",); +} + #[test] fn event() { event!(tokio_trace::Level::DEBUG, foo = 3, bar = 2, baz = false); diff --git a/tokio-trace/tests/span.rs b/tokio-trace/tests/span.rs index f7764cf9f2d..45c192982bf 100644 --- a/tokio-trace/tests/span.rs +++ b/tokio-trace/tests/span.rs @@ -7,6 +7,7 @@ use std::thread; use tokio_trace::{ dispatcher, field::{debug, display}, + subscriber::with_default, Dispatch, Level, Span, }; @@ -456,3 +457,22 @@ fn add_fields_only_after_new_span() { handle.assert_finished(); } + +#[test] +fn new_span_with_target_and_log_level() { + let (subscriber, handle) = subscriber::mock() + .new_span( + span::mock() + .named("foo") + .with_target("app_span") + .at_level(tokio_trace::Level::DEBUG), + ) + .done() + .run_with_handle(); + + with_default(subscriber, || { + span!(target: "app_span", level: tokio_trace::Level::DEBUG, "foo"); + }); + + handle.assert_finished(); +}