@@ -6,7 +6,7 @@ use format::{write_span_mode, Buffers, ColorLevel, Config, FmtEvent, SpanMode};
66
77use nu_ansi_term:: { Color , Style } ;
88use std:: {
9- fmt:: { self , Write as _ } ,
9+ fmt:: { self , Write } ,
1010 io:: { self , IsTerminal } ,
1111 iter:: Fuse ,
1212 mem,
@@ -267,69 +267,64 @@ where
267267 Ok ( ( ) )
268268 }
269269
270- /// If `span_retrace` ensures that `new_span` is properly printed before an event
270+ /// Ensures that `new_span` and all its ancestors are properly printed before an event
271271 fn write_retrace_span < ' a , S > (
272272 & self ,
273273 new_span : & SpanRef < ' a , S > ,
274274 bufs : & mut Buffers ,
275275 ctx : & ' a Context < S > ,
276+ pre_open : bool ,
276277 ) where
277278 S : Subscriber + for < ' new_span > LookupSpan < ' new_span > ,
278279 {
279- let should_write = if self . config . deferred_spans {
280- if let Some ( data) = new_span. extensions_mut ( ) . get_mut :: < Data > ( ) {
281- !data. written
282- } else {
283- false
284- }
285- } else {
286- false
287- } ;
288-
289280 // Also handle deferred spans along with retrace since deferred spans may need to print
290281 // multiple spans at once as a whole tree can be deferred
291- if self . config . span_retrace || should_write {
292- let old_span_id = bufs. current_span . replace ( ( new_span. id ( ) ) . clone ( ) ) ;
293- let old_span_id = old_span_id. as_ref ( ) ;
294-
295- if Some ( & new_span. id ( ) ) != old_span_id {
296- let old_span = old_span_id. as_ref ( ) . and_then ( |v| ctx. span ( v) ) ;
297- let old_path = old_span. as_ref ( ) . map ( scope_path) . into_iter ( ) . flatten ( ) ;
298-
299- let new_path = scope_path ( new_span) ;
300-
301- // Print the path from the common base of the two spans
302- let new_path = DifferenceIter :: new ( old_path, new_path, |v| v. id ( ) ) ;
303-
304- for ( i, span) in new_path. enumerate ( ) {
305- // Mark traversed spans as *written*
306- let was_written = if let Some ( data) = span. extensions_mut ( ) . get_mut :: < Data > ( ) {
307- mem:: replace ( & mut data. written , true )
308- } else {
309- // `on_new_span` was not called, before
310- // Consider if this should panic instead, which is *technically* correct but is
311- // bad behavior for a logging layer in production.
312- false
313- } ;
314-
315- // Print the previous span before entering a new deferred or retraced span
316- if i == 0 && self . config . verbose_entry {
317- if let Some ( parent) = & span. parent ( ) {
318- self . write_span_info ( parent, bufs, SpanMode :: PreOpen ) ;
319- }
282+ //
283+ // If a another event occurs right after a previous event in the same span, this will
284+ // simply print nothing since the path to the common lowest ancestor is empty
285+ // if self.config.span_retrace || self.config.deferred_spans {
286+ let old_span_id = bufs. current_span . replace ( ( new_span. id ( ) ) . clone ( ) ) ;
287+ let old_span_id = old_span_id. as_ref ( ) ;
288+ let new_span_id = new_span. id ( ) ;
289+
290+ if Some ( & new_span_id) != old_span_id {
291+ let old_span = old_span_id. as_ref ( ) . and_then ( |v| ctx. span ( v) ) ;
292+ let old_path = old_span. as_ref ( ) . map ( scope_path) . into_iter ( ) . flatten ( ) ;
293+
294+ let new_path = scope_path ( new_span) ;
295+
296+ // Print the path from the common base of the two spans
297+ let new_path = DifferenceIter :: new ( old_path, new_path, |v| v. id ( ) ) ;
298+
299+ for ( i, span) in new_path. enumerate ( ) {
300+ // Mark traversed spans as *written*
301+ let was_written = if let Some ( data) = span. extensions_mut ( ) . get_mut :: < Data > ( ) {
302+ mem:: replace ( & mut data. written , true )
303+ } else {
304+ // `on_new_span` was not called, before
305+ // Consider if this should panic instead, which is *technically* correct but is
306+ // bad behavior for a logging layer in production.
307+ false
308+ } ;
309+
310+ // Print the parent of the first span
311+ let mut verbose = false ;
312+ if i == 0 && pre_open {
313+ if let Some ( span) = span. parent ( ) {
314+ verbose = true ;
315+ self . write_span_info ( & span, bufs, SpanMode :: PreOpen ) ;
320316 }
321- let verbose = self . config . verbose_entry && i == 0 ;
322-
323- self . write_span_info (
324- & span,
325- bufs,
326- if was_written {
327- SpanMode :: Retrace { verbose }
328- } else {
329- SpanMode :: Open { verbose }
330- } ,
331- )
332317 }
318+
319+ self . write_span_info (
320+ & span,
321+ bufs,
322+ if was_written {
323+ SpanMode :: Retrace { verbose }
324+ } else {
325+ SpanMode :: Open { verbose }
326+ } ,
327+ )
333328 }
334329 }
335330 }
@@ -491,22 +486,24 @@ where
491486
492487 let bufs = & mut * self . bufs . lock ( ) . unwrap ( ) ;
493488
494- // Store the most recently entered span
495- bufs. current_span = Some ( span. id ( ) ) ;
496-
497- if self . config . verbose_entry {
498- if let Some ( span) = span. parent ( ) {
499- self . write_span_info ( & span, bufs, SpanMode :: PreOpen ) ;
489+ if self . config . span_retrace {
490+ self . write_retrace_span ( & span, bufs, & ctx, self . config . verbose_entry ) ;
491+ } else {
492+ if self . config . verbose_entry {
493+ if let Some ( span) = span. parent ( ) {
494+ self . write_span_info ( & span, bufs, SpanMode :: PreOpen ) ;
495+ }
500496 }
497+ // Store the most recently entered span
498+ bufs. current_span = Some ( span. id ( ) ) ;
499+ self . write_span_info (
500+ & span,
501+ bufs,
502+ SpanMode :: Open {
503+ verbose : self . config . verbose_entry ,
504+ } ,
505+ ) ;
501506 }
502-
503- self . write_span_info (
504- & span,
505- bufs,
506- SpanMode :: Open {
507- verbose : self . config . verbose_entry ,
508- } ,
509- ) ;
510507 }
511508
512509 fn on_event ( & self , event : & Event < ' _ > , ctx : Context < S > ) {
@@ -518,7 +515,9 @@ where
518515 let bufs = & mut * guard;
519516
520517 if let Some ( new_span) = & span {
521- self . write_retrace_span ( new_span, bufs, & ctx) ;
518+ if self . config . span_retrace || self . config . deferred_spans {
519+ self . write_retrace_span ( new_span, bufs, & ctx, self . config . verbose_entry ) ;
520+ }
522521 }
523522
524523 let mut event_buf = & mut bufs. current_buf ;
0 commit comments