Skip to content

Commit

Permalink
simplify the macro
Browse files Browse the repository at this point in the history
  • Loading branch information
guswynn committed Jan 11, 2022
1 parent 0b4c3ac commit 12474e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 188 deletions.
171 changes: 13 additions & 158 deletions tracing/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,52 +814,6 @@ macro_rules! event {
///
#[macro_export]
macro_rules! enabled {
(target: $target:expr, parent: $parent:expr, $lvl:expr, { $($fields:tt)* } )=> (
if $crate::level_enabled!($lvl) {
use $crate::__macro_support::Callsite as _;
static CALLSITE: $crate::__macro_support::MacroCallsite = $crate::callsite2! {
name: concat!(
"enabled! ",
file!(),
":",
line!()
),
kind: $crate::metadata::Kind::HINT,
target: $target,
level: $lvl,
fields: $($fields)*
};
let interest = CALLSITE.interest();
if !interest.is_never() && CALLSITE.is_enabled(interest) {
let meta = CALLSITE.metadata();
// event with explicit parent
$crate::Event::child_of_enabled(
$parent,
meta,
&$crate::valueset!(meta.fields(), $($fields)*)
)
} else {
false
}
} else {
false
}
);

(target: $target:expr, parent: $parent:expr, $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => (
$crate::enabled!(
target: $target,
parent: $parent,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
)
);
(target: $target:expr, parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
$crate::enabled!(target: $target, parent: $parent, $lvl, { $($k).+ = $($fields)* })
);
(target: $target:expr, parent: $parent:expr, $lvl:expr, $($arg:tt)+) => (
$crate::enabled!(target: $target, parent: $parent, $lvl, { $($arg)+ })
);
(target: $target:expr, $lvl:expr, { $($fields:tt)* } )=> ({
if $crate::level_enabled!($lvl) {
use $crate::__macro_support::Callsite as _;
Expand All @@ -878,144 +832,45 @@ macro_rules! enabled {
let interest = CALLSITE.interest();
if !interest.is_never() && CALLSITE.is_enabled(interest) {
let meta = CALLSITE.metadata();
// event with contextual parent
$crate::Event::enabled(
meta,
&$crate::valueset!(meta.fields(), $($fields)*)
)
$crate::dispatch::get_default(|current| current.enabled(meta))
} else {
false
}
} else {
false
}
});
(target: $target:expr, $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => (
$crate::enabled!(
target: $target,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
)
);
(target: $target:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
$crate::enabled!(target: $target, $lvl, { $($k).+ = $($fields)* })
);
(target: $target:expr, $lvl:expr, $($arg:tt)+ ) => (
$crate::enabled!(target: $target, $lvl, { $($arg)+ })
);
// This cases is added on top of how `event` works
// Just target and level
(target: $target:expr, $lvl:expr ) => (
$crate::enabled!(target: $target, $lvl, { "hint_message" })
);
(parent: $parent:expr, $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => (
$crate::enabled!(
target: module_path!(),
parent: $parent,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
)
);
(parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
parent: $parent,
$lvl,
{ $($k).+ = $($field)*}
)
);
(parent: $parent:expr, $lvl:expr, ?$($k:ident).+ = $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
parent: $parent,
$lvl,
{ ?$($k).+ = $($field)*}
)
$crate::enabled!(target: $target, $lvl, { })
);
(parent: $parent:expr, $lvl:expr, %$($k:ident).+ = $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
parent: $parent,
$lvl,
{ %$($k).+ = $($field)*}
)

// These two cases handle fields with no values
(target: $target:expr, $lvl:expr, $($k:ident).+) => (
$crate::enabled!(target: $target, $lvl, $($k).+,)
);
(parent: $parent:expr, $lvl:expr, $($k:ident).+, $($field:tt)*) => (
(target: $target:expr, $lvl:expr, $($k:ident).+, $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
parent: $parent,
target: $target,
$lvl,
{ $($k).+, $($field)*}
)
);
(parent: $parent:expr, $lvl:expr, %$($k:ident).+, $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
parent: $parent,
$lvl,
{ %$($k).+, $($field)*}
)
);
(parent: $parent:expr, $lvl:expr, ?$($k:ident).+, $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
parent: $parent,
$lvl,
{ ?$($k).+, $($field)*}
)
);
(parent: $parent:expr, $lvl:expr, $($arg:tt)+ ) => (
$crate::enabled!(target: module_path!(), parent: $parent, $lvl, { $($arg)+ })
);
( $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => (
$crate::enabled!(
target: module_path!(),
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
)
);
($lvl:expr, $($k:ident).+ = $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
$lvl,
{ $($k).+ = $($field)*}
)
);

($lvl:expr, $($k:ident).+, $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
$lvl,
{ $($k).+, $($field)*}
)
);
($lvl:expr, ?$($k:ident).+, $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
$lvl,
{ ?$($k).+, $($field)*}
)
);
($lvl:expr, %$($k:ident).+, $($field:tt)*) => (
$crate::enabled!(
target: module_path!(),
$lvl,
{ %$($k).+, $($field)*}
)
);
($lvl:expr, ?$($k:ident).+) => (
$crate::enabled!($lvl, ?$($k).+,)
);
($lvl:expr, %$($k:ident).+) => (
$crate::enabled!($lvl, %$($k).+,)
);
($lvl:expr, $($k:ident).+) => (
$crate::enabled!($lvl, $($k).+,)
);
( $lvl:expr, $($arg:tt)+ ) => (
$crate::enabled!(target: module_path!(), $lvl, { $($arg)+ })
);
// This cases is added on top of how `event` works

// Simplest `enabled!` case
( $lvl:expr ) => (
$crate::enabled!(target: module_path!(), $lvl, { "hint_message" })
$crate::enabled!(target: module_path!(), $lvl, { })
);
}

Expand Down
34 changes: 4 additions & 30 deletions tracing/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,40 +337,14 @@ fn event() {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn enabled() {
enabled!(Level::DEBUG, foo = ?3, bar.baz = %2, quux = false);
enabled!(Level::DEBUG, foo = 3, bar.baz = 2, quux = false);
enabled!(Level::DEBUG, foo = 3, bar.baz = 3,);
enabled!(Level::DEBUG, "foo");
enabled!(Level::DEBUG, "foo: {}", 3);
enabled!(Level::INFO, foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
enabled!(
Level::INFO,
foo = 3,
bar.baz = 2,
quux = false,
"hello world {:?}",
42
);
enabled!(Level::INFO, foo = 3, bar.baz = 3, "hello world {:?}", 42,);
enabled!(Level::DEBUG, { foo = 3, bar.baz = 80 }, "quux");
enabled!(Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}", true);
enabled!(Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
enabled!(Level::DEBUG, { foo = ?2, bar.baz = %78 }, "quux");
enabled!(target: "foo_events", Level::DEBUG, foo = 3, bar.baz = 2, quux = false);
enabled!(target: "foo_events", Level::DEBUG, foo = 3, bar.baz = 3,);
enabled!(target: "foo_events", Level::DEBUG, "foo");
enabled!(target: "foo_events", Level::DEBUG, "foo: {}", 3);
enabled!(target: "foo_events", Level::DEBUG, { foo = 3, bar.baz = 80 }, "quux");
enabled!(target: "foo_events", Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}", true);
enabled!(target: "foo_events", Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
enabled!(target: "foo_events", Level::DEBUG, { foo = 2, bar.baz = 78, }, "quux");
let foo = 1;
enabled!(Level::DEBUG, ?foo);
enabled!(Level::DEBUG, %foo);
enabled!(Level::DEBUG, foo, bar.baz, quux,);
enabled!(Level::DEBUG, message);
enabled!(Level::INFO, foo, bar.baz, quux, messaged,);
enabled!(Level::DEBUG, foo);

enabled!(Level::DEBUG);
enabled!(target: "rando", Level::DEBUG);
enabled!(target: "rando", Level::DEBUG, field);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
Expand Down

0 comments on commit 12474e1

Please sign in to comment.