Skip to content

Commit cc02d08

Browse files
mladedavdpc
authored andcommittedApr 20, 2024
tracing: fix event macros with constant field names in the first position (tokio-rs#2883)
## Motivation Const argumetns in `level!` macros do not work when in the first position. This also seems to have fixed tokio-rs#2748 where literals for fields names like `info!("foo" = 2)` could not be used outside the `event!` macro. Fixes tokio-rs#2837 Fixes tokio-rs#2738 ## Solution Previsously, `level!($(args:tt))` was forwarded to `event!(target: ..., level: ..., { $(args:tt) })` but the added curly braces seem to have prevented the `event` macro from correctly understanding the arguments and it tried to pass them to `format!`. With this change there may have some performance impact when expanding the macros in most cases where the braces could have been added as it will take one more step. These are the two relevant `event!` blocks I believe, the new tests used to expand to the first one (with empty fields), now they expand to the latter: ``` (target: $target:expr, $lvl:expr, { $($fields:tt)* }, $($arg:tt)+ ) => ( $crate::event!( target: $target, $lvl, { message = $crate::__macro_support::format_args!($($arg)+), $($fields)* } ) ); (target: $target:expr, $lvl:expr, $($arg:tt)+ ) => ( $crate::event!(target: $target, $lvl, { $($arg)+ }) ); ```
1 parent e5b2603 commit cc02d08

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed
 

Diff for: ‎tracing/src/macros.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,6 @@ macro_rules! trace {
15471547
$crate::event!(
15481548
target: module_path!(),
15491549
$crate::Level::TRACE,
1550-
{},
15511550
$($arg)+
15521551
)
15531552
);
@@ -1824,7 +1823,6 @@ macro_rules! debug {
18241823
$crate::event!(
18251824
target: module_path!(),
18261825
$crate::Level::DEBUG,
1827-
{},
18281826
$($arg)+
18291827
)
18301828
);
@@ -2112,7 +2110,6 @@ macro_rules! info {
21122110
$crate::event!(
21132111
target: module_path!(),
21142112
$crate::Level::INFO,
2115-
{},
21162113
$($arg)+
21172114
)
21182115
);
@@ -2393,7 +2390,6 @@ macro_rules! warn {
23932390
$crate::event!(
23942391
target: module_path!(),
23952392
$crate::Level::WARN,
2396-
{},
23972393
$($arg)+
23982394
)
23992395
);
@@ -2670,7 +2666,6 @@ macro_rules! error {
26702666
$crate::event!(
26712667
target: module_path!(),
26722668
$crate::Level::ERROR,
2673-
{},
26742669
$($arg)+
26752670
)
26762671
);

Diff for: ‎tracing/tests/event.rs

+54
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ fn constant_field_name() {
525525
)
526526
};
527527
let (subscriber, handle) = subscriber::mock()
528+
.event(expect_event())
529+
.event(expect_event())
530+
.event(expect_event())
531+
.event(expect_event())
532+
.event(expect_event())
533+
.event(expect_event())
528534
.event(expect_event())
529535
.event(expect_event())
530536
.only()
@@ -548,6 +554,54 @@ fn constant_field_name() {
548554
},
549555
"quux"
550556
);
557+
tracing::info!(
558+
{ std::convert::identity(FOO) } = "bar",
559+
{ "constant string" } = "also works",
560+
foo.bar = "baz",
561+
"quux"
562+
);
563+
tracing::info!(
564+
{
565+
{ std::convert::identity(FOO) } = "bar",
566+
{ "constant string" } = "also works",
567+
foo.bar = "baz",
568+
},
569+
"quux"
570+
);
571+
tracing::event!(
572+
Level::INFO,
573+
{ std::convert::identity(FOO) } = "bar",
574+
{ "constant string" } = "also works",
575+
foo.bar = "baz",
576+
"{}",
577+
"quux"
578+
);
579+
tracing::event!(
580+
Level::INFO,
581+
{
582+
{ std::convert::identity(FOO) } = "bar",
583+
{ "constant string" } = "also works",
584+
foo.bar = "baz",
585+
},
586+
"{}",
587+
"quux"
588+
);
589+
tracing::info!(
590+
{ std::convert::identity(FOO) } = "bar",
591+
{ "constant string" } = "also works",
592+
foo.bar = "baz",
593+
"{}",
594+
"quux"
595+
);
596+
tracing::info!(
597+
{
598+
{ std::convert::identity(FOO) } = "bar",
599+
{ "constant string" } = "also works",
600+
foo.bar = "baz",
601+
},
602+
"{}",
603+
"quux"
604+
);
551605
});
552606

553607
handle.assert_finished();

Diff for: ‎tracing/tests/macros.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,15 @@ fn trace() {
545545
trace!(foo = ?3, bar.baz = %2, quux = false);
546546
trace!(foo = 3, bar.baz = 2, quux = false);
547547
trace!(foo = 3, bar.baz = 3,);
548+
trace!("foo" = 3, bar.baz = 3,);
549+
trace!(foo = 3, "bar.baz" = 3,);
548550
trace!("foo");
549551
trace!("foo: {}", 3);
550552
trace!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
551553
trace!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
552554
trace!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
553555
trace!({ foo = 3, bar.baz = 80 }, "quux");
556+
trace!({ "foo" = 3, "bar.baz" = 80 }, "quux");
554557
trace!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
555558
trace!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
556559
trace!({ foo = 2, bar.baz = 78 }, "quux");
@@ -569,6 +572,9 @@ fn trace() {
569572
trace!(?foo);
570573
trace!(%foo);
571574
trace!(foo);
575+
trace!("foo" = ?foo);
576+
trace!("foo" = %foo);
577+
trace!("foo" = foo);
572578
trace!(name: "foo", ?foo);
573579
trace!(name: "foo", %foo);
574580
trace!(name: "foo", foo);
@@ -589,12 +595,15 @@ fn debug() {
589595
debug!(foo = ?3, bar.baz = %2, quux = false);
590596
debug!(foo = 3, bar.baz = 2, quux = false);
591597
debug!(foo = 3, bar.baz = 3,);
598+
debug!("foo" = 3, bar.baz = 3,);
599+
debug!(foo = 3, "bar.baz" = 3,);
592600
debug!("foo");
593601
debug!("foo: {}", 3);
594602
debug!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
595603
debug!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
596604
debug!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
597605
debug!({ foo = 3, bar.baz = 80 }, "quux");
606+
debug!({ "foo" = 3, "bar.baz" = 80 }, "quux");
598607
debug!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
599608
debug!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
600609
debug!({ foo = 2, bar.baz = 78 }, "quux");
@@ -613,6 +622,9 @@ fn debug() {
613622
debug!(?foo);
614623
debug!(%foo);
615624
debug!(foo);
625+
debug!("foo" = ?foo);
626+
debug!("foo" = %foo);
627+
debug!("foo" = foo);
616628
debug!(name: "foo", ?foo);
617629
debug!(name: "foo", %foo);
618630
debug!(name: "foo", foo);
@@ -633,12 +645,15 @@ fn info() {
633645
info!(foo = ?3, bar.baz = %2, quux = false);
634646
info!(foo = 3, bar.baz = 2, quux = false);
635647
info!(foo = 3, bar.baz = 3,);
648+
info!("foo" = 3, bar.baz = 3,);
649+
info!(foo = 3, "bar.baz" = 3,);
636650
info!("foo");
637651
info!("foo: {}", 3);
638652
info!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
639653
info!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
640654
info!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
641655
info!({ foo = 3, bar.baz = 80 }, "quux");
656+
info!({ "foo" = 3, "bar.baz" = 80 }, "quux");
642657
info!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
643658
info!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
644659
info!({ foo = 2, bar.baz = 78 }, "quux");
@@ -657,6 +672,9 @@ fn info() {
657672
info!(?foo);
658673
info!(%foo);
659674
info!(foo);
675+
info!("foo" = ?foo);
676+
info!("foo" = %foo);
677+
info!("foo" = foo);
660678
info!(name: "foo", ?foo);
661679
info!(name: "foo", %foo);
662680
info!(name: "foo", foo);
@@ -677,12 +695,15 @@ fn warn() {
677695
warn!(foo = ?3, bar.baz = %2, quux = false);
678696
warn!(foo = 3, bar.baz = 2, quux = false);
679697
warn!(foo = 3, bar.baz = 3,);
698+
warn!("foo" = 3, bar.baz = 3,);
699+
warn!(foo = 3, "bar.baz" = 3,);
680700
warn!("foo");
681701
warn!("foo: {}", 3);
682702
warn!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
683703
warn!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
684704
warn!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
685705
warn!({ foo = 3, bar.baz = 80 }, "quux");
706+
warn!({ "foo" = 3, "bar.baz" = 80 }, "quux");
686707
warn!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
687708
warn!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
688709
warn!({ foo = 2, bar.baz = 78 }, "quux");
@@ -701,6 +722,9 @@ fn warn() {
701722
warn!(?foo);
702723
warn!(%foo);
703724
warn!(foo);
725+
warn!("foo" = ?foo);
726+
warn!("foo" = %foo);
727+
warn!("foo" = foo);
704728
warn!(name: "foo", ?foo);
705729
warn!(name: "foo", %foo);
706730
warn!(name: "foo", foo);
@@ -721,15 +745,18 @@ fn error() {
721745
error!(foo = ?3, bar.baz = %2, quux = false);
722746
error!(foo = 3, bar.baz = 2, quux = false);
723747
error!(foo = 3, bar.baz = 3,);
748+
error!("foo" = 3, bar.baz = 3,);
749+
error!(foo = 3, "bar.baz" = 3,);
724750
error!("foo");
725751
error!("foo: {}", 3);
726752
error!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
727753
error!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
728754
error!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
729755
error!({ foo = 3, bar.baz = 80 }, "quux");
756+
error!({ "foo" = 3, "bar.baz" = 80 }, "quux");
730757
error!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
731758
error!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
732-
error!({ foo = 2, bar.baz = 78, }, "quux");
759+
error!({ foo = 2, bar.baz = 78 }, "quux");
733760
error!({ foo = ?2, bar.baz = %78 }, "quux");
734761
error!(name: "foo", foo = 3, bar.baz = 2, quux = false);
735762
error!(name: "foo", target: "foo_events", foo = 3, bar.baz = 2, quux = false);
@@ -745,6 +772,9 @@ fn error() {
745772
error!(?foo);
746773
error!(%foo);
747774
error!(foo);
775+
error!("foo" = ?foo);
776+
error!("foo" = %foo);
777+
error!("foo" = foo);
748778
error!(name: "foo", ?foo);
749779
error!(name: "foo", %foo);
750780
error!(name: "foo", foo);

0 commit comments

Comments
 (0)
Please sign in to comment.