Skip to content

Commit 2cd77d4

Browse files
committed
Add Configuration for Higher Time Precision with Decimals
Resolve merge conflicts in src/lib.rs and src/format.rs during rebase
1 parent 8aa51bc commit 2cd77d4

File tree

2 files changed

+94
-31
lines changed

2 files changed

+94
-31
lines changed

src/format.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ pub struct Config {
6060
pub deferred_spans: bool,
6161
/// Print a label of the span mode (open/close etc).
6262
pub span_modes: bool,
63+
/// Whether to print the time with higher precision.
64+
pub higher_precision: bool,
6365
}
6466

6567
impl Config {
@@ -138,6 +140,13 @@ impl Config {
138140
}
139141
}
140142

143+
pub fn with_higher_precision(self, higher_precision: bool) -> Self {
144+
Self {
145+
higher_precision,
146+
..self
147+
}
148+
}
149+
141150
pub(crate) fn prefix(&self) -> String {
142151
let mut buf = String::new();
143152
if self.render_thread_ids {
@@ -177,6 +186,7 @@ impl Default for Config {
177186
bracketed_fields: false,
178187
deferred_spans: false,
179188
span_modes: false,
189+
higher_precision: false,
180190
}
181191
}
182192
}

src/lib.rs

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ where
233233
}
234234
}
235235

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+
236244
fn styled(&self, style: Style, text: impl AsRef<str>) -> String {
237245
if self.config.ansi {
238246
style.paint(text.as_ref()).to_string()
@@ -397,6 +405,79 @@ where
397405
let writer = self.make_writer.make_writer();
398406
bufs.flush_current_buf(writer)
399407
}
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+
}
400481
}
401482

402483
impl<S, W, FT> Layer<S> for HierarchicalLayer<W, FT>
@@ -475,38 +556,10 @@ where
475556

476557
// check if this event occurred in the context of a span.
477558
// 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");
487562
}
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");
510563
}
511564

512565
#[cfg(feature = "tracing-log")]

0 commit comments

Comments
 (0)