Skip to content

Commit

Permalink
tracing: use full path when calling format_args! (#2757)
Browse files Browse the repository at this point in the history
When a custom macro "format_args" is defined, macros such as
`tracing::warn`, `tracing::debug`, etc. will fail. Adding the
full path `::core::format_args!` to the calls fixes this.

Fixes: #2721
  • Loading branch information
kaffarell authored and hawkw committed Oct 13, 2023
1 parent c4b2a56 commit 3825a50
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 28 deletions.
18 changes: 9 additions & 9 deletions tracing/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ macro_rules! event {
target: $target,
parent: $parent,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
{ message = ::core::format_args!($($arg)+), $($fields)* }
)
);
(name: $name:expr, target: $target:expr, parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -678,7 +678,7 @@ macro_rules! event {
name: $name,
target: $target,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
{ message = ::core::format_args!($($arg)+), $($fields)* }
)
);
(name: $name:expr, target: $target:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -736,7 +736,7 @@ macro_rules! event {
target: $target,
parent: $parent,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
{ message = ::core::format_args!($($arg)+), $($fields)* }
)
);
(target: $target:expr, parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -789,7 +789,7 @@ macro_rules! event {
name: $name,
parent: $parent,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
{ message = ::core::format_args!($($arg)+), $($fields)* }
)
);
(name: $name:expr, parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -839,7 +839,7 @@ macro_rules! event {
$crate::event!(
name: $name,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
{ message = ::core::format_args!($($arg)+), $($fields)* }
)
);
(name: $name:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand Down Expand Up @@ -894,7 +894,7 @@ macro_rules! event {
$crate::event!(
target: $target,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
{ message = ::core::format_args!($($arg)+), $($fields)* }
)
);
(target: $target:expr, $lvl:expr, $($k:ident).+ = $($fields:tt)* ) => (
Expand All @@ -910,7 +910,7 @@ macro_rules! event {
target: module_path!(),
parent: $parent,
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
{ message = ::core::format_args!($($arg)+), $($fields)* }
)
);
(parent: $parent:expr, $lvl:expr, $($k:ident).+ = $($field:tt)*) => (
Expand Down Expand Up @@ -970,7 +970,7 @@ macro_rules! event {
$crate::event!(
target: module_path!(),
$lvl,
{ message = format_args!($($arg)+), $($fields)* }
{ message = ::core::format_args!($($arg)+), $($fields)* }
)
);
( $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => (
Expand Down Expand Up @@ -2963,7 +2963,7 @@ macro_rules! valueset {

// Remainder is unparsable, but exists --- must be format args!
(@ { $(,)* $($out:expr),* }, $next:expr, $($rest:tt)+) => {
$crate::valueset!(@ { (&$next, ::core::option::Option::Some(&format_args!($($rest)+) as &dyn Value)), $($out),* }, $next, )
$crate::valueset!(@ { (&$next, ::core::option::Option::Some(&::core::format_args!($($rest)+) as &dyn Value)), $($out),* }, $next, )
};

// === entry ===
Expand Down
151 changes: 132 additions & 19 deletions tracing/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,20 @@ fn span_root() {
span!(target: "foo_events", parent: ::core::option::Option::None, Level::TRACE, "foo", bar.baz = 2, quux = 4,);
span!(target: "foo_events", parent: ::core::option::Option::None, Level::TRACE, "foo");
span!(target: "foo_events", parent: ::core::option::Option::None, Level::TRACE, "bar",);
span!(parent: ::core::option::Option::None, Level::DEBUG, "foo", bar.baz = 2, quux = 3);
span!(parent: ::core::option::Option::None, Level::DEBUG, "foo", bar.baz = 2, quux = 4,);
span!(
parent: ::core::option::Option::None,
Level::DEBUG,
"foo",
bar.baz = 2,
quux = 3
);
span!(
parent: ::core::option::Option::None,
Level::DEBUG,
"foo",
bar.baz = 2,
quux = 4,
);
span!(parent: ::core::option::Option::None, Level::DEBUG, "foo");
span!(parent: ::core::option::Option::None, Level::DEBUG, "bar",);
}
Expand All @@ -139,8 +151,18 @@ fn trace_span_root() {
trace_span!(target: "foo_events", parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
trace_span!(target: "foo_events", parent: ::core::option::Option::None, "foo");
trace_span!(target: "foo_events", parent: ::core::option::Option::None, "bar",);
trace_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 3);
trace_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
trace_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 3
);
trace_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 4,
);
trace_span!(parent: ::core::option::Option::None, "foo");
trace_span!(parent: ::core::option::Option::None, "bar",);
}
Expand All @@ -152,8 +174,18 @@ fn debug_span_root() {
debug_span!(target: "foo_events", parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
debug_span!(target: "foo_events", parent: ::core::option::Option::None, "foo");
debug_span!(target: "foo_events", parent: ::core::option::Option::None, "bar",);
debug_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 3);
debug_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
debug_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 3
);
debug_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 4,
);
debug_span!(parent: ::core::option::Option::None, "foo");
debug_span!(parent: ::core::option::Option::None, "bar",);
}
Expand All @@ -165,8 +197,18 @@ fn info_span_root() {
info_span!(target: "foo_events", parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
info_span!(target: "foo_events", parent: ::core::option::Option::None, "foo");
info_span!(target: "foo_events", parent: ::core::option::Option::None, "bar",);
info_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 3);
info_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
info_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 3
);
info_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 4,
);
info_span!(parent: ::core::option::Option::None, "foo");
info_span!(parent: ::core::option::Option::None, "bar",);
}
Expand All @@ -178,8 +220,18 @@ fn warn_span_root() {
warn_span!(target: "foo_events", parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
warn_span!(target: "foo_events", parent: ::core::option::Option::None, "foo");
warn_span!(target: "foo_events", parent: ::core::option::Option::None, "bar",);
warn_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 3);
warn_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
warn_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 3
);
warn_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 4,
);
warn_span!(parent: ::core::option::Option::None, "foo");
warn_span!(parent: ::core::option::Option::None, "bar",);
}
Expand All @@ -191,8 +243,18 @@ fn error_span_root() {
error_span!(target: "foo_events", parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
error_span!(target: "foo_events", parent: ::core::option::Option::None, "foo");
error_span!(target: "foo_events", parent: ::core::option::Option::None, "bar",);
error_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 3);
error_span!(parent: ::core::option::Option::None, "foo", bar.baz = 2, quux = 4,);
error_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 3
);
error_span!(
parent: ::core::option::Option::None,
"foo",
bar.baz = 2,
quux = 4,
);
error_span!(parent: ::core::option::Option::None, "foo");
error_span!(parent: ::core::option::Option::None, "bar",);
}
Expand Down Expand Up @@ -712,9 +774,19 @@ fn event_root() {
bar.baz = 2,
quux = false
);
event!(parent: ::core::option::Option::None, Level::DEBUG, foo = 3, bar.baz = 3,);
event!(
parent: ::core::option::Option::None,
Level::DEBUG,
foo = 3,
bar.baz = 3,
);
event!(parent: ::core::option::Option::None, Level::DEBUG, "foo");
event!(parent: ::core::option::Option::None, Level::DEBUG, "foo: {}", 3);
event!(
parent: ::core::option::Option::None,
Level::DEBUG,
"foo: {}",
3
);
event!(parent: ::core::option::Option::None, Level::DEBUG, { foo = 3, bar.baz = 80 }, "quux");
event!(parent: ::core::option::Option::None, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}", true);
event!(parent: ::core::option::Option::None, Level::DEBUG, { foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
Expand All @@ -741,7 +813,12 @@ fn event_root() {
#[test]
fn trace_root() {
trace!(parent: ::core::option::Option::None, foo = ?3, bar.baz = %2, quux = false);
trace!(parent: ::core::option::Option::None, foo = 3, bar.baz = 2, quux = false);
trace!(
parent: ::core::option::Option::None,
foo = 3,
bar.baz = 2,
quux = false
);
trace!(parent: ::core::option::Option::None, foo = 3, bar.baz = 3,);
trace!(parent: ::core::option::Option::None, "foo");
trace!(parent: ::core::option::Option::None, "foo: {}", 3);
Expand Down Expand Up @@ -772,7 +849,12 @@ fn trace_root() {
#[test]
fn debug_root() {
debug!(parent: ::core::option::Option::None, foo = ?3, bar.baz = %2, quux = false);
debug!(parent: ::core::option::Option::None, foo = 3, bar.baz = 2, quux = false);
debug!(
parent: ::core::option::Option::None,
foo = 3,
bar.baz = 2,
quux = false
);
debug!(parent: ::core::option::Option::None, foo = 3, bar.baz = 3,);
debug!(parent: ::core::option::Option::None, "foo");
debug!(parent: ::core::option::Option::None, "foo: {}", 3);
Expand Down Expand Up @@ -803,7 +885,12 @@ fn debug_root() {
#[test]
fn info_root() {
info!(parent: ::core::option::Option::None, foo = ?3, bar.baz = %2, quux = false);
info!(parent: ::core::option::Option::None, foo = 3, bar.baz = 2, quux = false);
info!(
parent: ::core::option::Option::None,
foo = 3,
bar.baz = 2,
quux = false
);
info!(parent: ::core::option::Option::None, foo = 3, bar.baz = 3,);
info!(parent: ::core::option::Option::None, "foo");
info!(parent: ::core::option::Option::None, "foo: {}", 3);
Expand Down Expand Up @@ -834,7 +921,12 @@ fn info_root() {
#[test]
fn warn_root() {
warn!(parent: ::core::option::Option::None, foo = ?3, bar.baz = %2, quux = false);
warn!(parent: ::core::option::Option::None, foo = 3, bar.baz = 2, quux = false);
warn!(
parent: ::core::option::Option::None,
foo = 3,
bar.baz = 2,
quux = false
);
warn!(parent: ::core::option::Option::None, foo = 3, bar.baz = 3,);
warn!(parent: ::core::option::Option::None, "foo");
warn!(parent: ::core::option::Option::None, "foo: {}", 3);
Expand Down Expand Up @@ -865,7 +957,12 @@ fn warn_root() {
#[test]
fn error_root() {
error!(parent: ::core::option::Option::None, foo = ?3, bar.baz = %2, quux = false);
error!(parent: ::core::option::Option::None, foo = 3, bar.baz = 2, quux = false);
error!(
parent: ::core::option::Option::None,
foo = 3,
bar.baz = 2,
quux = false
);
error!(parent: ::core::option::Option::None, foo = 3, bar.baz = 3,);
error!(parent: ::core::option::Option::None, "foo");
error!(parent: ::core::option::Option::None, "foo: {}", 3);
Expand Down Expand Up @@ -1156,3 +1253,19 @@ fn callsite_macro_api() {
fields: foo,
};
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn format_args_already_defined() {
// Reproduces: https://github.com/tokio-rs/tracing/issues/2721
#[allow(unused)]
macro_rules! format_args {
($i:expr) => {};
}
event!(Level::DEBUG, "foo: {}", 3);
trace!("foo: {}", 3);
debug!("foo: {}", 3);
info!("foo: {}", 3);
warn!("foo: {}", 3);
error!("foo: {}", 3);
}

0 comments on commit 3825a50

Please sign in to comment.