Skip to content

Commit

Permalink
Update time crate to 0.3.5
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jamessan authored Jan 13, 2022
1 parent fd7573d commit 1c9fa73
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
21 changes: 17 additions & 4 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 alacritty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ version = "0.1.0"
[dependencies]
clap = { version = "3.0.0", features = ["derive"] }
log = { version = "0.4", features = ["std", "serde"] }
time = "0.1.40"
time = { version = "0.3.5", features = ["formatting", "local-offset", "macros"] }
fnv = "1"
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.8"
Expand Down
19 changes: 15 additions & 4 deletions alacritty/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -41,14 +43,20 @@ pub struct Logger {
logfile: Mutex<OnDemandLogFile>,
stdout: Mutex<LineWriter<Stdout>>,
event_proxy: Mutex<EventLoopProxy<Event>>,
tz_offset: UtcOffset,
}

impl Logger {
fn new(event_proxy: EventLoopProxy<Event>) -> Self {
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<PathBuf> {
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 1c9fa73

Please sign in to comment.