Skip to content

Commit 8cab4a7

Browse files
authored
Merge pull request #94 from ivan-ochc/precise-timestamp
Add precise timestamp with nanos
2 parents 211d566 + 1e406b0 commit 8cab4a7

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/fmt.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use std::time::SystemTime;
4444

4545
use termcolor::{self, ColorSpec, ColorChoice, Buffer, BufferWriter, WriteColor};
4646
use atty;
47-
use humantime::format_rfc3339_seconds;
47+
use humantime::{format_rfc3339_seconds, format_rfc3339_nanos};
4848

4949
/// A formatter to write logs into.
5050
///
@@ -148,6 +148,10 @@ pub struct StyledValue<'a, T> {
148148
/// [`Formatter`]: struct.Formatter.html
149149
pub struct Timestamp(SystemTime);
150150

151+
/// An [RFC3339] formatted timestamp with nanos
152+
#[derive(Debug)]
153+
pub struct PreciseTimestamp(SystemTime);
154+
151155
/// Log target, either `stdout` or `stderr`.
152156
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
153157
pub enum Target {
@@ -468,6 +472,11 @@ impl Formatter {
468472
Timestamp(SystemTime::now())
469473
}
470474

475+
/// Get a [`PreciseTimestamp`] for the current date and time in UTC with nanos.
476+
pub fn precise_timestamp(&self) -> PreciseTimestamp {
477+
PreciseTimestamp(SystemTime::now())
478+
}
479+
471480
pub(crate) fn print(&self, writer: &Writer) -> io::Result<()> {
472481
writer.inner.print(&self.buf.borrow())
473482
}
@@ -575,6 +584,12 @@ impl fmt::Display for Timestamp {
575584
}
576585
}
577586

587+
impl fmt::Display for PreciseTimestamp {
588+
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
589+
format_rfc3339_nanos(self.0).fmt(f)
590+
}
591+
}
592+
578593
// The `Color` type is copied from https://github.com/BurntSushi/ripgrep/tree/master/termcolor
579594

580595
/// The set of available colors for the terminal foreground/background.

src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ struct Format {
268268
default_format_timestamp: bool,
269269
default_format_module_path: bool,
270270
default_format_level: bool,
271+
default_format_timestamp_nanos: bool,
271272
custom_format: Option<Box<Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send>>,
272273
}
273274

@@ -277,6 +278,7 @@ impl Default for Format {
277278
default_format_timestamp: true,
278279
default_format_module_path: true,
279280
default_format_level: true,
281+
default_format_timestamp_nanos: false,
280282
custom_format: None,
281283
}
282284
}
@@ -312,8 +314,13 @@ impl Format {
312314
};
313315

314316
let write_ts = if self.default_format_timestamp {
315-
let ts = buf.timestamp();
316-
write!(buf, "{}: ", ts)
317+
if self.default_format_timestamp_nanos {
318+
let ts_nanos = buf.precise_timestamp();
319+
write!(buf, "{}: ", ts_nanos)
320+
} else {
321+
let ts = buf.timestamp();
322+
write!(buf, "{}: ", ts)
323+
}
317324
} else {
318325
Ok(())
319326
};
@@ -529,6 +536,12 @@ impl Builder {
529536
self
530537
}
531538

539+
/// Whether or not to write the timestamp with nanos.
540+
pub fn default_format_timestamp_nanos(&mut self, write: bool) -> &mut Self {
541+
self.format.default_format_timestamp_nanos = write;
542+
self
543+
}
544+
532545
/// Adds a directive to the filter for a specific module.
533546
///
534547
/// # Examples

0 commit comments

Comments
 (0)