Make logging generic across backends and simplify the code

This commit is contained in:
Niko 2025-02-10 14:02:19 -07:00
parent d3fe24a512
commit 1db502bc1a
No known key found for this signature in database
7 changed files with 253 additions and 100 deletions

73
src/common_logging.rs Normal file
View file

@ -0,0 +1,73 @@
macro_rules! generic_error {
($($expr:tt)*) => {
#[cfg(feature = "log_backend")]
log::error!($($expr)*);
#[cfg(feature = "tracing_backend")]
tracing::error!($($expr)*);
};
}
macro_rules! generic_warn {
($($expr:tt)*) => {
#[cfg(feature = "log_backend")]
log::warn!($($expr)*);
#[cfg(feature = "tracing_backend")]
tracing::warn!($($expr)*);
}
}
macro_rules! generic_info {
($($expr:tt)*) => {
#[cfg(feature = "log_backend")]
log::info!($($expr)*);
#[cfg(feature = "tracing_backend")]
tracing::info!($($expr)*);
}
}
macro_rules! generic_debug {
($($expr:tt)*) => {
#[cfg(feature = "log_backend")]
log::debug!($($expr)*);
#[cfg(feature = "tracing_backend")]
tracing::debug!($($expr)*);
}
}
macro_rules! generic_trace {
($($expr:tt)*) => {
#[cfg(feature = "log_backend")]
log::trace!($($expr)*);
#[cfg(feature = "tracing_backend")]
tracing::trace!($($expr)*);
}
}
use whisper_rs_sys::ggml_log_level;
pub(crate) use {generic_debug, generic_error, generic_info, generic_trace, generic_warn};
// Unsigned integer type on most platforms is 32 bit, niche platforms that whisper.cpp
// likely doesn't even support would use 16 bit and would still fit
#[repr(u32)]
pub(crate) enum GGMLLogLevel {
None = whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_NONE,
Info = whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_INFO,
Warn = whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_WARN,
Error = whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_ERROR,
Debug = whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_DEBUG,
Cont = whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_CONT,
Unknown(ggml_log_level),
}
impl From<ggml_log_level> for GGMLLogLevel {
fn from(level: ggml_log_level) -> Self {
match level {
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_NONE => GGMLLogLevel::None,
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_INFO => GGMLLogLevel::Info,
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_WARN => GGMLLogLevel::Warn,
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_ERROR => GGMLLogLevel::Error,
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_DEBUG => GGMLLogLevel::Debug,
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_CONT => GGMLLogLevel::Cont,
other => GGMLLogLevel::Unknown(other),
}
}
}