fix(core): fix bug that caused multiple processes to overwrite logs

This commit is contained in:
Federico Terzi 2021-05-16 21:39:24 +02:00
parent befcbd984e
commit 6eb3fdfcf3
3 changed files with 17 additions and 8 deletions

View File

@ -51,8 +51,8 @@ impl Default for CliModule {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum LogMode { pub enum LogMode {
Read, Read,
Write, AppendOnly,
Append, CleanAndAppend,
} }
pub struct CliModuleArgs { pub struct CliModuleArgs {

View File

@ -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() let mut log_file = OpenOptions::new()
.read(true) .read(true)
.write(true) .write(!read_only)
.create(true) .create(true)
.truncate(truncate) .append(true)
.append(append)
.open(path)?; .open(path)?;
let mut lock = self.output.lock().expect("unable to obtain FileProxy lock"); let mut lock = self.output.lock().expect("unable to obtain FileProxy lock");

View File

@ -17,6 +17,9 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
// This is needed to avoid showing a console window when starting espanso on Windows
// TODO #![windows_subsystem = "windows"]
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
@ -309,8 +312,8 @@ fn main() {
log_proxy log_proxy
.set_output_file( .set_output_file(
&paths.runtime.join(LOG_FILE_NAME), &paths.runtime.join(LOG_FILE_NAME),
handler.log_mode == LogMode::Write, handler.log_mode == LogMode::Read,
handler.log_mode == LogMode::Append, handler.log_mode == LogMode::CleanAndAppend,
) )
.expect("unable to set up log output file"); .expect("unable to set up log output file");
} }