|
| 1 | +/// A type that can measure and format the current time. |
| 2 | +/// |
| 3 | +/// This trait is used by [HierarchicalLayer] to include a timestamp with each |
| 4 | +/// [Event] when it is logged. |
| 5 | +/// |
| 6 | +/// Notable default implementations of this trait are [OffsetDateTime] and `()`. |
| 7 | +/// The former prints the current time as reported by [time's OffsetDateTime] |
| 8 | +/// (note that it may panic! make sure to check out the docs for the [OffsetDateTime]), |
| 9 | +/// and the latter does not print the current time at all. |
| 10 | +/// |
| 11 | +/// Inspired by the [FormatTime] trait from [tracing-subscriber]. |
| 12 | +/// |
| 13 | +/// [HierarchicalLayer]: crate::HierarchicalLayer |
| 14 | +/// [Event]: tracing_core::Event |
| 15 | +/// [time's OffsetDateTime]: time::OffsetDateTime |
| 16 | +/// [FormatTime]: tracing_subscriber::fmt::time::FormatTime |
| 17 | +/// [tracing-subscriber]: tracing_subscriber |
| 18 | +// NB: |
| 19 | +// We can't use `tracing_subscriber::fmt::format::Writer` |
| 20 | +// since it doesn't have a public constructor. |
| 21 | +pub trait FormatTime { |
| 22 | + fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result; |
| 23 | +} |
| 24 | + |
| 25 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 26 | + |
| 27 | +/// Default do-nothing time formatter. |
| 28 | +impl FormatTime for () { |
| 29 | + fn format_time(&self, _w: &mut impl std::fmt::Write) -> std::fmt::Result { |
| 30 | + Ok(()) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 35 | + |
| 36 | +/// Retrieve and print the current wall-clock time. |
| 37 | +/// |
| 38 | +/// # Panics |
| 39 | +/// |
| 40 | +/// Panics if [time crate] cannot determine the local UTC offset. |
| 41 | +/// |
| 42 | +/// [time crate]: time |
| 43 | +// NB: |
| 44 | +// Can't use `tracing_subscriber::fmt::time::SystemTime` since it uses |
| 45 | +// private `datetime` module to format the actual time. |
| 46 | +#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] |
| 47 | +pub struct OffsetDateTime; |
| 48 | + |
| 49 | +impl FormatTime for OffsetDateTime { |
| 50 | + fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result { |
| 51 | + let time = time::OffsetDateTime::now_local().expect("time offset cannot be determined"); |
| 52 | + write!(w, "{}", time) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 57 | + |
| 58 | +/// Retrieve and print the relative elapsed wall-clock time since an epoch. |
| 59 | +/// |
| 60 | +/// The `Default` implementation for `Uptime` makes the epoch the current time. |
| 61 | +// NB: Copy-pasted from `tracing-subscriber::fmt::time::Uptime`. |
| 62 | +#[derive(Debug, Clone, Copy, Eq, PartialEq)] |
| 63 | +pub struct Uptime { |
| 64 | + epoch: std::time::Instant, |
| 65 | +} |
| 66 | + |
| 67 | +impl Default for Uptime { |
| 68 | + fn default() -> Self { |
| 69 | + Uptime { |
| 70 | + epoch: std::time::Instant::now(), |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl From<std::time::Instant> for Uptime { |
| 76 | + fn from(epoch: std::time::Instant) -> Self { |
| 77 | + Uptime { epoch } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl FormatTime for Uptime { |
| 82 | + fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result { |
| 83 | + let e = self.epoch.elapsed(); |
| 84 | + write!(w, "{:4}.{:09}s", e.as_secs(), e.subsec_nanos()) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 89 | + |
| 90 | +impl<'a, F> FormatTime for &'a F |
| 91 | +where |
| 92 | + F: FormatTime, |
| 93 | +{ |
| 94 | + fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result { |
| 95 | + (*self).format_time(w) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// NB: |
| 100 | +// Can't impl for `fn(&mut impl std::fmt::Write)` since impl trait is not allowed |
| 101 | +// outside of function and inherent method return types for now. |
0 commit comments