From fab86e833a89b2227d052f80eb1bf250a68eb60e Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Sat, 2 Dec 2023 04:53:31 -0500 Subject: other: add back local time in debug logs (#1346) * other: add back local time in debug logs This still has a UTC fallback. * cleanup and some warnings --- Cargo.lock | 11 +++++++++++ Cargo.toml | 2 +- src/utils/logging.rs | 29 +++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8a92f30..a860cae3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -830,6 +830,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "nvml-wrapper" version = "0.9.0" @@ -1311,6 +1320,8 @@ checksum = "c4a34ab300f2dee6e562c10a046fc05e358b29f9bf92277f30c3c8d82275f6f5" dependencies = [ "deranged", "itoa", + "libc", + "num_threads", "powerfmt", "serde", "time-core", diff --git a/Cargo.toml b/Cargo.toml index 07fafc3f..30ca689a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,7 @@ nvidia = ["nvml-wrapper"] zfs = [] # Including logging for debugging purposes. -logging = ["fern", "log"] +logging = ["fern", "log", "time/local-offset"] # The features we use on deploy. Logging is not included as that is primarily (for now) just for debugging locally. deploy = ["battery", "gpu", "zfs"] diff --git a/src/utils/logging.rs b/src/utils/logging.rs index 312c909f..3b405ad7 100644 --- a/src/utils/logging.rs +++ b/src/utils/logging.rs @@ -1,16 +1,37 @@ +#[cfg(feature = "logging")] +pub static OFFSET: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { + use time::util::local_offset::Soundness; + + // SAFETY: We only invoke this once, quickly, and it should be invoked in a single-thread context. + // We also should only ever hit this logging at all in a debug context which is generally fine, + // release builds should have this logging disabled entirely for now. + unsafe { + // XXX: If we ever DO add general logging as a release feature, evaluate this again and whether this is + // something we want enabled in release builds! What might be safe is falling back to the non-set-soundness + // mode when specifically using certain feature flags (e.g. dev-logging feature enables this behaviour). + + time::util::local_offset::set_soundness(Soundness::Unsound); + let res = time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC); + time::util::local_offset::set_soundness(Soundness::Sound); + + res + } +}); + #[cfg(feature = "logging")] pub fn init_logger( min_level: log::LevelFilter, debug_file_name: &std::ffi::OsStr, ) -> Result<(), fern::InitError> { fern::Dispatch::new() .format(|out, message, record| { - // Note we aren't using local time since it only works on single-threaded processes. - // If that ever does get patched in again, enable the "local-offset" feature. - let offset = time::OffsetDateTime::now_utc(); + let offset_time = { + let utc = time::OffsetDateTime::now_utc(); + utc.checked_to_offset(*OFFSET).unwrap_or(utc) + }; out.finish(format_args!( "{}[{}][{}] {}", - offset + offset_time .format(&time::macros::format_description!( // The weird "[[[" is because we need to escape a bracket ("[[") to show one "[". // See https://time-rs.github.io/book/api/format-description.html -- cgit v1.2.3