[refactor] centralize logging logic with log_with_level macro; clean up imports and optimize code organization across modules

This commit is contained in:
2025-08-08 20:16:44 +02:00
parent fe98bd36b6
commit a0dcc239aa
4 changed files with 49 additions and 31 deletions

View File

@@ -41,18 +41,15 @@ pub trait TranscribeBackend {
) -> Result<Vec<OutputEntry>>; ) -> Result<Vec<OutputEntry>>;
} }
fn check_lib(names: &[&str]) -> bool { fn check_lib(_names: &[&str]) -> bool {
#[cfg(test)] #[cfg(test)]
{ {
// During unit tests, avoid touching system libs to prevent loader crashes in CI. // 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 false
} }
#[cfg(not(test))] #[cfg(not(test))]
{ {
// Disabled runtime dlopen probing to avoid loader instability; rely on environment overrides. // Disabled runtime dlopen probing to avoid loader instability; rely on environment overrides.
let _ = names;
false false
} }
} }

View File

@@ -178,27 +178,42 @@ macro_rules! elog {
eprintln!("ERROR: {}", format!($($arg)*)); 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_export]
macro_rules! wlog { macro_rules! log_with_level {
($($arg:tt)*) => {{ ($label:expr, $min_lvl:expr, $always:expr, $($arg:tt)*) => {{
eprintln!("WARN: {}", format!($($arg)*)); let should_print = if $always {
}} true
} } else if let Some(minv) = $min_lvl {
/// Log an informational line to stderr unless quiet mode is enabled. !$crate::is_quiet() && $crate::verbose_level() >= minv
#[macro_export] } else {
macro_rules! ilog { !$crate::is_quiet()
($($arg:tt)*) => {{ };
if !$crate::is_quiet() { if should_print {
eprintln!("INFO: {}", format!($($arg)*)); 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). /// Log a debug/trace line when verbose level is at least the given level (u8).
#[macro_export] #[macro_export]
macro_rules! dlog { macro_rules! dlog {
($lvl:expr, $($arg:tt)*) => {{ ($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)*);
}} }}
} }

View File

@@ -761,6 +761,9 @@ mod tests {
use polyscribe::format_srt_time; use polyscribe::format_srt_time;
use std::env as std_env; use std::env as std_env;
use std::fs; use std::fs;
use std::sync::{Mutex, OnceLock};
static ENV_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
#[test] #[test]
fn test_cli_name_polyscribe() { fn test_cli_name_polyscribe() {
@@ -935,8 +938,6 @@ mod tests {
#[test] #[test]
fn test_backend_auto_order_prefers_cuda_then_hip_then_vulkan_then_cpu() { fn test_backend_auto_order_prefers_cuda_then_hip_then_vulkan_then_cpu() {
use std::sync::{Mutex, OnceLock};
static ENV_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
let _guard = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap(); let _guard = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap();
// Clear overrides // Clear overrides
unsafe { unsafe {
@@ -976,8 +977,6 @@ mod tests {
#[test] #[test]
fn test_backend_explicit_missing_errors() { fn test_backend_explicit_missing_errors() {
use std::sync::{Mutex, OnceLock};
static ENV_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
let _guard = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap(); let _guard = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap();
// Ensure all off // Ensure all off
unsafe { unsafe {

View File

@@ -735,6 +735,20 @@ fn download_one_model(client: &Client, models_dir: &Path, entry: &ModelEntry) ->
Ok(()) 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. /// Update locally stored models by re-downloading when size or hash does not match online metadata.
pub fn update_local_models() -> Result<()> { pub fn update_local_models() -> Result<()> {
let models_dir_buf = crate::models_dir_path(); 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)?; download_one_model(&client, models_dir, remote)?;
} else if remote.size > 0 { } else if remote.size > 0 {
match std::fs::metadata(&path) { 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) => { Ok(md) => {
qlog!( if qlog_size_comparison(&fname, md.len(), remote.size) {
"{} size {} differs from remote {}. Updating...", continue;
fname, }
md.len(),
remote.size
);
download_one_model(&client, models_dir, remote)?; download_one_model(&client, models_dir, remote)?;
} }
Err(e) => { Err(e) => {