[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>>;
}
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
}
}

View File

@@ -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)*);
}}
}

View File

@@ -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<Mutex<()>> = 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<Mutex<()>> = 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<Mutex<()>> = OnceLock::new();
let _guard = ENV_LOCK.get_or_init(|| Mutex::new(())).lock().unwrap();
// Ensure all off
unsafe {

View File

@@ -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) => {