From 1c9fa73165f0bc97255dd10e64ad4c9b76ad1d02 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 12 Jan 2022 20:53:03 -0500 Subject: Update time crate to 0.3.5 Due to unsoundness issues (c.f., time-rs/time#380 and time-rs/time#293), determining the local timezone can only happen while single-threaded. Determine the timezone early in startup and apply the offset to the UTC timestamp before formatting. --- alacritty/src/logging.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'alacritty/src') diff --git a/alacritty/src/logging.rs b/alacritty/src/logging.rs index 56ed4ab5..0ed37220 100644 --- a/alacritty/src/logging.rs +++ b/alacritty/src/logging.rs @@ -13,6 +13,8 @@ use std::{env, process}; use glutin::event_loop::EventLoopProxy; use log::{self, Level, LevelFilter}; +use time::macros::format_description; +use time::{OffsetDateTime, UtcOffset}; use crate::cli::Options; use crate::event::{Event, EventType}; @@ -41,6 +43,7 @@ pub struct Logger { logfile: Mutex, stdout: Mutex>, event_proxy: Mutex>, + tz_offset: UtcOffset, } impl Logger { @@ -48,7 +51,12 @@ impl Logger { let logfile = Mutex::new(OnDemandLogFile::new()); let stdout = Mutex::new(LineWriter::new(io::stdout())); - Logger { logfile, stdout, event_proxy: Mutex::new(event_proxy) } + Logger { + logfile, + stdout, + event_proxy: Mutex::new(event_proxy), + tz_offset: UtcOffset::current_local_offset().expect("local timezone offset"), + } } fn file_path(&self) -> Option { @@ -108,7 +116,7 @@ impl log::Log for Logger { } // Create log message for the given `record` and `target`. - let message = create_log_message(record, target); + let message = create_log_message(record, target, self.tz_offset); if let Ok(mut logfile) = self.logfile.lock() { // Write to logfile. @@ -127,8 +135,11 @@ impl log::Log for Logger { fn flush(&self) {} } -fn create_log_message(record: &log::Record<'_>, target: &str) -> String { - let now = time::strftime("%F %T.%f", &time::now()).unwrap(); +fn create_log_message(record: &log::Record<'_>, target: &str, tz_offset: UtcOffset) -> String { + let time_format = format_description!( + "[year]-[month]-[day] [hour repr:24]:[minute]:[second].[subsecond digits:9]" + ); + let now = OffsetDateTime::now_utc().to_offset(tz_offset).format(time_format).unwrap(); let mut message = format!("[{}] [{:<5}] [{}] ", now, record.level(), target); // Alignment for the lines after the first new line character in the payload. We don't deal -- cgit