Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some logging utilities to capture whisper.cpp output #112

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ repository = "https://github.com/tazz4843/whisper-rs"

[dependencies]
whisper-rs-sys = { path = "sys", version = "0.8" }
log = { version = "0.4", optional = true }
tracing = { version = "0.1", optional = true }

[dev-dependencies]
hound = "3.5.0"
Expand All @@ -30,6 +32,8 @@ openblas = ["whisper-rs-sys/openblas"]
metal = ["whisper-rs-sys/metal", "_gpu"]
_gpu = []
test-with-tiny-model = []
whisper-cpp-log = ["dep:log"]
whisper-cpp-tracing = ["dep:tracing"]

[package.metadata.docs.rs]
features = ["simd"]
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ mod whisper_ctx;
mod whisper_grammar;
mod whisper_params;
mod whisper_state;
#[cfg(feature = "whisper-cpp-log")]
mod whisper_sys_log;
#[cfg(feature = "whisper-cpp-tracing")]
mod whisper_sys_tracing;

static LOG_TRAMPOLINE_INSTALL: Once = Once::new();

pub use error::WhisperError;
pub use standalone::*;
use std::sync::Once;
pub use utilities::*;
pub use whisper_ctx::WhisperContext;
pub use whisper_ctx::WhisperContextParameters;
Expand Down
42 changes: 42 additions & 0 deletions src/whisper_sys_log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use log::{debug, error, info, warn};
use whisper_rs_sys::ggml_log_level;

unsafe extern "C" fn whisper_cpp_log_trampoline(
level: ggml_log_level,
text: *const std::os::raw::c_char,
_: *mut std::os::raw::c_void, // user_data
) {
if text.is_null() {
error!("whisper_cpp_log_trampoline: text is nullptr");
}

// SAFETY: we must trust whisper.cpp that it will not pass us a string that does not satisfy
// from_ptr's requirements.
let log_str = unsafe { std::ffi::CStr::from_ptr(text) }.to_string_lossy();
// whisper.cpp gives newlines at the end of its log messages, so we trim them
let trimmed = log_str.trim();

match level {
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_DEBUG => debug!("{}", trimmed),
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_INFO => info!("{}", trimmed),
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_WARN => warn!("{}", trimmed),
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_ERROR => error!("{}", trimmed),
_ => {
warn!(
"whisper_cpp_log_trampoline: unknown log level {}: message: {}",
level, trimmed
)
}
}
}

/// Shortcut utility to redirect all whisper.cpp logging to the `log` crate.
///
/// Filter for logs from the `whisper-rs` crate to see all log output from whisper.cpp.
///
/// You should only call this once (subsequent calls have no ill effect).
pub fn install_whisper_log_trampoline() {
crate::LOG_TRAMPOLINE_INSTALL.call_once(|| unsafe {
whisper_rs_sys::whisper_log_set(Some(whisper_cpp_log_trampoline), std::ptr::null_mut())
});
}
42 changes: 42 additions & 0 deletions src/whisper_sys_tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use tracing::{debug, error, info, warn};
use whisper_rs_sys::ggml_log_level;

unsafe extern "C" fn whisper_cpp_tracing_trampoline(
level: ggml_log_level,
text: *const std::os::raw::c_char,
_: *mut std::os::raw::c_void, // user_data
) {
if text.is_null() {
error!("whisper_cpp_tracing_trampoline: text is nullptr");
}

// SAFETY: we must trust whisper.cpp that it will not pass us a string that does not satisfy
// from_ptr's requirements.
let log_str = unsafe { std::ffi::CStr::from_ptr(text) }.to_string_lossy();
// whisper.cpp gives newlines at the end of its log messages, so we trim them
let trimmed = log_str.trim();

match level {
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_DEBUG => debug!("{}", trimmed),
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_INFO => info!("{}", trimmed),
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_WARN => warn!("{}", trimmed),
whisper_rs_sys::ggml_log_level_GGML_LOG_LEVEL_ERROR => error!("{}", trimmed),
_ => {
warn!(
"whisper_cpp_tracing_trampoline: unknown log level {}: message: {}",
level, trimmed
)
}
}
}

/// Shortcut utility to redirect all whisper.cpp logging to the `tracing` crate.
///
/// Filter for logs from the `whisper-rs` crate to see all log output from whisper.cpp.
///
/// You should only call this once (subsequent calls have no effect).
pub fn install_whisper_tracing_trampoline() {
crate::LOG_TRAMPOLINE_INSTALL.call_once(|| unsafe {
whisper_rs_sys::whisper_log_set(Some(whisper_cpp_tracing_trampoline), std::ptr::null_mut())
});
}