From d34a63816046f0c87db9bb3f1f3587209f58af2f Mon Sep 17 00:00:00 2001 From: Raphiiko Date: Wed, 20 Nov 2024 19:41:46 +0100 Subject: [PATCH] Attempt at fixing time related crash on log cleanup --- CHANGELOG.md | 4 ++ src-core/src/commands/log_utils.rs | 91 ++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb70a5e3..210fc4de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - GPU acceleration for SteamVR overlays (Community contribution by [BenjaminZehowlt](https://github.com/BenjaminZehowlt)) +### Fixed + +- Possible crash when encountering time issues on log cleanup + ## [1.14.6] ### Fixed diff --git a/src-core/src/commands/log_utils.rs b/src-core/src/commands/log_utils.rs index b7479fc0..23a09d83 100644 --- a/src-core/src/commands/log_utils.rs +++ b/src-core/src/commands/log_utils.rs @@ -1,5 +1,5 @@ -use log::info; -use std::path::PathBuf; +use log::{error, info}; +use std::{path::PathBuf, time::SystemTime}; use tokio::sync::Mutex; lazy_static! { @@ -31,6 +31,74 @@ pub async fn clear_log_files() { info!("[Core] Deleted {} log file(s)", logs_deleted); } +pub async fn print_file_debug_data(path: PathBuf) { + error!(""); + error!("Path: {:?}", path); + let now = SystemTime::now(); + error!("System Time: {:?}", now); + // Print now as unix time + match now.duration_since(SystemTime::UNIX_EPOCH) { + Ok(duration) => { + error!("System Time [UNIX]: {}", duration.as_secs()) + } + Err(e) => { + error!("Error getting unix time for now: {}", e); + } + }; + match std::fs::metadata(&path) { + Ok(metadata) => { + // Print modified time + match metadata.modified() { + Ok(modified) => { + error!("Modified Time: {:?}", modified); + match modified.elapsed() { + Ok(elapsed) => { + error!("Elapsed Time: {:?}", elapsed); + } + Err(e) => { + error!("Error getting elapsed time for log file: {}", e); + } + }; + } + Err(e) => { + error!("Error getting modified time for log file: {}", e); + } + }; + // Print elapsed time + match metadata.modified() { + Ok(modified) => match modified.elapsed() { + Ok(elapsed) => { + error!("Elapsed Time [UNIX]: {}", elapsed.as_secs()); + } + Err(e) => { + error!("Error getting elapsed time for log file: {}", e); + } + }, + Err(e) => { + error!("Error getting modified time for log file: {}", e); + } + }; + // Print modified as unix time + match metadata + .modified() + .unwrap() + .duration_since(SystemTime::UNIX_EPOCH) + { + Ok(duration) => { + error!("Modified Time [UNIX]: {}", duration.as_secs()) + } + Err(e) => { + error!("Error getting unix time for modified: {}", e); + } + }; + } + Err(e) => { + error!("Error getting metadata for log file: {}", e); + } + }; + error!(""); +} + pub async fn clean_log_files() { info!("[Core] Cleaning old and oversized log files..."); let guard = LOG_DIR.lock().await; @@ -42,10 +110,23 @@ pub async fn clean_log_files() { if path.is_file() { let metadata = std::fs::metadata(&path).unwrap(); let too_large = metadata.len() > 10 * 1024 * 1024; // 10 MB - let too_old = - metadata.modified().unwrap().elapsed().unwrap().as_secs() > 60 * 60 * 24 * 30; // 30 Days + let too_old = match metadata.modified() { + Ok(modified) => match modified.elapsed() { + Ok(elapsed) => elapsed.as_secs() > 60 * 60 * 24 * 30, // 30 Days + Err(e) => { + error!("Error getting elapsed time for log file: {}", e); + print_file_debug_data(path.clone()).await; + false + } + }, + Err(e) => { + error!("Error getting modified time for log file: {}", e); + print_file_debug_data(path.clone()).await; + false + } + }; if too_large || too_old { - if std::fs::remove_file(path).is_ok() { + if std::fs::remove_file(path.clone()).is_ok() { logs_deleted += 1; } }