From 27088fab2875fa6927476cc1ff44ddde43390dfa Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 17 Mar 2022 16:52:35 -0700 Subject: [PATCH] on windows, log to C:\Users\USER\.local/share/wezterm/gui-log-PID.txt refs: #1576 --- env-bootstrap/Cargo.toml | 2 +- env-bootstrap/src/ringlog.rs | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/env-bootstrap/Cargo.toml b/env-bootstrap/Cargo.toml index b956a7881..a033a25c9 100644 --- a/env-bootstrap/Cargo.toml +++ b/env-bootstrap/Cargo.toml @@ -11,6 +11,7 @@ chrono = {version="0.4", features=["unstable-locales"]} config = { path = "../config" } dirs-next = "2.0" lazy_static = "1.4" +libc = "0.2" log = "0.4" pretty_env_logger = "0.4" @@ -19,6 +20,5 @@ winapi = "0.3" [target.'cfg(target_os = "macos")'.dependencies] cocoa = "0.20" -libc = "0.2" objc = "0.2" diff --git a/env-bootstrap/src/ringlog.rs b/env-bootstrap/src/ringlog.rs index 12dfb898c..de61055ec 100644 --- a/env-bootstrap/src/ringlog.rs +++ b/env-bootstrap/src/ringlog.rs @@ -171,13 +171,26 @@ pub fn get_entries() -> Vec { fn setup_pretty() -> (LevelFilter, Option>) { #[cfg(windows)] { + use std::os::windows::io::IntoRawHandle; use winapi::um::winbase::STD_ERROR_HANDLE; - // Working around - // wherein Rust 1.56 panics in the case that stderr is NULL. - // That can legitimately occur in a Windows subsystem executable. - // We detect that here and avoid initializing the pretty env logger. + + // When launched as a GUI, stderr is null. + // Let's redirect it to a log file to aid in debugging. if unsafe { winapi::um::processenv::GetStdHandle(STD_ERROR_HANDLE).is_null() } { - return (LevelFilter::Info, None); + let log_file_name = + config::RUNTIME_DIR.join(format!("gui-log-{}.txt", unsafe { libc::getpid() })); + if let Ok(file) = std::fs::OpenOptions::new() + .append(true) + .create(true) + .open(log_file_name) + { + let handle = file.into_raw_handle(); + unsafe { winapi::um::processenv::SetStdHandle(STD_ERROR_HANDLE, handle) }; + } else { + // If we fail to init a log file, skip setting up a logger to + // avoid a panic when built with 1.56 + return (LevelFilter::Info, None); + } } }