Skip to content

Commit

Permalink
other: add back local time in debug logs (#1346)
Browse files Browse the repository at this point in the history
* other: add back local time in debug logs

This still has a UTC fallback.

* cleanup and some warnings
  • Loading branch information
ClementTsang authored Dec 2, 2023
1 parent 074b205 commit fab86e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
29 changes: 25 additions & 4 deletions src/utils/logging.rs
Original file line number Diff line number Diff line change
@@ -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")]
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
Expand Down

0 comments on commit fab86e8

Please sign in to comment.