1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use crate::wintun_raw;
use log::*;
use widestring::U16CStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
pub fn set_logger(wintun: &Arc<wintun_raw::wintun>, f: wintun_raw::WINTUN_LOGGER_CALLBACK) {
unsafe { wintun.WintunSetLogger(f) };
}
static SET_LOGGER: AtomicBool = AtomicBool::new(false);
pub extern "C" fn default_logger(
level: wintun_raw::WINTUN_LOGGER_LEVEL,
message: *const wintun_raw::WCHAR,
) {
let msg = unsafe { U16CStr::from_ptr_str(message) };
let utf8_msg = msg.to_string_lossy();
match level {
wintun_raw::WINTUN_LOGGER_LEVEL_WINTUN_LOG_INFO => info!("WinTun: {}", utf8_msg),
wintun_raw::WINTUN_LOGGER_LEVEL_WINTUN_LOG_WARN => warn!("WinTun: {}", utf8_msg),
wintun_raw::WINTUN_LOGGER_LEVEL_WINTUN_LOG_ERR => error!("WinTun: {}", utf8_msg),
_ => error!("WinTun: {} (with invalid log level {})", utf8_msg, level),
}
}
pub(crate) fn set_default_logger_if_unset(wintun: &Arc<wintun_raw::wintun>) {
if !SET_LOGGER.load(Ordering::SeqCst) {
set_logger(wintun, Some(default_logger));
SET_LOGGER.store(true, Ordering::SeqCst);
}
}