Skip to content

Commit

Permalink
Allow clients to set a log callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
mutexlox-signal committed Oct 17, 2024
1 parent 716a8b4 commit 302f0c4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cubeb-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ gecko-in-tree = ["cubeb-sys/gecko-in-tree"]
[dependencies]
bitflags = "1.2.0"
cubeb-sys = { path = "../cubeb-sys", version = "0.14" }

[build-dependencies]
cc = "1.1.30"
42 changes: 41 additions & 1 deletion cubeb-core/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// This program is made available under an ISC-style license. See the
// accompanying file LICENSE for details.

use ffi;
use std::ffi::{c_char, CStr};
use std::sync::Mutex;
use {ffi, Error, Result};

/// Level (verbosity) of logging for a particular cubeb context.
#[derive(PartialEq, Eq, Clone, Debug, Copy, PartialOrd, Ord)]
Expand Down Expand Up @@ -31,6 +33,44 @@ pub fn log_enabled() -> bool {
unsafe { ffi::cubeb_log_get_level() != LogLevel::Disabled as _ }
}

static LOG_CALLBACK: Mutex<Option<fn(s: &CStr)>> = Mutex::new(None);

extern "C" {
fn cubeb_write_log(fmt: *const c_char, ...);
}

/// # Safety
///
/// |s| must be null, or a pointer to a valid, nul-terminated, array of chars.
pub unsafe extern "C" fn rust_write_formatted_msg(s: *const c_char) {
if s.is_null() {
// Do nothing if the pointer is null.
return;
}
if let Ok(guard) = LOG_CALLBACK.lock() {
if let Some(f) = *guard {
f(CStr::from_ptr(s));
}
// Do nothing if there is no callback.
}
// Silently fail if lock cannot be acquired.
}

pub fn set_logging(level: LogLevel, f: Option<fn(s: &CStr)>) -> Result<()> {
match LOG_CALLBACK.lock() {
Ok(mut guard) => {
*guard = f;
}
Err(_) => return Err(Error::error()),
}
unsafe {
call!(ffi::cubeb_set_log_callback(
level as u32,
Some(cubeb_write_log)
))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 302f0c4

Please sign in to comment.