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.

Use lazy_static to determine this early in startup and apply the offset
to the UTC timestamp before formatting.
  • Loading branch information
jamessan committed Jan 12, 2022
1 parent fd7573d commit fc6b7f3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
22 changes: 18 additions & 4 deletions Cargo.lock

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

3 changes: 2 additions & 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 All @@ -35,6 +35,7 @@ libc = "0.2"
unicode-width = "0.1"
bitflags = "1"
dirs = "3.0.1"
lazy_static = "1.4.0"

[build-dependencies]
gl_generator = "0.14.0"
Expand Down
17 changes: 16 additions & 1 deletion 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 All @@ -24,12 +26,20 @@ const ALACRITTY_LOG_ENV: &str = "ALACRITTY_LOG";
const ALLOWED_TARGETS: [&str; 4] =
["alacritty_terminal", "alacritty_config_derive", "alacritty", "crossfont"];

lazy_static! {
static ref LOCAL_TZ: UtcOffset = UtcOffset::current_local_offset().unwrap();
}

pub fn initialize(
options: &Options,
event_proxy: EventLoopProxy<Event>,
) -> Result<Option<PathBuf>, log::SetLoggerError> {
log::set_max_level(options.log_level());

// LOCAL_TZ must be initialized before spawning any threads, otherwise the call will fail due
// to unsoundness of get/setenv in multi-threaded applications.
let _ = *LOCAL_TZ;

let logger = Logger::new(event_proxy);
let path = logger.file_path();
log::set_boxed_logger(Box::new(logger))?;
Expand Down Expand Up @@ -128,7 +138,12 @@ impl log::Log for Logger {
}

fn create_log_message(record: &log::Record<'_>, target: &str) -> String {
let now = time::strftime("%F %T.%f", &time::now()).unwrap();
let now = OffsetDateTime::now_utc()
.to_offset(*LOCAL_TZ)
.format(format_description!(
"[year]-[month]-[day] [hour repr:24]:[minute]:[second].[subsecond digits:9]Z"
))
.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
3 changes: 3 additions & 0 deletions alacritty/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use log::info;
#[cfg(windows)]
use winapi::um::wincon::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS};

#[macro_use]
extern crate lazy_static;

use alacritty_terminal::tty;

mod cli;
Expand Down

0 comments on commit fc6b7f3

Please sign in to comment.