use std::cell::Cell;
use std::ffi::CString;
use std::os::raw::{c_char, c_void};
pub type LogCb = unsafe extern "C" fn(msg: *const c_char, user_data: *mut c_void);
thread_local! {
static LOG_CALLBACK: Cell<Option<(LogCb, *mut c_void)>> = Cell::new(None);
}
pub(crate) fn set_log_callback(cb: Option<(LogCb, *mut c_void)>) {
LOG_CALLBACK.with(|cell| cell.set(cb));
}
pub(crate) fn log_to_current(msg: &str) {
LOG_CALLBACK.with(|cell| {
if let Some((cb, user_data)) = cell.get() {
if let Ok(cstr) = CString::new(msg) {
unsafe { cb(cstr.as_ptr(), user_data) };
}
} else {
eprintln!("{}", msg);
}
});
}
macro_rules! rip_log {
($($arg:tt)*) => {
$crate::logging::log_to_current(&format!($($arg)*))
};
}
pub(crate) use rip_log;