Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tokio-trace-macros crate with a trace-ified version of std::dbg! #1

Merged
merged 4 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]

members = [
"tokio-trace-proc-macros",
"tokio-trace-futures",
"tokio-trace-tower",
"tokio-trace-tower-http",
Expand Down
12 changes: 2 additions & 10 deletions tokio-trace-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
[package]
name = "tokio-trace-macros"
version = "0.1.0"
authors = ["Eliza Weisman <eliza@buoyant.io>", "David Barsky <dbarsky@amazon.com>"]

[lib]
proc-macro = true
authors = ["Eliza Weisman <eliza@buoyant.io>"]

[dependencies]
tokio-trace = "0.0.1"
syn = { version = "0.15", features = ["full", "extra-traits"] }
quote = "0.6"
proc-macro2 = { version = "0.4", features = ["nightly"] }


[dev-dependencies]
tokio-trace = "0.0.1"
tokio-trace-log = { path = "../tokio-trace-log" }
env_logger = "0.5"
ansi_term = "0.11"
humantime = "1.1.1"
24 changes: 24 additions & 0 deletions tokio-trace-macros/examples/factorial.rs
Original file line number Diff line number Diff line change
@@ -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 dbg!(n <= 1) {
dbg!(1)
} else {
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, || {
dbg!(factorial(4))
});
}
100 changes: 60 additions & 40 deletions tokio-trace-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,66 @@
extern crate proc_macro;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate quote;
extern crate proc_macro2;

use proc_macro::TokenStream;
use proc_macro2::Span;
use syn::token::{Async, Const, Unsafe};
use syn::{Abi, Attribute, Block, Ident, ItemFn, Visibility};

#[proc_macro_attribute]
pub fn trace(_args: TokenStream, item: TokenStream) -> TokenStream {
let input: ItemFn = parse_macro_input!(item as ItemFn);
let call_site = Span::call_site();
extern crate tokio_trace;

// these are needed ahead of time, as ItemFn contains the function body _and_
// isn't representable inside a quote!/quote_spanned! macro
// (Syn's ToTokens isn't implemented for ItemFn)
let attrs: Vec<Attribute> = input.clone().attrs;
let vis: Visibility = input.clone().vis;
let constness: Option<Const> = input.clone().constness;
let unsafety: Option<Unsafe> = input.clone().unsafety;
let asyncness: Option<Async> = input.clone().asyncness;
let abi: Option<Abi> = input.clone().abi;
/// 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)
};

// function body
let block: Box<Block> = input.clone().block;
// function name
let ident: Ident = input.clone().ident;
let ident_str = ident.to_string();

let return_type = input.clone().decl.output;
let params = input.clone().decl.inputs;
}

quote_spanned!(call_site=>
#(#attrs) *
#vis #constness #unsafety #asyncness #abi fn #ident(#params) #return_type {
span!(#ident_str, traced_function = &#ident_str).enter(move || {
#block
})
/// 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! 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: $target,
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
}
}
)
.into()
};
(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)
};
}
20 changes: 20 additions & 0 deletions tokio-trace-proc-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "tokio-trace-proc-macros"
version = "0.1.0"
authors = ["Eliza Weisman <eliza@buoyant.io>", "David Barsky <dbarsky@amazon.com>"]

[lib]
proc-macro = true

[dependencies]
tokio-trace = "0.0.1"
syn = { version = "0.15", features = ["full", "extra-traits"] }
quote = "0.6"
proc-macro2 = { version = "0.4", features = ["nightly"] }

[dev-dependencies]
tokio-trace = "0.0.1"
tokio-trace-log = { path = "../tokio-trace-log" }
env_logger = "0.5"
ansi_term = "0.11"
humantime = "1.1.1"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate tokio_trace;
#[macro_use]
extern crate tokio_trace_macros;
extern crate tokio_trace_proc_macros;
extern crate env_logger;
extern crate tokio_trace_log;

Expand Down
46 changes: 46 additions & 0 deletions tokio-trace-proc-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
extern crate proc_macro;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate quote;
extern crate proc_macro2;

use proc_macro::TokenStream;
use proc_macro2::Span;
use syn::token::{Async, Const, Unsafe};
use syn::{Abi, Attribute, Block, Ident, ItemFn, Visibility};

#[proc_macro_attribute]
pub fn trace(_args: TokenStream, item: TokenStream) -> TokenStream {
let input: ItemFn = parse_macro_input!(item as ItemFn);
let call_site = Span::call_site();

// these are needed ahead of time, as ItemFn contains the function body _and_
// isn't representable inside a quote!/quote_spanned! macro
// (Syn's ToTokens isn't implemented for ItemFn)
let attrs: Vec<Attribute> = input.clone().attrs;
let vis: Visibility = input.clone().vis;
let constness: Option<Const> = input.clone().constness;
let unsafety: Option<Unsafe> = input.clone().unsafety;
let asyncness: Option<Async> = input.clone().asyncness;
let abi: Option<Abi> = input.clone().abi;

// function body
let block: Box<Block> = input.clone().block;
// function name
let ident: Ident = input.clone().ident;
let ident_str = ident.to_string();

let return_type = input.clone().decl.output;
let params = input.clone().decl.inputs;

quote_spanned!(call_site=>
#(#attrs) *
#vis #constness #unsafety #asyncness #abi fn #ident(#params) #return_type {
span!(#ident_str, traced_function = &#ident_str).enter(move || {
#block
})
}
)
.into()
}