Refactor logging to use a centralized logging utility across the application (#5277)

- Replaced direct log calls with a new logging macro that includes a logging type for better categorization.
- Updated logging in various modules including `merge.rs`, `mod.rs`, `tun.rs`, `clash.rs`, `profile.rs`, `proxy.rs`, `window.rs`, `lightweight.rs`, `guard.rs`, `autostart.rs`, `dirs.rs`, `dns.rs`, `scheme.rs`, `server.rs`, and `window_manager.rs`.
- Introduced logging types such as `Core`, `Network`, `ProxyMode`, `Window`, `Lightweight`, `Service`, and `File` to enhance log clarity and filtering.
This commit is contained in:
Tunglies
2025-11-01 20:47:01 +08:00
committed by GitHub
parent 50567d9b97
commit fb260fb33d
28 changed files with 473 additions and 210 deletions

View File

@@ -1,3 +1,5 @@
use crate::{logging, utils::logging::Type};
use super::use_lowercase;
use serde_yaml_ng::{self, Mapping, Value};
@@ -19,7 +21,11 @@ pub fn use_merge(merge: Mapping, config: Mapping) -> Mapping {
deep_merge(&mut config, &Value::from(merge));
config.as_mapping().cloned().unwrap_or_else(|| {
log::error!("Failed to convert merged config to mapping, using empty mapping");
logging!(
error,
Type::Core,
"Failed to convert merged config to mapping, using empty mapping"
);
Mapping::new()
})
}

View File

@@ -8,6 +8,7 @@ mod tun;
use self::{chain::*, field::*, merge::*, script::*, seq::*, tun::*};
use crate::utils::dirs;
use crate::{config::Config, utils::tmpl};
use crate::{logging, utils::logging::Type};
use serde_yaml_ng::Mapping;
use smartstring::alias::String;
use std::collections::{HashMap, HashSet};
@@ -422,14 +423,14 @@ fn apply_builtin_scripts(
.filter(|(s, _)| s.is_support(clash_core.as_ref()))
.map(|(_, c)| c)
.for_each(|item| {
log::debug!(target: "app", "run builtin script {}", item.uid);
logging!(debug, Type::Core, "run builtin script {}", item.uid);
if let ChainType::Script(script) = item.data {
match use_script(script, config.to_owned(), "".into()) {
Ok((res_config, _)) => {
config = res_config;
}
Err(err) => {
log::error!(target: "app", "builtin script error `{err}`");
logging!(error, Type::Core, "builtin script error `{err}`");
}
}
}
@@ -451,17 +452,17 @@ async fn apply_dns_settings(mut config: Mapping, enable_dns_settings: bool) -> M
&& hosts_value.is_mapping()
{
config.insert("hosts".into(), hosts_value.clone());
log::info!(target: "app", "apply hosts configuration");
logging!(info, Type::Core, "apply hosts configuration");
}
if let Some(dns_value) = dns_config.get("dns") {
if let Some(dns_mapping) = dns_value.as_mapping() {
config.insert("dns".into(), dns_mapping.clone().into());
log::info!(target: "app", "apply dns_config.yaml (dns section)");
logging!(info, Type::Core, "apply dns_config.yaml (dns section)");
}
} else {
config.insert("dns".into(), dns_config.into());
log::info!(target: "app", "apply dns_config.yaml");
logging!(info, Type::Core, "apply dns_config.yaml");
}
}
}

View File

@@ -2,6 +2,8 @@ use serde_yaml_ng::{Mapping, Value};
#[cfg(target_os = "macos")]
use crate::process::AsyncHandler;
#[cfg(target_os = "linux")]
use crate::{logging, utils::logging::Type};
macro_rules! revise {
($map: expr, $key: expr, $val: expr) => {
@@ -42,9 +44,10 @@ pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping {
if should_override {
revise!(tun_val, "stack", "mixed");
log::warn!(
target: "app",
"gVisor TUN stack detected on Linux; falling back to 'mixed' for compatibility"
logging!(
warn,
Type::Network,
"Warning: gVisor TUN stack detected on Linux; falling back to 'mixed' for compatibility"
);
}
}