From 8ae57e85fca3e86354ebb003a3671ae4294d2c68 Mon Sep 17 00:00:00 2001 From: Reactor Scram Date: Fri, 2 Feb 2024 11:28:01 -0600 Subject: [PATCH] fix(windows): fix counting / exporting / clearing log files (#3535) Closes #3531 I refactored it so that all the log-related files pull their path from one function, so hopefully they won't skew in the future. The path could still be wrong though. A smoke test might be able to catch that: #3534 --- .../src-tauri/src/client/logging.rs | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/rust/windows-client/src-tauri/src/client/logging.rs b/rust/windows-client/src-tauri/src/client/logging.rs index 9fa091afe..fb6fc8113 100644 --- a/rust/windows-client/src-tauri/src/client/logging.rs +++ b/rust/windows-client/src-tauri/src/client/logging.rs @@ -33,12 +33,9 @@ pub(crate) enum Error { SetLogger(#[from] tracing_log::log_tracer::SetLoggerError), } -/// Set up logs for the first time. +/// Set up logs after the process has started pub(crate) fn setup(log_filter: &str) -> Result { - let log_path = app_local_data_dir() - .map_err(|_| Error::CantFindLocalAppDataFolder)? - .join("data") - .join("logs"); + let log_path = log_path()?; std::fs::create_dir_all(&log_path).map_err(Error::CreateDirAll)?; let (layer, logger) = file_logger::layer(&log_path); @@ -83,7 +80,7 @@ pub(crate) async fn count_logs() -> StdResult { /// This includes the current log file, so we won't write any more logs to disk /// until the file rolls over or the app restarts. pub(crate) async fn clear_logs_inner(managed: &Managed) -> Result<()> { - let mut dir = tokio::fs::read_dir("logs").await?; + let mut dir = tokio::fs::read_dir(log_path()?).await?; while let Some(entry) = dir.next_entry().await? { tokio::fs::remove_file(entry.path()).await?; } @@ -132,7 +129,7 @@ pub(crate) async fn export_logs_to(path: PathBuf, stem: PathBuf) -> Result<()> { let mut zip = zip::ZipWriter::new(f); // All files will have the same modified time. Doing otherwise seems to be difficult let options = zip::write::FileOptions::default(); - for entry in fs::read_dir("logs")? { + for entry in fs::read_dir(log_path()?)? { let entry = entry?; let Some(path) = stem.join(entry.file_name()).to_str().map(|x| x.to_owned()) else { bail!("log filename isn't valid Unicode") @@ -152,7 +149,7 @@ pub(crate) async fn export_logs_to(path: PathBuf, stem: PathBuf) -> Result<()> { /// Count log files and their sizes pub(crate) async fn count_logs_inner() -> Result { - let mut dir = tokio::fs::read_dir("logs").await?; + let mut dir = tokio::fs::read_dir(log_path()?).await?; let mut file_count = FileCount::default(); while let Some(entry) = dir.next_entry().await? { @@ -163,3 +160,14 @@ pub(crate) async fn count_logs_inner() -> Result { Ok(file_count) } + +/// Returns the well-known log path +/// +/// e.g. %LOCALAPPDATA%/dev.firezone.client/data/logs/ +pub(crate) fn log_path() -> Result { + let path = app_local_data_dir() + .map_err(|_| Error::CantFindLocalAppDataFolder)? + .join("data") + .join("logs"); + Ok(path) +}