Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Conversation

@mmende
Copy link
Contributor

@mmende mmende commented Feb 20, 2025

The log level enum must have been changed at some point (e.g. GGML_LOG_LEVEL_DEBUG is now 1 instead of 4). The bindings are updated in this regard. I also made GGMLLogLevel public such that it can be used to implement custom logging logic, e.g.:

use log::{debug, error, info, trace, warn};
use whisper_rs::GGMLLogLevel;

pub unsafe extern "C" fn whisper_log_fn(
    level: u32,
    text: *const std::os::raw::c_char,
    _: *mut std::os::raw::c_void, // user_data
) {
    if text.is_null() {
        error!("whisper_log_fn: 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();

    let level = GGMLLogLevel::from(level);

    match level {
        GGMLLogLevel::None => debug!(target: "whisper", "{}", trimmed),
        GGMLLogLevel::Debug => debug!(target: "whisper", "{}", trimmed),
        GGMLLogLevel::Info => info!(target: "whisper", "{}", trimmed),
        GGMLLogLevel::Warn => warn!(target: "whisper", "{}", trimmed),
        GGMLLogLevel::Error => error!(target: "whisper", "{}", trimmed),
        GGMLLogLevel::Cont => trace!(target: "whisper", "{}", trimmed),
        GGMLLogLevel::Unknown(level) => warn!(
            target: "whisper",
            "whisper_log_fn: unknown log level {}: message: {}", level, trimmed
        ),
    }
}


fn main() {
    unsafe {
        whisper_rs::set_log_callback(Some(whisper_log_fn), std::ptr::null_mut());
    }
}

@mmende mmende changed the title Updated bindings to reflect correct log levels and exposing GGMLLogLevel for custom log callbacks Exposing GGMLLogLevel for custom log callbacks Feb 21, 2025
@tazz4843 tazz4843 enabled auto-merge (squash) February 21, 2025 22:21
@tazz4843 tazz4843 merged commit 5479964 into tazz4843:master Feb 21, 2025
13 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants