Fixed logging to file on Windows without console window.

This commit is contained in:
Revertron
2021-10-25 17:22:50 +02:00
parent 3a6932e5b2
commit e29c58bec3
+19 -13
View File
@@ -36,12 +36,12 @@ const LOG_TARGET_MAIN: &str = "alfis::Main";
fn main() { fn main() {
#[allow(unused_assignments)] #[allow(unused_assignments)]
let mut use_logger = true; let mut console_attached = true;
// When linked with the windows subsystem windows won't automatically attach // 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. // to the console of the parent process, so we do it explicitly. This fails silently if the parent has no console.
#[cfg(windows)] #[cfg(windows)]
unsafe { unsafe {
use_logger = AttachConsole(ATTACH_PARENT_PROCESS) != 0; console_attached = AttachConsole(ATTACH_PARENT_PROCESS) != 0;
#[cfg(feature = "webgui")] #[cfg(feature = "webgui")]
winapi::um::shellscalingapi::SetProcessDpiAwareness(2); winapi::um::shellscalingapi::SetProcessDpiAwareness(2);
} }
@@ -110,9 +110,7 @@ fn main() {
Some(path) => path Some(path) => path
}; };
if use_logger { setup_logger(&opt_matches, console_attached);
setup_logger(&opt_matches);
}
if let Some(status) = opt_matches.opt_str("s") { if let Some(status) = opt_matches.opt_str("s") {
register(move |_, event| { register(move |_, event| {
match event { match event {
@@ -244,7 +242,7 @@ fn main() {
} }
/// Sets up logger in accordance with command line options /// 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; let mut level = LevelFilter::Info;
if opt_matches.opt_present("d") || env::var(ALFIS_DEBUG).is_ok() { if opt_matches.opt_present("d") || env::var(ALFIS_DEBUG).is_ok() {
level = LevelFilter::Trace; level = LevelFilter::Trace;
@@ -262,8 +260,10 @@ fn setup_logger(opt_matches: &Matches) {
.build(); .build();
match opt_matches.opt_str("l") { match opt_matches.opt_str("l") {
None => { None => {
if let Err(e) = TermLogger::init(level, config, TerminalMode::Stdout, ColorChoice::Auto) { if console_attached {
println!("Unable to initialize logger!\n{}", e); if let Err(e) = TermLogger::init(level, config, TerminalMode::Stdout, ColorChoice::Auto) {
println!("Unable to initialize logger!\n{}", e);
}
} }
} }
Some(path) => { Some(path) => {
@@ -277,11 +277,17 @@ fn setup_logger(opt_matches: &Matches) {
exit(1); exit(1);
} }
}; };
CombinedLogger::init(vec![ if console_attached {
TermLogger::new(level, config.clone(), TerminalMode::Stdout, ColorChoice::Auto), CombinedLogger::init(vec![
WriteLogger::new(level, config, file), TermLogger::new(level, config.clone(), TerminalMode::Stdout, ColorChoice::Auto),
]) WriteLogger::new(level, config, file),
.unwrap(); ])
.unwrap();
} else {
if let Err(e) = WriteLogger::init(level, config, file) {
println!("Unable to initialize logger!\n{}", e);
}
}
} }
} }
} }