Add logging callback

merge-requests/7/merge
T. Okubo 2015-12-11 22:20:25 +09:00
parent 8ec8ec48e2
commit a0e9f5b41b
2 changed files with 32 additions and 2 deletions

View File

@ -7,7 +7,7 @@ use std::borrow::Cow;
use std::marker::PhantomData;
use ffi;
use ::tools::{to_cstr, from_cstr, from_cstr_ref};
use ::libc::c_void;
use ::libc::{c_void, c_char, c_int};
use ::enums::*;
/// Retrieve libvlc version.
@ -93,6 +93,15 @@ impl Instance {
else { Some(ModuleDescriptionList{ptr: p}) }
}
}
/// Set logging callback
pub fn set_log<F: Fn(LogLevel, Log, Cow<str>) + Send + 'static>(&self, f: F) {
let cb: Box<Box<Fn(LogLevel, Log, Cow<str>) + Send + 'static>> = Box::new(Box::new(f));
unsafe{
ffi::libvlc_log_set(self.ptr, logging_cb, Box::into_raw(cb) as *mut _);
}
}
}
impl Drop for Instance {
@ -103,6 +112,21 @@ impl Drop for Instance {
}
}
extern "C" {
fn vsnprintf(s: *mut c_char, n: usize, fmt: *const c_char, arg: ffi::va_list);
}
const BUF_SIZE: usize = 1024; // Write log message to the buffer by vsnprintf.
unsafe extern "C" fn logging_cb(
data: *mut c_void, level: c_int, ctx: *const ffi::libvlc_log_t, fmt: *const c_char, args: ffi::va_list) {
let f: &Box<Fn(LogLevel, Log, Cow<str>) + Send + 'static> = ::std::mem::transmute(data);
let mut buf: [c_char; BUF_SIZE] = [0; BUF_SIZE];
vsnprintf(buf.as_mut_ptr(), BUF_SIZE, fmt, args);
f(::std::mem::transmute(level), Log{ptr: ctx}, from_cstr_ref(buf.as_ptr()).unwrap());
}
/// List of module description.
pub struct ModuleDescriptionList {
ptr: *mut ffi::libvlc_module_description_t,
@ -464,3 +488,7 @@ pub struct VLCObject {
_ptr: *mut c_void,
}
pub struct Log {
pub ptr: *const ffi::libvlc_log_t
}

View File

@ -32,6 +32,8 @@ pub enum libvlc_log_t {}
pub enum vlc_log_t {}
pub type libvlc_callback_t = unsafe extern "C" fn(*const libvlc_event_t, *mut c_void);
pub type va_list = *mut c_void;
pub type libvlc_log_cb = unsafe extern "C" fn(*mut c_void, c_int, *const libvlc_log_t, *const c_char, va_list);
pub use LogLevel as libvlc_log_level;
@ -77,7 +79,7 @@ extern "C" {
ctx: *const libvlc_log_t, name: *const *const c_char,
header: *const *const c_char, id: *mut uintptr_t);
pub fn libvlc_log_unset(_: *mut libvlc_instance_t);
// pub fn libvlc_log_set
pub fn libvlc_log_set(instance: *mut libvlc_instance_t, cb: libvlc_log_cb, data: *mut c_void);
pub fn libvlc_log_set_file(_: *mut libvlc_instance_t, stream: *mut FILE);
pub fn libvlc_module_description_list_release(p_list: *mut libvlc_module_description_t);
pub fn libvlc_audio_filter_list_get(