From b1e2bee52af001227831d88474c32571224df6a5 Mon Sep 17 00:00:00 2001 From: Joe Date: Sun, 8 Mar 2026 15:55:45 -0500 Subject: [PATCH] feat: add asynchronous log compression with thread priority adjustment Implemented asynchronous log compression in the logging initialization process. A new function, `lower_thread_priority`, adjusts the thread priority to below normal for the compression task, improving overall application performance during logging operations. --- src/logging.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/logging.rs b/src/logging.rs index f834b05c..339a2ae1 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -92,7 +92,11 @@ pub fn init_logging( match std::fs::create_dir_all(&log_dir) { Ok(()) => { - compress_old_logs(&log_dir); + let compress_dir = log_dir.clone(); + std::thread::spawn(move || { + lower_thread_priority(); + compress_old_logs(&compress_dir); + }); let log_filename = session_log_filename(command_name); let log_path = log_dir.join(&log_filename); @@ -152,6 +156,26 @@ pub fn init_logging( } } +fn lower_thread_priority() { + #[cfg(windows)] + unsafe { + extern "system" { + fn GetCurrentThread() -> isize; + fn SetThreadPriority(handle: isize, priority: i32) -> i32; + } + const THREAD_PRIORITY_BELOW_NORMAL: i32 = -1; + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL); + } + + #[cfg(unix)] + unsafe { + extern "C" { + fn nice(inc: i32) -> i32; + } + nice(10); + } +} + fn compress_old_logs(log_dir: &Path) { let now_secs = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH)