-
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from tazz4843/update-logging
Make logging generic across backends and simplify logging code
- Loading branch information
Showing
8 changed files
with
257 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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 | ||
#[cfg_attr(any(not(windows), target_env = "gnu"), repr(u32))] | ||
// Of course Windows thinks it's a special little shit and | ||
// picks a signed integer for an unsigned type | ||
#[cfg_attr(all(windows, not(target_env = "gnu")), repr(i32))] | ||
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::common_logging::{ | ||
generic_debug, generic_error, generic_info, generic_trace, generic_warn, GGMLLogLevel, | ||
}; | ||
use core::ffi::{c_char, c_void}; | ||
use std::borrow::Cow; | ||
use std::ffi::CStr; | ||
use std::sync::Once; | ||
use whisper_rs_sys::ggml_log_level; | ||
|
||
static GGML_LOG_TRAMPOLINE_INSTALL: Once = Once::new(); | ||
pub(crate) fn install_ggml_logging_hook() { | ||
GGML_LOG_TRAMPOLINE_INSTALL.call_once(|| unsafe { | ||
whisper_rs_sys::ggml_log_set(Some(ggml_logging_trampoline), std::ptr::null_mut()) | ||
}); | ||
} | ||
|
||
unsafe extern "C" fn ggml_logging_trampoline( | ||
level: ggml_log_level, | ||
text: *const c_char, | ||
_: *mut c_void, // user_data | ||
) { | ||
if text.is_null() { | ||
generic_error!("ggml_logging_trampoline: text is nullptr"); | ||
} | ||
let level = GGMLLogLevel::from(level); | ||
|
||
// SAFETY: we must trust ggml that it will not pass us a string that does not satisfy | ||
// from_ptr's requirements. | ||
let log_str = unsafe { CStr::from_ptr(text) }.to_string_lossy(); | ||
|
||
ggml_logging_trampoline_safe(level, log_str) | ||
} | ||
|
||
// this code essentially compiles down to a noop if neither feature is enabled | ||
#[cfg_attr( | ||
not(any(feature = "log_backend", feature = "tracing_backend")), | ||
allow(unused_variables) | ||
)] | ||
fn ggml_logging_trampoline_safe(level: GGMLLogLevel, text: Cow<str>) { | ||
match level { | ||
GGMLLogLevel::None => { | ||
// no clue what to do here, trace it? | ||
generic_trace!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Info => { | ||
generic_info!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Warn => { | ||
generic_warn!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Error => { | ||
generic_error!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Debug => { | ||
generic_debug!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Cont => { | ||
// this means continue previous log | ||
// storing state to do this is a massive pain so it's just a lot easier to not | ||
// plus as far as i can tell it's not actually *used* anywhere | ||
// ggml splits at 128 chars and doesn't actually change the kind of log | ||
// so technically this is unused | ||
generic_trace!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Unknown(level) => { | ||
generic_warn!( | ||
"ggml_logging_trampoline: unknown log level {}: message: {}", | ||
level, | ||
text.trim() | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::common_logging::{ | ||
generic_debug, generic_error, generic_info, generic_trace, generic_warn, GGMLLogLevel, | ||
}; | ||
use core::ffi::{c_char, c_void}; | ||
use std::borrow::Cow; | ||
use std::ffi::CStr; | ||
use std::sync::Once; | ||
use whisper_rs_sys::ggml_log_level; | ||
|
||
static WHISPER_LOG_TRAMPOLINE_INSTALL: Once = Once::new(); | ||
pub(crate) fn install_whisper_logging_hook() { | ||
WHISPER_LOG_TRAMPOLINE_INSTALL.call_once(|| unsafe { | ||
whisper_rs_sys::whisper_log_set(Some(whisper_logging_trampoline), std::ptr::null_mut()) | ||
}); | ||
} | ||
|
||
unsafe extern "C" fn whisper_logging_trampoline( | ||
level: ggml_log_level, | ||
text: *const c_char, | ||
_: *mut c_void, // user_data | ||
) { | ||
if text.is_null() { | ||
generic_error!("whisper_logging_trampoline: text is nullptr"); | ||
} | ||
let level = GGMLLogLevel::from(level); | ||
|
||
// 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 { CStr::from_ptr(text) }.to_string_lossy(); | ||
|
||
whisper_logging_trampoline_safe(level, log_str) | ||
} | ||
|
||
// this code essentially compiles down to a noop if neither feature is enabled | ||
#[cfg_attr( | ||
not(any(feature = "log_backend", feature = "tracing_backend")), | ||
allow(unused_variables) | ||
)] | ||
fn whisper_logging_trampoline_safe(level: GGMLLogLevel, text: Cow<str>) { | ||
match level { | ||
GGMLLogLevel::None => { | ||
// no clue what to do here, trace it? | ||
generic_trace!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Info => { | ||
generic_info!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Warn => { | ||
generic_warn!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Error => { | ||
generic_error!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Debug => { | ||
generic_debug!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Cont => { | ||
// this means continue previous log | ||
// storing state to do this is a massive pain so it's just a lot easier to not | ||
// plus as far as i can tell it's not actually *used* anywhere | ||
// whisper splits at 1024 chars and doesn't actually change the kind | ||
// so technically this is unused | ||
generic_trace!("{}", text.trim()); | ||
} | ||
GGMLLogLevel::Unknown(level) => { | ||
generic_warn!( | ||
"whisper_logging_trampoline: unknown log level {}: message: {}", | ||
level, | ||
text.trim() | ||
); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.