From 6eb3fdfcf333be13362c758263fb0233ca71506e Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 16 May 2021 21:39:24 +0200 Subject: [PATCH] fix(core): fix bug that caused multiple processes to overwrite logs --- espanso/src/cli/mod.rs | 4 ++-- espanso/src/logging/mod.rs | 14 ++++++++++---- espanso/src/main.rs | 7 +++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/espanso/src/cli/mod.rs b/espanso/src/cli/mod.rs index 4c0af1f..c1da747 100644 --- a/espanso/src/cli/mod.rs +++ b/espanso/src/cli/mod.rs @@ -51,8 +51,8 @@ impl Default for CliModule { #[derive(Debug, PartialEq)] pub enum LogMode { Read, - Write, - Append, + AppendOnly, + CleanAndAppend, } pub struct CliModuleArgs { diff --git a/espanso/src/logging/mod.rs b/espanso/src/logging/mod.rs index 05f5bb2..bbab677 100644 --- a/espanso/src/logging/mod.rs +++ b/espanso/src/logging/mod.rs @@ -46,13 +46,19 @@ impl FileProxy { } } - pub fn set_output_file(&self, path: &Path, truncate: bool, append: bool) -> Result<()> { + pub fn set_output_file(&self, path: &Path, read_only: bool, create_new: bool) -> Result<()> { + // Remove previous log, if present + if create_new && !read_only { + if path.is_file() { + std::fs::remove_file(path)?; + } + } + let mut log_file = OpenOptions::new() .read(true) - .write(true) + .write(!read_only) .create(true) - .truncate(truncate) - .append(append) + .append(true) .open(path)?; let mut lock = self.output.lock().expect("unable to obtain FileProxy lock"); diff --git a/espanso/src/main.rs b/espanso/src/main.rs index 5eca592..2d8c2f4 100644 --- a/espanso/src/main.rs +++ b/espanso/src/main.rs @@ -17,6 +17,9 @@ * along with espanso. If not, see . */ +// This is needed to avoid showing a console window when starting espanso on Windows +// TODO #![windows_subsystem = "windows"] + #[macro_use] extern crate lazy_static; @@ -309,8 +312,8 @@ fn main() { log_proxy .set_output_file( &paths.runtime.join(LOG_FILE_NAME), - handler.log_mode == LogMode::Write, - handler.log_mode == LogMode::Append, + handler.log_mode == LogMode::Read, + handler.log_mode == LogMode::CleanAndAppend, ) .expect("unable to set up log output file"); }