Skip to content

Commit 7bb5bb2

Browse files
mladedavhds
authored andcommitted
tracing: fix event macros with constant field names in the first position (#2883)
## Motivation Const argumetns in `level!` macros do not work when in the first position. This also seems to have fixed #2748 where literals for fields names like `info!("foo" = 2)` could not be used outside the `event!` macro. Fixes #2837 Fixes #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 83136ef commit 7bb5bb2

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

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
);

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();

tracing/tests/macros.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,15 @@ fn trace() {
555555
trace!(foo = ?3, bar.baz = %2, quux = false);
556556
trace!(foo = 3, bar.baz = 2, quux = false);
557557
trace!(foo = 3, bar.baz = 3,);
558+
trace!("foo" = 3, bar.baz = 3,);
559+
trace!(foo = 3, "bar.baz" = 3,);
558560
trace!("foo");
559561
trace!("foo: {}", 3);
560562
trace!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
561563
trace!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
562564
trace!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
563565
trace!({ foo = 3, bar.baz = 80 }, "quux");
566+
trace!({ "foo" = 3, "bar.baz" = 80 }, "quux");
564567
trace!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
565568
trace!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
566569
trace!({ foo = 2, bar.baz = 78 }, "quux");
@@ -579,6 +582,9 @@ fn trace() {
579582
trace!(?foo);
580583
trace!(%foo);
581584
trace!(foo);
585+
trace!("foo" = ?foo);
586+
trace!("foo" = %foo);
587+
trace!("foo" = foo);
582588
trace!(name: "foo", ?foo);
583589
trace!(name: "foo", %foo);
584590
trace!(name: "foo", foo);
@@ -615,12 +621,15 @@ fn debug() {
615621
debug!(foo = ?3, bar.baz = %2, quux = false);
616622
debug!(foo = 3, bar.baz = 2, quux = false);
617623
debug!(foo = 3, bar.baz = 3,);
624+
debug!("foo" = 3, bar.baz = 3,);
625+
debug!(foo = 3, "bar.baz" = 3,);
618626
debug!("foo");
619627
debug!("foo: {}", 3);
620628
debug!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
621629
debug!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
622630
debug!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
623631
debug!({ foo = 3, bar.baz = 80 }, "quux");
632+
debug!({ "foo" = 3, "bar.baz" = 80 }, "quux");
624633
debug!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
625634
debug!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
626635
debug!({ foo = 2, bar.baz = 78 }, "quux");
@@ -639,6 +648,9 @@ fn debug() {
639648
debug!(?foo);
640649
debug!(%foo);
641650
debug!(foo);
651+
debug!("foo" = ?foo);
652+
debug!("foo" = %foo);
653+
debug!("foo" = foo);
642654
debug!(name: "foo", ?foo);
643655
debug!(name: "foo", %foo);
644656
debug!(name: "foo", foo);
@@ -675,12 +687,15 @@ fn info() {
675687
info!(foo = ?3, bar.baz = %2, quux = false);
676688
info!(foo = 3, bar.baz = 2, quux = false);
677689
info!(foo = 3, bar.baz = 3,);
690+
info!("foo" = 3, bar.baz = 3,);
691+
info!(foo = 3, "bar.baz" = 3,);
678692
info!("foo");
679693
info!("foo: {}", 3);
680694
info!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
681695
info!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
682696
info!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
683697
info!({ foo = 3, bar.baz = 80 }, "quux");
698+
info!({ "foo" = 3, "bar.baz" = 80 }, "quux");
684699
info!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
685700
info!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
686701
info!({ foo = 2, bar.baz = 78 }, "quux");
@@ -699,6 +714,9 @@ fn info() {
699714
info!(?foo);
700715
info!(%foo);
701716
info!(foo);
717+
info!("foo" = ?foo);
718+
info!("foo" = %foo);
719+
info!("foo" = foo);
702720
info!(name: "foo", ?foo);
703721
info!(name: "foo", %foo);
704722
info!(name: "foo", foo);
@@ -735,12 +753,15 @@ fn warn() {
735753
warn!(foo = ?3, bar.baz = %2, quux = false);
736754
warn!(foo = 3, bar.baz = 2, quux = false);
737755
warn!(foo = 3, bar.baz = 3,);
756+
warn!("foo" = 3, bar.baz = 3,);
757+
warn!(foo = 3, "bar.baz" = 3,);
738758
warn!("foo");
739759
warn!("foo: {}", 3);
740760
warn!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
741761
warn!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
742762
warn!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
743763
warn!({ foo = 3, bar.baz = 80 }, "quux");
764+
warn!({ "foo" = 3, "bar.baz" = 80 }, "quux");
744765
warn!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
745766
warn!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
746767
warn!({ foo = 2, bar.baz = 78 }, "quux");
@@ -759,6 +780,9 @@ fn warn() {
759780
warn!(?foo);
760781
warn!(%foo);
761782
warn!(foo);
783+
warn!("foo" = ?foo);
784+
warn!("foo" = %foo);
785+
warn!("foo" = foo);
762786
warn!(name: "foo", ?foo);
763787
warn!(name: "foo", %foo);
764788
warn!(name: "foo", foo);
@@ -795,15 +819,18 @@ fn error() {
795819
error!(foo = ?3, bar.baz = %2, quux = false);
796820
error!(foo = 3, bar.baz = 2, quux = false);
797821
error!(foo = 3, bar.baz = 3,);
822+
error!("foo" = 3, bar.baz = 3,);
823+
error!(foo = 3, "bar.baz" = 3,);
798824
error!("foo");
799825
error!("foo: {}", 3);
800826
error!(foo = ?3, bar.baz = %2, quux = false, "hello world {:?}", 42);
801827
error!(foo = 3, bar.baz = 2, quux = false, "hello world {:?}", 42);
802828
error!(foo = 3, bar.baz = 3, "hello world {:?}", 42,);
803829
error!({ foo = 3, bar.baz = 80 }, "quux");
830+
error!({ "foo" = 3, "bar.baz" = 80 }, "quux");
804831
error!({ foo = 2, bar.baz = 79 }, "quux {:?}", true);
805832
error!({ foo = 2, bar.baz = 79 }, "quux {:?}, {quux}", true, quux = false);
806-
error!({ foo = 2, bar.baz = 78, }, "quux");
833+
error!({ foo = 2, bar.baz = 78 }, "quux");
807834
error!({ foo = ?2, bar.baz = %78 }, "quux");
808835
error!(name: "foo", foo = 3, bar.baz = 2, quux = false);
809836
error!(name: "foo", target: "foo_events", foo = 3, bar.baz = 2, quux = false);
@@ -819,6 +846,9 @@ fn error() {
819846
error!(?foo);
820847
error!(%foo);
821848
error!(foo);
849+
error!("foo" = ?foo);
850+
error!("foo" = %foo);
851+
error!("foo" = foo);
822852
error!(name: "foo", ?foo);
823853
error!(name: "foo", %foo);
824854
error!(name: "foo", foo);

0 commit comments

Comments
 (0)