init_log once

This commit is contained in:
rustdesk 2024-03-16 22:20:50 +08:00
parent fb12ee7f2a
commit 6cbbb6498f

View File

@ -350,21 +350,23 @@ pub fn is_domain_port_str(id: &str) -> bool {
}
pub fn init_log(_is_async: bool, _name: &str) -> Option<flexi_logger::LoggerHandle> {
static INIT: std::sync::Once = std::sync::Once::new();
#[allow(unused_mut)]
let mut logger_holder: Option<flexi_logger::LoggerHandle> = None;
INIT.call_once(|| {
#[cfg(debug_assertions)]
{
use env_logger::*;
init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info"));
None
}
#[cfg(not(debug_assertions))]
{
// https://docs.rs/flexi_logger/latest/flexi_logger/error_info/index.html#write
// though async logger more efficient, but it also causes more problems, disable it for now
let mut logger_holder: Option<flexi_logger::LoggerHandle> = None;
let mut path = config::Config::log_path();
#[cfg(target_os = "android")]
if !config::Config::get_home().exists() {
return None;
return;
}
if !_name.is_empty() {
path.push(_name);
@ -387,8 +389,9 @@ pub fn init_log(_is_async: bool, _name: &str) -> Option<flexi_logger::LoggerHand
.start()
.ok();
}
logger_holder
}
});
logger_holder
}
#[cfg(test)]