From 53af9befcc76054471459d216a2ab2d11e81150e Mon Sep 17 00:00:00 2001 From: Sakarias Johansson Date: Sun, 16 Apr 2023 14:52:03 +0200 Subject: =?UTF-8?q?=F0=9F=90=9B=20Fix=20bug=20where=20logs=20would=20not?= =?UTF-8?q?=20flush?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Slog has a problem where if the logs are created in the same scope as std::process::exit is called it will not be able to flush the logs. Moved log creation to another method. The main function does barely anything now. --- racer-tracer/src/main.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/racer-tracer/src/main.rs b/racer-tracer/src/main.rs index 16fe6c4..7069b56 100644 --- a/racer-tracer/src/main.rs +++ b/racer-tracer/src/main.rs @@ -166,22 +166,33 @@ fn create_log(log_file: &Path) -> Result { }) } -fn main() { +// There is a problem with slog where if the log is created in the +// same scope as where a process::exit is called it won't flush +// correctly before exiting. +fn bridge_main(config: Config) -> i32 { let log_file = std::env::temp_dir().join("racer-tracer.log"); let log = create_log(log_file.as_ref()).expect("Expected to be able to create a log"); let term = Terminal::new(log.new(o!("scope" => "terminal"))); terminal::write_term!(term, &format!("Log file: {}", log_file.display())); - match Config::try_from(Args::from_args()) - .and_then(|config| run(config, log.new(o!("scope" => "run")), term)) - { - Err(TracerError::ExitEvent) => {} - Ok(_) => {} + match run(config, log.new(o!("scope" => "run")), term) { + Err(TracerError::ExitEvent) => 0, + Ok(_) => 0, Err(e) => { error!(log, "Error: {}", e); let exit_code = i32::from(e); error!(log, "Exiting with: {}", exit_code); - std::process::exit(exit_code) + exit_code + } + } +} + +fn main() { + match Config::try_from(Args::from_args()).map(bridge_main) { + Ok(ec) => std::process::exit(ec), + Err(e) => { + println!("Failed to parse config file: {}", e); + std::process::exit(0) } } } -- cgit v1.2.3