diff --git a/Cargo.lock b/Cargo.lock index 8f8895bd5..d0f22a0ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2675,6 +2675,7 @@ dependencies = [ "dirs-next", "env_logger 0.9.3", "filetime", + "flexi_logger", "futures", "futures-util", "lazy_static", @@ -4938,7 +4939,6 @@ dependencies = [ "enigo", "errno", "evdev", - "flexi_logger", "flutter_rust_bridge", "flutter_rust_bridge_codegen", "fruitbasket", diff --git a/Cargo.toml b/Cargo.toml index b53615c4e..ba92733ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,6 @@ lazy_static = "1.4" sha2 = "0.10" repng = "0.2" parity-tokio-ipc = { git = "https://github.com/open-trade/parity-tokio-ipc" } -flexi_logger = { version = "0.22", features = ["async", "use_chrono_for_offset"] } runas = "0.2" magnum-opus = { git = "https://github.com/rustdesk/magnum-opus" } dasp = { version = "0.11", features = ["signal", "interpolate-linear", "interpolate"], optional = true } diff --git a/libs/hbb_common/Cargo.toml b/libs/hbb_common/Cargo.toml index a125078d2..c77f11eb5 100644 --- a/libs/hbb_common/Cargo.toml +++ b/libs/hbb_common/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +flexi_logger = { version = "0.22", features = ["async", "use_chrono_for_offset"] } protobuf = { version = "3.1", features = ["with-bytes"] } tokio = { version = "1.20", features = ["full"] } tokio-util = { version = "0.7", features = ["full"] } diff --git a/libs/hbb_common/src/lib.rs b/libs/hbb_common/src/lib.rs index bfb773908..21566aa23 100644 --- a/libs/hbb_common/src/lib.rs +++ b/libs/hbb_common/src/lib.rs @@ -39,8 +39,8 @@ pub use tokio_socks::IntoTargetAddr; pub use tokio_socks::TargetAddr; pub mod password_security; pub use chrono; -pub use libc; pub use directories_next; +pub use libc; pub mod keyboard; pub use sysinfo; @@ -312,6 +312,44 @@ pub fn is_domain_port_str(id: &str) -> bool { .is_match(id) } +pub fn init_log(_is_async: bool, _name: &str) -> Option { + #[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 = None; + let mut path = config::Config::log_path(); + if !_name.is_empty() { + path.push(_name); + } + use flexi_logger::*; + if let Ok(x) = Logger::try_with_env_or_str("debug") { + logger_holder = x + .log_to_file(FileSpec::default().directory(path)) + .write_mode(if _is_async { + WriteMode::Async + } else { + WriteMode::Direct + }) + .format(opt_format) + .rotate( + Criterion::Age(Age::Day), + Naming::Timestamps, + Cleanup::KeepLogFiles(6), + ) + .start() + .ok(); + } + logger_holder + } +} + #[cfg(test)] mod test { use super::*; diff --git a/src/core_main.rs b/src/core_main.rs index 0eeb2c6f9..60a7d9c9c 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -8,9 +8,6 @@ use hbb_common::platform::register_breakdown_handler; /// If it returns [`None`], then the process will terminate, and flutter gui will not be started. /// If it returns [`Some`], then the process will continue, and flutter gui will be started. pub fn core_main() -> Option> { - // 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 _async_logger_holder: Option = None; let mut args = Vec::new(); let mut flutter_args = Vec::new(); let mut i = 0; @@ -76,35 +73,14 @@ pub fn core_main() -> Option> { || (!click_setup && crate::platform::is_elevated(None).unwrap_or(false))); crate::portable_service::client::set_quick_support(_is_quick_support); } - #[cfg(debug_assertions)] - { - use hbb_common::env_logger::*; - init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info")); - } - #[cfg(not(debug_assertions))] - { - let mut path = hbb_common::config::Config::log_path(); - if args.len() > 0 && args[0].starts_with("--") { - let name = args[0].replace("--", ""); - if !name.is_empty() { - path.push(name); - } - } - use flexi_logger::*; - if let Ok(x) = Logger::try_with_env_or_str("debug") { - // _async_logger_holder = - x.log_to_file(FileSpec::default().directory(path)) - //.write_mode(WriteMode::Async) - .format(opt_format) - .rotate( - Criterion::Age(Age::Day), - Naming::Timestamps, - Cleanup::KeepLogFiles(6), - ) - .start() - .ok(); + let mut log_name = "".to_owned(); + if args.len() > 0 && args[0].starts_with("--") { + let name = args[0].replace("--", ""); + if !name.is_empty() { + log_name = name; } } + hbb_common::init_log(false, &log_name); #[cfg(windows)] if !crate::platform::is_installed() && args.is_empty()