use std::ffi::CStr;
use std::os::raw::{c_char, c_int};
use std::sync::Once;
use libgvproxy_sys::gvproxy_set_log_callback;
extern "C" fn gvproxy_log_callback(level: c_int, message: *const c_char) {
if message.is_null() {
return;
}
let msg = unsafe {
match CStr::from_ptr(message).to_str() {
Ok(s) => s,
Err(_) => {
return;
}
}
};
match level {
0 => tracing::trace!(target: "gvproxy", "{}", msg),
1 => tracing::debug!(target: "gvproxy", "{}", msg),
2 => tracing::info!(target: "gvproxy", "{}", msg),
3 => tracing::warn!(target: "gvproxy", "{}", msg),
4 => tracing::error!(target: "gvproxy", "{}", msg),
_ => tracing::info!(target: "gvproxy", "{}", msg),
}
}
pub fn init_logging() {
static INIT_LOGGING: Once = Once::new();
INIT_LOGGING.call_once(|| {
tracing::debug!("Initializing gvproxy log callback");
unsafe {
gvproxy_set_log_callback(gvproxy_log_callback as *const std::ffi::c_void);
}
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_init_logging_multiple_times() {
init_logging();
init_logging();
init_logging();
}
}