1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
use std::{fmt::Write, time::Duration};
use nu_ansi_term::Style;
use crate::styled;
/// A type that can measure and format the current time.
///
/// This trait is used by [HierarchicalLayer] to include a timestamp with each
/// [Event] when it is logged.
///
/// Notable default implementations of this trait are [LocalDateTime] and `()`.
/// The former prints the current time as reported by [time's OffsetDateTime]
/// (note that it requires a `time` feature to be enabled and may panic!
/// make sure to check out the docs for the [LocalDateTime]),
/// and the latter does not print the current time at all.
///
/// Inspired by the [FormatTime] trait from [tracing-subscriber].
///
/// [HierarchicalLayer]: crate::HierarchicalLayer
/// [Event]: tracing_core::Event
/// [time's OffsetDateTime]: time::OffsetDateTime
/// [FormatTime]: tracing_subscriber::fmt::time::FormatTime
/// [tracing-subscriber]: tracing_subscriber
// NB:
// We can't use `tracing_subscriber::fmt::format::Writer`
// since it doesn't have a public constructor.
pub trait FormatTime {
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result;
fn style_timestamp(
&self,
ansi: bool,
elapsed: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Default do-nothing time formatter.
impl FormatTime for () {
fn format_time(&self, _w: &mut impl std::fmt::Write) -> std::fmt::Result {
Ok(())
}
fn style_timestamp(
&self,
_ansi: bool,
_elapsed: Duration,
_w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
Ok(())
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Retrieve and print the current wall-clock time in UTC timezone.
#[cfg(feature = "time")]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct UtcDateTime {
/// Whether to print the time with higher precision.
pub higher_precision: bool,
}
#[cfg(feature = "time")]
impl FormatTime for UtcDateTime {
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result {
let time = time::OffsetDateTime::now_utc();
write!(w, "{} {}", time.date(), time.time())
}
fn style_timestamp(
&self,
ansi: bool,
elapsed: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
style_timestamp(ansi, self.higher_precision, elapsed, w)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Retrieve and print the current wall-clock time.
///
/// # Panics
///
/// Panics if [time crate] cannot determine the local UTC offset.
///
/// [time crate]: time
// NB:
// Can't use `tracing_subscriber::fmt::time::SystemTime` since it uses
// private `datetime` module to format the actual time.
#[cfg(feature = "time")]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct LocalDateTime {
/// Whether to print the time with higher precision.
pub higher_precision: bool,
}
#[cfg(feature = "time")]
impl FormatTime for LocalDateTime {
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result {
let time = time::OffsetDateTime::now_local().expect("time offset cannot be determined");
write!(w, "{}", time)
}
fn style_timestamp(
&self,
ansi: bool,
elapsed: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
style_timestamp(ansi, self.higher_precision, elapsed, w)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Retrieve and print the relative elapsed wall-clock time since an epoch.
///
/// The `Default` implementation for `Uptime` makes the epoch the current time.
// NB: Copy-pasted from `tracing-subscriber::fmt::time::Uptime`.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Uptime {
epoch: std::time::Instant,
/// Whether to print the time with higher precision.
pub higher_precision: bool,
}
impl Default for Uptime {
fn default() -> Self {
Uptime::from(std::time::Instant::now())
}
}
impl From<std::time::Instant> for Uptime {
fn from(epoch: std::time::Instant) -> Self {
Uptime {
epoch,
higher_precision: false,
}
}
}
impl FormatTime for Uptime {
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result {
let e = self.epoch.elapsed();
write!(w, "{:4}.{:06}s", e.as_secs(), e.subsec_micros())
}
fn style_timestamp(
&self,
ansi: bool,
elapsed: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
style_timestamp(ansi, self.higher_precision, elapsed, w)
}
}
fn style_timestamp(
ansi: bool,
higher_precision: bool,
elapsed: Duration,
w: &mut impl Write,
) -> std::fmt::Result {
if higher_precision {
format_timestamp_with_decimals(ansi, elapsed, w)
} else {
format_timestamp(ansi, elapsed, w)
}
}
fn format_timestamp(ansi: bool, elapsed: Duration, w: &mut impl Write) -> std::fmt::Result {
let millis = elapsed.as_millis();
let secs = elapsed.as_secs();
// Convert elapsed time to appropriate units: ms, s, or m.
// - Less than 1s : use ms
// - Less than 1m : use s
// - 1m and above : use m
let (n, unit) = if millis < 1000 {
(millis as _, "ms")
} else if secs < 60 {
(secs, "s ")
} else {
(secs / 60, "m ")
};
let timestamp = format!("{n:>3}");
write_style_timestamp(ansi, timestamp, unit, w)
}
fn format_timestamp_with_decimals(
ansi: bool,
elapsed: Duration,
w: &mut impl Write,
) -> std::fmt::Result {
let secs = elapsed.as_secs_f64();
// Convert elapsed time to appropriate units: μs, ms, or s.
// - Less than 1ms: use μs
// - Less than 1s : use ms
// - 1s and above : use s
let (n, unit) = if secs < 0.001 {
(secs * 1_000_000.0, "μs")
} else if secs < 1.0 {
(secs * 1_000.0, "ms")
} else {
(secs, "s ")
};
let timestamp = format!(" {n:.2}");
write_style_timestamp(ansi, timestamp, unit, w)
}
fn write_style_timestamp(
ansi: bool,
timestamp: String,
unit: &str,
w: &mut impl Write,
) -> std::fmt::Result {
write!(
w,
"{timestamp}{unit}",
timestamp = styled(ansi, Style::new().dimmed(), timestamp),
unit = styled(ansi, Style::new().dimmed(), unit),
)
}
////////////////////////////////////////////////////////////////////////////////////////////////////
impl<'a, F> FormatTime for &'a F
where
F: FormatTime,
{
fn format_time(&self, w: &mut impl std::fmt::Write) -> std::fmt::Result {
F::format_time(self, w)
}
fn style_timestamp(
&self,
ansi: bool,
duration: Duration,
w: &mut impl std::fmt::Write,
) -> std::fmt::Result {
F::style_timestamp(self, ansi, duration, w)
}
}
// NB:
// Can't impl for `fn(&mut impl std::fmt::Write)` since impl trait is not allowed
// outside of function and inherent method return types for now.