@@ -7,7 +7,10 @@ use crate::{
7
7
registry:: LookupSpan ,
8
8
} ;
9
9
10
- use std:: fmt:: { self , Write } ;
10
+ use std:: {
11
+ fmt:: { self , Write } ,
12
+ iter,
13
+ } ;
11
14
use tracing_core:: {
12
15
field:: { self , Field , Visit } ,
13
16
span, Event , Level , Subscriber ,
@@ -344,11 +347,11 @@ where
344
347
let full_ctx = {
345
348
#[ cfg( feature = "ansi" ) ]
346
349
{
347
- FullCtx :: new ( ctx, self . ansi )
350
+ FullCtx :: new ( ctx, event . parent ( ) , self . ansi )
348
351
}
349
352
#[ cfg( not( feature = "ansi" ) ) ]
350
353
{
351
- FullCtx :: new ( & ctx)
354
+ FullCtx :: new ( ctx, event . parent ( ) )
352
355
}
353
356
} ;
354
357
@@ -401,11 +404,11 @@ where
401
404
let fmt_ctx = {
402
405
#[ cfg( feature = "ansi" ) ]
403
406
{
404
- FmtCtx :: new ( & ctx, self . ansi )
407
+ FmtCtx :: new ( & ctx, event . parent ( ) , self . ansi )
405
408
}
406
409
#[ cfg( not( feature = "ansi" ) ) ]
407
410
{
408
- FmtCtx :: new ( & ctx)
411
+ FmtCtx :: new ( & ctx, event . parent ( ) )
409
412
}
410
413
} ;
411
414
write ! ( writer, "{}" , fmt_ctx) ?;
@@ -575,6 +578,7 @@ impl<'a> fmt::Debug for DefaultVisitor<'a> {
575
578
576
579
struct FmtCtx < ' a , S , N > {
577
580
ctx : & ' a FmtContext < ' a , S , N > ,
581
+ span : Option < & ' a span:: Id > ,
578
582
#[ cfg( feature = "ansi" ) ]
579
583
ansi : bool ,
580
584
}
@@ -585,13 +589,17 @@ where
585
589
N : for < ' writer > FormatFields < ' writer > + ' static ,
586
590
{
587
591
#[ cfg( feature = "ansi" ) ]
588
- pub ( crate ) fn new ( ctx : & ' a FmtContext < ' _ , S , N > , ansi : bool ) -> Self {
589
- Self { ctx, ansi }
592
+ pub ( crate ) fn new (
593
+ ctx : & ' a FmtContext < ' _ , S , N > ,
594
+ span : Option < & ' a span:: Id > ,
595
+ ansi : bool ,
596
+ ) -> Self {
597
+ Self { ctx, ansi, span }
590
598
}
591
599
592
600
#[ cfg( not( feature = "ansi" ) ) ]
593
- pub ( crate ) fn new ( ctx : & ' a FmtContext < ' _ , S , N > ) -> Self {
594
- Self { ctx }
601
+ pub ( crate ) fn new ( ctx : & ' a FmtContext < ' _ , S , N > , span : Option < & ' a span :: Id > ) -> Self {
602
+ Self { ctx, span }
595
603
}
596
604
597
605
fn bold ( & self ) -> Style {
@@ -615,10 +623,19 @@ where
615
623
let bold = self . bold ( ) ;
616
624
let mut seen = false ;
617
625
618
- self . ctx . visit_spans ( |span| {
626
+ let span = self
627
+ . span
628
+ . and_then ( |id| self . ctx . ctx . span ( & id) )
629
+ . or_else ( || self . ctx . ctx . lookup_current ( ) ) ;
630
+
631
+ let scope = span
632
+ . into_iter ( )
633
+ . flat_map ( |span| span. from_root ( ) . chain ( iter:: once ( span) ) ) ;
634
+
635
+ for span in scope {
619
636
seen = true ;
620
- write ! ( f, "{}:" , bold. paint( span. metadata( ) . name( ) ) )
621
- } ) ? ;
637
+ write ! ( f, "{}:" , bold. paint( span. metadata( ) . name( ) ) ) ? ;
638
+ }
622
639
623
640
if seen {
624
641
f. write_char ( ' ' ) ?;
@@ -633,6 +650,7 @@ where
633
650
N : for < ' writer > FormatFields < ' writer > + ' static ,
634
651
{
635
652
ctx : & ' a FmtContext < ' a , S , N > ,
653
+ span : Option < & ' a span:: Id > ,
636
654
#[ cfg( feature = "ansi" ) ]
637
655
ansi : bool ,
638
656
}
@@ -643,13 +661,17 @@ where
643
661
N : for < ' writer > FormatFields < ' writer > + ' static ,
644
662
{
645
663
#[ cfg( feature = "ansi" ) ]
646
- pub ( crate ) fn new ( ctx : & ' a FmtContext < ' a , S , N > , ansi : bool ) -> Self {
647
- Self { ctx, ansi }
664
+ pub ( crate ) fn new (
665
+ ctx : & ' a FmtContext < ' a , S , N > ,
666
+ span : Option < & ' a span:: Id > ,
667
+ ansi : bool ,
668
+ ) -> Self {
669
+ Self { ctx, span, ansi }
648
670
}
649
671
650
672
#[ cfg( not( feature = "ansi" ) ) ]
651
- pub ( crate ) fn new ( ctx : & ' a FmtContext < ' a , S , N > ) -> Self {
652
- Self { ctx }
673
+ pub ( crate ) fn new ( ctx : & ' a FmtContext < ' a , S , N > , span : Option < & ' a span :: Id > ) -> Self {
674
+ Self { ctx, span }
653
675
}
654
676
655
677
fn bold ( & self ) -> Style {
@@ -673,7 +695,16 @@ where
673
695
let bold = self . bold ( ) ;
674
696
let mut seen = false ;
675
697
676
- self . ctx . visit_spans ( |span| {
698
+ let span = self
699
+ . span
700
+ . and_then ( |id| self . ctx . ctx . span ( & id) )
701
+ . or_else ( || self . ctx . ctx . lookup_current ( ) ) ;
702
+
703
+ let scope = span
704
+ . into_iter ( )
705
+ . flat_map ( |span| span. from_root ( ) . chain ( iter:: once ( span) ) ) ;
706
+
707
+ for span in scope {
677
708
write ! ( f, "{}" , bold. paint( span. metadata( ) . name( ) ) ) ?;
678
709
seen = true ;
679
710
@@ -684,8 +715,8 @@ where
684
715
if !fields. is_empty ( ) {
685
716
write ! ( f, "{}{}{}" , bold. paint( "{" ) , fields, bold. paint( "}" ) ) ?;
686
717
}
687
- f. write_char ( ':' )
688
- } ) ? ;
718
+ f. write_char ( ':' ) ? ;
719
+ }
689
720
690
721
if seen {
691
722
f. write_char ( ' ' ) ?;
@@ -928,4 +959,71 @@ mod test {
928
959
actual. as_str( )
929
960
) ;
930
961
}
962
+
963
+ #[ test]
964
+ fn overridden_parents ( ) {
965
+ lazy_static ! {
966
+ static ref BUF : Mutex <Vec <u8 >> = Mutex :: new( vec![ ] ) ;
967
+ }
968
+
969
+ let make_writer = || MockWriter :: new ( & BUF ) ;
970
+ let subscriber = crate :: fmt:: Subscriber :: builder ( )
971
+ . with_writer ( make_writer)
972
+ . with_level ( false )
973
+ . with_ansi ( false )
974
+ . with_timer ( MockTime )
975
+ . finish ( ) ;
976
+
977
+ with_default ( subscriber, || {
978
+ let span1 = tracing:: info_span!( "span1" ) ;
979
+ let span2 = tracing:: info_span!( parent: & span1, "span2" ) ;
980
+ tracing:: info!( parent: & span2, "hello" ) ;
981
+ } ) ;
982
+ let actual = String :: from_utf8 ( BUF . try_lock ( ) . unwrap ( ) . to_vec ( ) ) . unwrap ( ) ;
983
+ assert_eq ! (
984
+ "fake time span1:span2: tracing_subscriber::fmt::format::test: hello\n " ,
985
+ actual. as_str( )
986
+ ) ;
987
+ }
988
+
989
+ #[ test]
990
+ fn overridden_parents_in_scope ( ) {
991
+ lazy_static ! {
992
+ static ref BUF : Mutex <Vec <u8 >> = Mutex :: new( vec![ ] ) ;
993
+ }
994
+
995
+ let make_writer = || MockWriter :: new ( & BUF ) ;
996
+ let subscriber = crate :: fmt:: Subscriber :: builder ( )
997
+ . with_writer ( make_writer)
998
+ . with_level ( false )
999
+ . with_ansi ( false )
1000
+ . with_timer ( MockTime )
1001
+ . finish ( ) ;
1002
+
1003
+ let actual = || {
1004
+ let mut buf = BUF . try_lock ( ) . unwrap ( ) ;
1005
+ let val = String :: from_utf8 ( buf. to_vec ( ) ) . unwrap ( ) ;
1006
+ buf. clear ( ) ;
1007
+ val
1008
+ } ;
1009
+
1010
+ with_default ( subscriber, || {
1011
+ let span1 = tracing:: info_span!( "span1" ) ;
1012
+ let span2 = tracing:: info_span!( parent: & span1, "span2" ) ;
1013
+ let span3 = tracing:: info_span!( "span3" ) ;
1014
+ let _e3 = span3. enter ( ) ;
1015
+
1016
+ tracing:: info!( "hello" ) ;
1017
+ assert_eq ! (
1018
+ "fake time span3: tracing_subscriber::fmt::format::test: hello\n " ,
1019
+ actual( ) . as_str( )
1020
+ ) ;
1021
+
1022
+ tracing:: info!( parent: & span2, "hello" ) ;
1023
+ assert_eq ! (
1024
+ "fake time span1:span2: tracing_subscriber::fmt::format::test: hello\n " ,
1025
+ actual( ) . as_str( )
1026
+ ) ;
1027
+ } ) ;
1028
+ }
931
1029
}
0 commit comments