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
This commit is contained in:
Clement Tsang 2023-12-02 04:53:31 -05:00 committed by GitHub
parent 074b205a82
commit fab86e833a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 5 deletions

11
Cargo.lock generated
View File

@ -830,6 +830,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "num_threads"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "nvml-wrapper" name = "nvml-wrapper"
version = "0.9.0" version = "0.9.0"
@ -1311,6 +1320,8 @@ checksum = "c4a34ab300f2dee6e562c10a046fc05e358b29f9bf92277f30c3c8d82275f6f5"
dependencies = [ dependencies = [
"deranged", "deranged",
"itoa", "itoa",
"libc",
"num_threads",
"powerfmt", "powerfmt",
"serde", "serde",
"time-core", "time-core",

View File

@ -69,7 +69,7 @@ nvidia = ["nvml-wrapper"]
zfs = [] zfs = []
# Including logging for debugging purposes. # 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. # The features we use on deploy. Logging is not included as that is primarily (for now) just for debugging locally.
deploy = ["battery", "gpu", "zfs"] deploy = ["battery", "gpu", "zfs"]

View File

@ -1,16 +1,37 @@
#[cfg(feature = "logging")]
pub static OFFSET: once_cell::sync::Lazy<time::UtcOffset> = 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")] #[cfg(feature = "logging")]
pub fn init_logger( pub fn init_logger(
min_level: log::LevelFilter, debug_file_name: &std::ffi::OsStr, min_level: log::LevelFilter, debug_file_name: &std::ffi::OsStr,
) -> Result<(), fern::InitError> { ) -> Result<(), fern::InitError> {
fern::Dispatch::new() fern::Dispatch::new()
.format(|out, message, record| { .format(|out, message, record| {
// Note we aren't using local time since it only works on single-threaded processes. let offset_time = {
// If that ever does get patched in again, enable the "local-offset" feature. let utc = time::OffsetDateTime::now_utc();
let offset = time::OffsetDateTime::now_utc(); utc.checked_to_offset(*OFFSET).unwrap_or(utc)
};
out.finish(format_args!( out.finish(format_args!(
"{}[{}][{}] {}", "{}[{}][{}] {}",
offset offset_time
.format(&time::macros::format_description!( .format(&time::macros::format_description!(
// The weird "[[[" is because we need to escape a bracket ("[[") to show one "[". // The weird "[[[" is because we need to escape a bracket ("[[") to show one "[".
// See https://time-rs.github.io/book/api/format-description.html // See https://time-rs.github.io/book/api/format-description.html