From e29c58bec3deb13af72b4550cd8afc21fd32209d Mon Sep 17 00:00:00 2001 From: Revertron Date: Mon, 25 Oct 2021 17:22:50 +0200 Subject: [PATCH] Fixed logging to file on Windows without console window. --- src/main.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index f143b0b..5172c9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,12 +36,12 @@ const LOG_TARGET_MAIN: &str = "alfis::Main"; fn main() { #[allow(unused_assignments)] - let mut use_logger = true; + let mut console_attached = true; // When linked with the windows subsystem windows won't automatically attach // to the console of the parent process, so we do it explicitly. This fails silently if the parent has no console. #[cfg(windows)] unsafe { - use_logger = AttachConsole(ATTACH_PARENT_PROCESS) != 0; + console_attached = AttachConsole(ATTACH_PARENT_PROCESS) != 0; #[cfg(feature = "webgui")] winapi::um::shellscalingapi::SetProcessDpiAwareness(2); } @@ -110,9 +110,7 @@ fn main() { Some(path) => path }; - if use_logger { - setup_logger(&opt_matches); - } + setup_logger(&opt_matches, console_attached); if let Some(status) = opt_matches.opt_str("s") { register(move |_, event| { match event { @@ -244,7 +242,7 @@ fn main() { } /// Sets up logger in accordance with command line options -fn setup_logger(opt_matches: &Matches) { +fn setup_logger(opt_matches: &Matches, console_attached: bool) { let mut level = LevelFilter::Info; if opt_matches.opt_present("d") || env::var(ALFIS_DEBUG).is_ok() { level = LevelFilter::Trace; @@ -262,8 +260,10 @@ fn setup_logger(opt_matches: &Matches) { .build(); match opt_matches.opt_str("l") { None => { - if let Err(e) = TermLogger::init(level, config, TerminalMode::Stdout, ColorChoice::Auto) { - println!("Unable to initialize logger!\n{}", e); + if console_attached { + if let Err(e) = TermLogger::init(level, config, TerminalMode::Stdout, ColorChoice::Auto) { + println!("Unable to initialize logger!\n{}", e); + } } } Some(path) => { @@ -277,11 +277,17 @@ fn setup_logger(opt_matches: &Matches) { exit(1); } }; - CombinedLogger::init(vec![ - TermLogger::new(level, config.clone(), TerminalMode::Stdout, ColorChoice::Auto), - WriteLogger::new(level, config, file), - ]) - .unwrap(); + if console_attached { + CombinedLogger::init(vec![ + TermLogger::new(level, config.clone(), TerminalMode::Stdout, ColorChoice::Auto), + WriteLogger::new(level, config, file), + ]) + .unwrap(); + } else { + if let Err(e) = WriteLogger::init(level, config, file) { + println!("Unable to initialize logger!\n{}", e); + } + } } } }