From c0c7e7d1c0930aff1dce993b185e7ac793fc53cd Mon Sep 17 00:00:00 2001 From: 1zuna Date: Fri, 27 Dec 2024 03:28:36 +0100 Subject: [PATCH] feat: logs clean up Logs will now clean up when older than 7 days --- src-tauri/src/main.rs | 10 ++++++---- src-tauri/src/utils/sys.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index f9a80b1..e164377 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -16,8 +16,7 @@ * You should have received a copy of the GNU General Public License * along with LiquidLauncher. If not, see . */ - -// #![feature(exit_status_error)] - wait for feature to be stable +#![feature(duration_constructors)] #![cfg_attr( all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows" @@ -65,9 +64,12 @@ static HTTP_CLIENT: Lazy = Lazy::new(|| { pub fn main() -> Result<()> { use tracing_subscriber::{fmt, EnvFilter}; - let log_folder = LAUNCHER_DIRECTORY.data_dir().join("logs"); + let logs = LAUNCHER_DIRECTORY.data_dir().join("logs"); + if let Err(e) = utils::clean_directory(&logs, 7) { + error!("Failed to clear log folder: {:?}", e); + } - let file_appender = tracing_appender::rolling::hourly(log_folder, "launcher.log"); + let file_appender = tracing_appender::rolling::daily(logs, "launcher.log"); let subscriber = tracing_subscriber::registry() .with(EnvFilter::from("liquidlauncher=debug")) diff --git a/src-tauri/src/utils/sys.rs b/src-tauri/src/utils/sys.rs index 9102597..7f7210f 100644 --- a/src-tauri/src/utils/sys.rs +++ b/src-tauri/src/utils/sys.rs @@ -23,6 +23,10 @@ use serde::Deserialize; use std::fmt::Display; use sysinfo::{RefreshKind, System, SystemExt}; +use std::fs; +use std::path::Path; +use std::time::{Duration, SystemTime}; + /// Get the total memory of the system in pub fn sys_memory() -> u64 { let sys = System::new_with_specifics(RefreshKind::new().with_memory()); @@ -148,3 +152,28 @@ impl Display for Architecture { f.write_str(self.get_simple_name().unwrap()) } } + +pub fn clean_directory( + path: &Path, + max_age_days: u64, +) -> Result<()> { + let now = SystemTime::now(); + let max_age = Duration::from_days(max_age_days); + + for entry in fs::read_dir(path)? { + let entry = entry?; + let metadata = entry.metadata()?; + + if !metadata.is_file() { + continue; + } + + if let Ok(modified) = metadata.modified() { + if now.duration_since(modified)? > max_age { + let _ = fs::remove_file(entry.path()); + } + } + } + + Ok(()) +} \ No newline at end of file