@@ -233,6 +233,14 @@ where
233
233
}
234
234
}
235
235
236
+ /// Whether to print the time with higher precision.
237
+ pub fn with_higher_precision ( self , higher_precision : bool ) -> Self {
238
+ Self {
239
+ config : self . config . with_higher_precision ( higher_precision) ,
240
+ ..self
241
+ }
242
+ }
243
+
236
244
fn styled ( & self , style : Style , text : impl AsRef < str > ) -> String {
237
245
if self . config . ansi {
238
246
style. paint ( text. as_ref ( ) ) . to_string ( )
@@ -397,6 +405,79 @@ where
397
405
let writer = self . make_writer . make_writer ( ) ;
398
406
bufs. flush_current_buf ( writer)
399
407
}
408
+
409
+ fn get_timestamp < S > ( & self , id : & Id , ctx : & Context < S > , ) -> Option < String >
410
+ where
411
+ S : Subscriber + for < ' span > LookupSpan < ' span > ,
412
+ {
413
+ match ctx. span ( id) {
414
+ // if the event is in a span, get the span's starting point.
415
+ Some ( ctx) => {
416
+ let ext = ctx. extensions ( ) ;
417
+ let data = ext
418
+ . get :: < Data > ( )
419
+ . expect ( "Data cannot be found in extensions" ) ;
420
+
421
+ if self . config . higher_precision {
422
+ Some ( self . format_timestamp_with_decimals ( data. start ) )
423
+ } else {
424
+ Some ( self . format_timestamp ( data. start ) )
425
+ }
426
+ }
427
+ None => None ,
428
+ }
429
+ }
430
+
431
+ fn format_timestamp ( & self , start : std:: time:: Instant ) -> String {
432
+ let elapsed = start. elapsed ( ) ;
433
+ let millis = elapsed. as_millis ( ) ;
434
+ let secs = elapsed. as_secs ( ) ;
435
+
436
+ // Convert elapsed time to appropriate units: ms, s, or m.
437
+ // - Less than 1s : use ms
438
+ // - Less than 1m : use s
439
+ // - 1m and above : use m
440
+ let ( n, unit) = if millis < 1000 {
441
+ ( millis as _ , "ms" )
442
+ } else if secs < 60 {
443
+ ( secs, "s " )
444
+ } else {
445
+ ( secs / 60 , "m " )
446
+ } ;
447
+
448
+ let n = format ! ( "{n:>3}" ) ;
449
+ format ! (
450
+ "{timestamp}{unit} " ,
451
+ timestamp = self . styled( Style :: new( ) . dimmed( ) , n) ,
452
+ unit = self . styled( Style :: new( ) . dimmed( ) , unit) ,
453
+ )
454
+ }
455
+
456
+ fn format_timestamp_with_decimals ( & self , start : std:: time:: Instant ) -> String {
457
+ let elapsed = start. elapsed ( ) ;
458
+ let nanos = elapsed. as_nanos ( ) as f64 ;
459
+ let micros = elapsed. as_micros ( ) as f64 ;
460
+ let millis = elapsed. as_millis ( ) as f64 ;
461
+
462
+ // Convert elapsed time to appropriate units: μs, ms, or s.
463
+ // - Less than 1ms: use μs
464
+ // - Less than 1s : use ms
465
+ // - 1s and above : use s
466
+ let ( n, unit) = if micros < 1000.0 {
467
+ ( nanos / 1000.0 , "μs" )
468
+ } else if millis < 1000.0 {
469
+ ( micros / 1000.0 , "ms" )
470
+ } else {
471
+ ( millis / 1000.0 , "s " )
472
+ } ;
473
+
474
+ let n = format ! ( " {n:.2}" ) ;
475
+ format ! (
476
+ "{timestamp}{unit} " ,
477
+ timestamp = self . styled( Style :: new( ) . dimmed( ) , n) ,
478
+ unit = self . styled( Style :: new( ) . dimmed( ) , unit) ,
479
+ )
480
+ }
400
481
}
401
482
402
483
impl < S , W , FT > Layer < S > for HierarchicalLayer < W , FT >
@@ -475,38 +556,10 @@ where
475
556
476
557
// check if this event occurred in the context of a span.
477
558
// if it has, get the start time of this span.
478
- let start = match span {
479
- Some ( span) => {
480
- // if the event is in a span, get the span's starting point.
481
- let ext = span. extensions ( ) ;
482
- let data = ext
483
- . get :: < Data > ( )
484
- . expect ( "Data cannot be found in extensions" ) ;
485
-
486
- Some ( data. start )
559
+ if let Some ( id) = ctx. current_span ( ) . id ( ) {
560
+ if let Some ( timestamp) = self . get_timestamp ( id, & ctx) {
561
+ write ! ( & mut event_buf, "{}" , timestamp) . expect ( "Unable to write to buffer" ) ;
487
562
}
488
- None => None ,
489
- } ;
490
-
491
- if let Some ( start) = start {
492
- let elapsed = start. elapsed ( ) ;
493
- let millis = elapsed. as_millis ( ) ;
494
- let secs = elapsed. as_secs ( ) ;
495
- let ( n, unit) = if millis < 1000 {
496
- ( millis as _ , "ms" )
497
- } else if secs < 60 {
498
- ( secs, "s " )
499
- } else {
500
- ( secs / 60 , "m " )
501
- } ;
502
- let n = format ! ( "{n:>3}" ) ;
503
- write ! (
504
- & mut event_buf,
505
- "{timestamp}{unit} " ,
506
- timestamp = self . styled( Style :: new( ) . dimmed( ) , n) ,
507
- unit = self . styled( Style :: new( ) . dimmed( ) , unit) ,
508
- )
509
- . expect ( "Unable to write to buffer" ) ;
510
563
}
511
564
512
565
#[ cfg( feature = "tracing-log" ) ]
0 commit comments