From 7a380f0081f3ca11506062909b1a1d08f4457621 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Thu, 19 Oct 2023 19:52:18 +0000 Subject: [PATCH] Handle failure of current_local_offset without panicking --- CHANGELOG.md | 4 ++++ src/main.rs | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 509b93f..e8cd3c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Condure Changelog ================= +Unreleased + + * If local time offset can't be determined, log timestamps as UTC instead of panicking. + v. 1.10.0 (2023-06-29) * Add support for outgoing connections. diff --git a/src/main.rs b/src/main.rs index 92a8ca5..39e3058 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,7 +44,7 @@ const PRIVATE_SUBNETS: &[&str] = &[ ]; struct SimpleLogger { - local_offset: UtcOffset, + local_offset: Option, } impl log::Log for SimpleLogger { @@ -57,7 +57,7 @@ impl log::Log for SimpleLogger { return; } - let now = OffsetDateTime::now_utc().to_offset(self.local_offset); + let now = OffsetDateTime::now_utc().to_offset(self.local_offset.unwrap_or(UtcOffset::UTC)); let format = format_description!( "[year]-[month]-[day] [hour]:[minute]:[second].[subsecond digits:3]" @@ -90,6 +90,12 @@ impl log::Log for SimpleLogger { fn flush(&self) {} } +impl SimpleLogger { + pub(crate) fn offset_known(&self) -> bool { + self.local_offset.is_some() + } +} + static mut LOGGER: mem::MaybeUninit = mem::MaybeUninit::uninit(); fn get_simple_logger() -> &'static SimpleLogger { @@ -97,8 +103,7 @@ fn get_simple_logger() -> &'static SimpleLogger { unsafe { INIT.call_once(|| { - let local_offset = - UtcOffset::current_local_offset().expect("failed to get local time offset"); + let local_offset = UtcOffset::current_local_offset().ok(); LOGGER.write(SimpleLogger { local_offset }); }); @@ -425,7 +430,8 @@ fn main() { ) .get_matches(); - log::set_logger(get_simple_logger()).unwrap(); + let logger = get_simple_logger(); + log::set_logger(logger).unwrap(); log::set_max_level(LevelFilter::Info); @@ -450,6 +456,10 @@ fn main() { log::set_max_level(level); + if !logger.offset_known() { + log::warn!("Failed to determine local time offset. Log timestamps will be in UTC."); + } + if *matches.get_one("sizes").unwrap() { for (name, size) in condure::app::App::sizes() { println!("{}: {} bytes", name, size);