From 0ed741ff7d7b812e892ff2c0ae69fd004edf94aa Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Tue, 22 Jan 2019 10:49:52 -0800 Subject: [PATCH 1/4] Rename proc-macros crate to `tokio-trace-derive` This is so we can also have a `tokio-trace-macros` crate containing macro-rules macros. Signed-off-by: Eliza Weisman --- Cargo.toml | 1 + {tokio-trace-macros => tokio-trace-derive}/Cargo.toml | 2 +- {tokio-trace-macros => tokio-trace-derive}/examples/basic.rs | 2 +- {tokio-trace-macros => tokio-trace-derive}/src/lib.rs | 0 4 files changed, 3 insertions(+), 2 deletions(-) rename {tokio-trace-macros => tokio-trace-derive}/Cargo.toml (94%) rename {tokio-trace-macros => tokio-trace-derive}/examples/basic.rs (95%) rename {tokio-trace-macros => tokio-trace-derive}/src/lib.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index a87e052de2..88faac6aa7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ + "tokio-trace-derive", "tokio-trace-futures", "tokio-trace-tower", "tokio-trace-tower-http", diff --git a/tokio-trace-macros/Cargo.toml b/tokio-trace-derive/Cargo.toml similarity index 94% rename from tokio-trace-macros/Cargo.toml rename to tokio-trace-derive/Cargo.toml index c4bd2906f0..e62d7d412c 100644 --- a/tokio-trace-macros/Cargo.toml +++ b/tokio-trace-derive/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "tokio-trace-macros" +name = "tokio-trace-derive" version = "0.1.0" authors = ["Eliza Weisman ", "David Barsky "] diff --git a/tokio-trace-macros/examples/basic.rs b/tokio-trace-derive/examples/basic.rs similarity index 95% rename from tokio-trace-macros/examples/basic.rs rename to tokio-trace-derive/examples/basic.rs index a128f67f18..bc9c21b8bc 100644 --- a/tokio-trace-macros/examples/basic.rs +++ b/tokio-trace-derive/examples/basic.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate tokio_trace; #[macro_use] -extern crate tokio_trace_macros; +extern crate tokio_trace_derive; extern crate env_logger; extern crate tokio_trace_log; diff --git a/tokio-trace-macros/src/lib.rs b/tokio-trace-derive/src/lib.rs similarity index 100% rename from tokio-trace-macros/src/lib.rs rename to tokio-trace-derive/src/lib.rs From d8004fe34b68e33200fb251adee0d655bb8b960b Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Tue, 22 Jan 2019 11:28:21 -0800 Subject: [PATCH 2/4] macros: Add a `tokio-trace`-compatible version of std's `dbg!` macro Signed-off-by: Eliza Weisman --- tokio-trace-macros/Cargo.toml | 12 ++++++++ tokio-trace-macros/examples/factorial.rs | 24 +++++++++++++++ tokio-trace-macros/src/lib.rs | 39 ++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 tokio-trace-macros/Cargo.toml create mode 100644 tokio-trace-macros/examples/factorial.rs create mode 100644 tokio-trace-macros/src/lib.rs diff --git a/tokio-trace-macros/Cargo.toml b/tokio-trace-macros/Cargo.toml new file mode 100644 index 0000000000..a59d7bb894 --- /dev/null +++ b/tokio-trace-macros/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "tokio-trace-macros" +version = "0.1.0" +authors = ["Eliza Weisman "] + +[dependencies] +tokio-trace = "0.0.1" + + +[dev-dependencies] +tokio-trace-log = { path = "../tokio-trace-log" } +env_logger = "0.5" diff --git a/tokio-trace-macros/examples/factorial.rs b/tokio-trace-macros/examples/factorial.rs new file mode 100644 index 0000000000..bc842fa907 --- /dev/null +++ b/tokio-trace-macros/examples/factorial.rs @@ -0,0 +1,24 @@ +//! Compare to the example given in the documentation for the `std::dbg` macro. +#[macro_use] +extern crate tokio_trace; +#[macro_use] +extern crate tokio_trace_macros; +extern crate env_logger; +extern crate tokio_trace_log; + +fn factorial(n: u32) -> u32 { + if trace_dbg!(n <= 1) { + trace_dbg!(1) + } else { + trace_dbg!(n * factorial(n - 1)) + } +} + +fn main() { + env_logger::Builder::new().parse("trace").init(); + let subscriber = tokio_trace_log::TraceLogger::new(); + + tokio_trace::subscriber::with_default(subscriber, || { + trace_dbg!(factorial(4)) + }); +} diff --git a/tokio-trace-macros/src/lib.rs b/tokio-trace-macros/src/lib.rs new file mode 100644 index 0000000000..92f93805f2 --- /dev/null +++ b/tokio-trace-macros/src/lib.rs @@ -0,0 +1,39 @@ +#[macro_use] +extern crate tokio_trace; + +/// Similar to the `std::dbg!` macro, but generates `tokio-trace` events rather +/// than printing to stdout. +#[macro_export] +macro_rules! trace_dbg { + (level: $level:expr, $ex:expr) => { + { + #[allow(unused_imports)] + use tokio_trace::{callsite, Id, Subscriber, Event, field::{debug, Value}}; + use tokio_trace::callsite::Callsite; + let callsite = callsite! {@ + name: concat!("event:trace_dbg(", stringify!($ex), ")"), + target: module_path!(), + level: $level, + fields: &[stringify!($ex)] + }; + let interest = callsite.interest(); + let val = $ex; + if interest.is_never() { + val + } else { + let meta = callsite.metadata(); + let mut event = Event::new(interest, meta); + if !event.is_disabled() { + let key = meta.fields().into_iter().next() + .expect("trace_dbg event must have one field"); + event.record(&key, &debug(val)); + } + drop(event); + val + } + } + }; + ($ex:expr) => { + trace_dbg!(level: tokio_trace::Level::DEBUG, $ex) + }; +} From 430c6e4b88200e51d5351b6d7f8a8dcd063234f4 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Fri, 1 Feb 2019 11:07:23 -0800 Subject: [PATCH 3/4] Make `trace_dbg!` an alias for `dbg!`, allow metadata customization Signed-off-by: Eliza Weisman --- tokio-trace-macros/examples/factorial.rs | 8 +++--- tokio-trace-macros/src/lib.rs | 35 +++++++++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/tokio-trace-macros/examples/factorial.rs b/tokio-trace-macros/examples/factorial.rs index bc842fa907..4e7e1d5177 100644 --- a/tokio-trace-macros/examples/factorial.rs +++ b/tokio-trace-macros/examples/factorial.rs @@ -7,10 +7,10 @@ extern crate env_logger; extern crate tokio_trace_log; fn factorial(n: u32) -> u32 { - if trace_dbg!(n <= 1) { - trace_dbg!(1) + if dbg!(n <= 1) { + dbg!(1) } else { - trace_dbg!(n * factorial(n - 1)) + dbg!(n * factorial(n - 1)) } } @@ -19,6 +19,6 @@ fn main() { let subscriber = tokio_trace_log::TraceLogger::new(); tokio_trace::subscriber::with_default(subscriber, || { - trace_dbg!(factorial(4)) + dbg!(factorial(4)) }); } diff --git a/tokio-trace-macros/src/lib.rs b/tokio-trace-macros/src/lib.rs index 92f93805f2..592d16211d 100644 --- a/tokio-trace-macros/src/lib.rs +++ b/tokio-trace-macros/src/lib.rs @@ -1,18 +1,39 @@ #[macro_use] extern crate tokio_trace; +/// Alias of `dbg!` for avoiding conflicts with the `std::dbg!` macro. +#[macro_export(local_inner_macros)] +macro_rules! trace_dbg { + (level: $level:expr, $ex:expr) => { + dbg!(level: $level, $ex) + }; + (level: $level:expr, $ex:expr) => { + dbg!(target: module_path!(), level: $level, $ex) + }; + (target: $level:expr, $ex:expr) => { + dbg!(target: $target, level: tokio_trace::Level::DEBUG, $ex) + }; + ($ex:expr) => { + dbg!(level: tokio_trace::Level::DEBUG, $ex) + }; + +} + /// Similar to the `std::dbg!` macro, but generates `tokio-trace` events rather /// than printing to stdout. +/// +/// By default, the verbosity level for the generated events is `DEBUG`, but +/// this can be customized. #[macro_export] -macro_rules! trace_dbg { - (level: $level:expr, $ex:expr) => { +macro_rules! dbg { + (target: $target:expr, level: $level:expr, $ex:expr) => { { #[allow(unused_imports)] use tokio_trace::{callsite, Id, Subscriber, Event, field::{debug, Value}}; use tokio_trace::callsite::Callsite; let callsite = callsite! {@ name: concat!("event:trace_dbg(", stringify!($ex), ")"), - target: module_path!(), + target: $target, level: $level, fields: &[stringify!($ex)] }; @@ -33,7 +54,13 @@ macro_rules! trace_dbg { } } }; + (level: $level:expr, $ex:expr) => { + dbg!(target: module_path!(), level: $level, $ex) + }; + (target: $level:expr, $ex:expr) => { + dbg!(target: $target, level: tokio_trace::Level::DEBUG, $ex) + }; ($ex:expr) => { - trace_dbg!(level: tokio_trace::Level::DEBUG, $ex) + dbg!(level: tokio_trace::Level::DEBUG, $ex) }; } From 3c813f2f74c44a6254f8731152804a3873e5a895 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Fri, 1 Feb 2019 13:00:30 -0800 Subject: [PATCH 4/4] s/derive/proc-macros Signed-off-by: Eliza Weisman --- Cargo.toml | 2 +- {tokio-trace-derive => tokio-trace-proc-macros}/Cargo.toml | 2 +- .../examples/basic.rs | 2 +- {tokio-trace-derive => tokio-trace-proc-macros}/src/lib.rs | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename {tokio-trace-derive => tokio-trace-proc-macros}/Cargo.toml (93%) rename {tokio-trace-derive => tokio-trace-proc-macros}/examples/basic.rs (95%) rename {tokio-trace-derive => tokio-trace-proc-macros}/src/lib.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 88faac6aa7..b72bd50618 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = [ - "tokio-trace-derive", + "tokio-trace-proc-macros", "tokio-trace-futures", "tokio-trace-tower", "tokio-trace-tower-http", diff --git a/tokio-trace-derive/Cargo.toml b/tokio-trace-proc-macros/Cargo.toml similarity index 93% rename from tokio-trace-derive/Cargo.toml rename to tokio-trace-proc-macros/Cargo.toml index e62d7d412c..f802e92aec 100644 --- a/tokio-trace-derive/Cargo.toml +++ b/tokio-trace-proc-macros/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "tokio-trace-derive" +name = "tokio-trace-proc-macros" version = "0.1.0" authors = ["Eliza Weisman ", "David Barsky "] diff --git a/tokio-trace-derive/examples/basic.rs b/tokio-trace-proc-macros/examples/basic.rs similarity index 95% rename from tokio-trace-derive/examples/basic.rs rename to tokio-trace-proc-macros/examples/basic.rs index bc9c21b8bc..fc72792e17 100644 --- a/tokio-trace-derive/examples/basic.rs +++ b/tokio-trace-proc-macros/examples/basic.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate tokio_trace; #[macro_use] -extern crate tokio_trace_derive; +extern crate tokio_trace_proc_macros; extern crate env_logger; extern crate tokio_trace_log; diff --git a/tokio-trace-derive/src/lib.rs b/tokio-trace-proc-macros/src/lib.rs similarity index 100% rename from tokio-trace-derive/src/lib.rs rename to tokio-trace-proc-macros/src/lib.rs