From a0dcc239aac3960c33a645c8dac80f04a894dc86 Mon Sep 17 00:00:00 2001 From: vikingowl Date: Fri, 8 Aug 2025 20:16:44 +0200 Subject: [PATCH] [refactor] centralize logging logic with `log_with_level` macro; clean up imports and optimize code organization across modules --- src/backend.rs | 5 +---- src/lib.rs | 41 ++++++++++++++++++++++++++++------------- src/main.rs | 7 +++---- src/models.rs | 27 +++++++++++++++++---------- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/backend.rs b/src/backend.rs index 5e45bdb..778fb2b 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -41,18 +41,15 @@ pub trait TranscribeBackend { ) -> Result>; } -fn check_lib(names: &[&str]) -> bool { +fn check_lib(_names: &[&str]) -> bool { #[cfg(test)] { // During unit tests, avoid touching system libs to prevent loader crashes in CI. - // Mark parameter as used to silence warnings in test builds. - let _ = names; false } #[cfg(not(test))] { // Disabled runtime dlopen probing to avoid loader instability; rely on environment overrides. - let _ = names; false } } diff --git a/src/lib.rs b/src/lib.rs index e7d7f27..d8cfeb9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -178,27 +178,42 @@ macro_rules! elog { eprintln!("ERROR: {}", format!($($arg)*)); }} } -/// Log a warning to stderr (should generally be printed even in quiet mode). +/// Internal helper macro used by other logging macros to centralize the +/// common behavior: build formatted message, check quiet/verbose flags, +/// and print to stderr with a label. #[macro_export] -macro_rules! wlog { - ($($arg:tt)*) => {{ - eprintln!("WARN: {}", format!($($arg)*)); - }} -} -/// Log an informational line to stderr unless quiet mode is enabled. -#[macro_export] -macro_rules! ilog { - ($($arg:tt)*) => {{ - if !$crate::is_quiet() { - eprintln!("INFO: {}", format!($($arg)*)); +macro_rules! log_with_level { + ($label:expr, $min_lvl:expr, $always:expr, $($arg:tt)*) => {{ + let should_print = if $always { + true + } else if let Some(minv) = $min_lvl { + !$crate::is_quiet() && $crate::verbose_level() >= minv + } else { + !$crate::is_quiet() + }; + if should_print { + eprintln!("{}: {}", $label, format!($($arg)*)); } }} } + +/// Log a warning to stderr (printed even in quiet mode). +#[macro_export] +macro_rules! wlog { + ($($arg:tt)*) => {{ $crate::log_with_level!("WARN", None, true, $($arg)*); }} +} + +/// Log an informational line to stderr unless quiet mode is enabled. +#[macro_export] +macro_rules! ilog { + ($($arg:tt)*) => {{ $crate::log_with_level!("INFO", None, false, $($arg)*); }} +} + /// Log a debug/trace line when verbose level is at least the given level (u8). #[macro_export] macro_rules! dlog { ($lvl:expr, $($arg:tt)*) => {{ - if !$crate::is_quiet() && $crate::verbose_level() >= $lvl { eprintln!("DEBUG{}: {}", $lvl, format!($($arg)*)); } + $crate::log_with_level!(&format!("DEBUG{}", &$lvl), Some($lvl), false, $($arg)*); }} } diff --git a/src/main.rs b/src/main.rs index 50b7717..cb5e62f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -761,6 +761,9 @@ mod tests { use polyscribe::format_srt_time; use std::env as std_env; use std::fs; + use std::sync::{Mutex, OnceLock}; + + static ENV_LOCK: OnceLock> = OnceLock::new(); #[test] fn test_cli_name_polyscribe() { @@ -935,8 +938,6 @@ mod tests { #[test] fn test_backend_auto_order_prefers_cuda_then_hip_then_vulkan_then_cpu() { - use std::sync::{Mutex, OnceLock}; - static ENV_LOCK: OnceLock> = OnceLock::new(); let _guard = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap(); // Clear overrides unsafe { @@ -976,8 +977,6 @@ mod tests { #[test] fn test_backend_explicit_missing_errors() { - use std::sync::{Mutex, OnceLock}; - static ENV_LOCK: OnceLock> = OnceLock::new(); let _guard = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap(); // Ensure all off unsafe { diff --git a/src/models.rs b/src/models.rs index 553f0ee..54379cf 100644 --- a/src/models.rs +++ b/src/models.rs @@ -735,6 +735,20 @@ fn download_one_model(client: &Client, models_dir: &Path, entry: &ModelEntry) -> Ok(()) } +// Update locally stored models by re-downloading when size or hash does not match online metadata. +fn qlog_size_comparison(fname: &str, local: u64, remote: u64) -> bool { + if local == remote { + qlog!("{} appears up-to-date by size ({}).", fname, remote); + true + } else { + qlog!( + "{} size {} differs from remote {}. Updating...", + fname, local, remote + ); + false + } +} + /// Update locally stored models by re-downloading when size or hash does not match online metadata. pub fn update_local_models() -> Result<()> { let models_dir_buf = crate::models_dir_path(); @@ -815,17 +829,10 @@ pub fn update_local_models() -> Result<()> { download_one_model(&client, models_dir, remote)?; } else if remote.size > 0 { match std::fs::metadata(&path) { - Ok(md) if md.len() == remote.size => { - qlog!("{} appears up-to-date by size ({}).", fname, remote.size); - continue; - } Ok(md) => { - qlog!( - "{} size {} differs from remote {}. Updating...", - fname, - md.len(), - remote.size - ); + if qlog_size_comparison(&fname, md.len(), remote.size) { + continue; + } download_one_model(&client, models_dir, remote)?; } Err(e) => {