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,7 +1,7 @@
#[cfg(target_os = "windows")]
use crate::{logging, utils::logging::Type};
#[cfg(target_os = "windows")]
use anyhow::{Result, anyhow};
#[cfg(target_os = "windows")]
use log::info;
#[cfg(target_os = "windows")]
use std::{os::windows::process::CommandExt, path::Path, path::PathBuf};
@@ -49,15 +49,15 @@ pub async fn create_shortcut() -> Result<()> {
.remove_if_exists()
.await
.inspect(|_| {
info!(target: "app", "成功移除旧启动快捷方式");
logging!(info, Type::Setup, "成功移除旧启动快捷方式");
})
.inspect_err(|err| {
log::error!(target: "app", "移除旧启动快捷方式失败: {err}");
logging!(error, Type::Setup, "移除旧启动快捷方式失败: {err}");
});
// 如果新快捷方式已存在,直接返回成功
if new_shortcut_path.exists() {
info!(target: "app", "启动快捷方式已存在");
logging!(info, Type::Setup, "启动快捷方式已存在");
return Ok(());
}
@@ -83,7 +83,7 @@ pub async fn create_shortcut() -> Result<()> {
return Err(anyhow!("创建快捷方式失败: {}", error_msg));
}
info!(target: "app", "成功创建启动快捷方式");
logging!(info, Type::Setup, "成功创建启动快捷方式");
Ok(())
}
@@ -102,22 +102,22 @@ pub async fn remove_shortcut() -> Result<()> {
.remove_if_exists()
.await
.inspect(|_| {
info!(target: "app", "成功删除旧启动快捷方式");
logging!(info, Type::Setup, "成功删除旧启动快捷方式");
removed_any = true;
})
.inspect_err(|err| {
log::error!(target: "app", "删除旧启动快捷方式失败: {err}");
logging!(error, Type::Setup, "删除旧启动快捷方式失败: {err}");
});
let _ = new_shortcut_path
.remove_if_exists()
.await
.inspect(|_| {
info!(target: "app", "成功删除启动快捷方式");
logging!(info, Type::Setup, "成功删除启动快捷方式");
removed_any = true;
})
.inspect_err(|err| {
log::error!(target: "app", "删除启动快捷方式失败: {err}");
logging!(error, Type::Setup, "删除启动快捷方式失败: {err}");
});
Ok(())

View File

@@ -62,7 +62,11 @@ pub fn app_home_dir() -> Result<PathBuf> {
match app_handle.path().data_dir() {
Ok(dir) => Ok(dir.join(APP_ID)),
Err(e) => {
log::error!(target: "app", "Failed to get the app home directory: {e}");
logging!(
error,
Type::File,
"Failed to get the app home directory: {e}"
);
Err(anyhow::anyhow!("Failed to get the app homedirectory"))
}
}
@@ -76,7 +80,11 @@ pub fn app_resources_dir() -> Result<PathBuf> {
match app_handle.path().resource_dir() {
Ok(dir) => Ok(dir.join("resources")),
Err(e) => {
log::error!(target: "app", "Failed to get the resource directory: {e}");
logging!(
error,
Type::File,
"Failed to get the resource directory: {e}"
);
Err(anyhow::anyhow!("Failed to get the resource directory"))
}
}
@@ -229,7 +237,11 @@ pub fn ensure_mihomo_safe_dir() -> Option<PathBuf> {
if home_config.exists() || fs::create_dir_all(&home_config).is_ok() {
Some(home_config)
} else {
log::error!(target: "app", "Failed to create safe directory: {home_config:?}");
logging!(
error,
Type::File,
"Failed to create safe directory: {home_config:?}"
);
None
}
})

View File

@@ -29,7 +29,6 @@ pub enum Type {
Lightweight,
Network,
ProxyMode,
// Cache,
Validate,
ClashVergeRev,
}
@@ -53,7 +52,6 @@ impl fmt::Display for Type {
Type::Lightweight => write!(f, "[Lightweight]"),
Type::Network => write!(f, "[Network]"),
Type::ProxyMode => write!(f, "[ProxMode]"),
// Type::Cache => write!(f, "[Cache]"),
Type::Validate => write!(f, "[Validate]"),
Type::ClashVergeRev => write!(f, "[ClashVergeRev]"),
}

View File

@@ -1,20 +1,27 @@
#[cfg(target_os = "macos")]
use crate::{logging, utils::logging::Type};
pub async fn set_public_dns(dns_server: String) {
use crate::{core::handle, utils::dirs};
use crate::utils::logging::Type;
use crate::{core::handle, logging, utils::dirs};
use tauri_plugin_shell::ShellExt;
let app_handle = handle::Handle::app_handle();
log::info!(target: "app", "try to set system dns");
logging!(info, Type::Config, "try to set system dns");
let resource_dir = match dirs::app_resources_dir() {
Ok(dir) => dir,
Err(e) => {
log::error!(target: "app", "Failed to get resource directory: {}", e);
logging!(
error,
Type::Config,
"Failed to get resource directory: {}",
e
);
return;
}
};
let script = resource_dir.join("set_dns.sh");
if !script.exists() {
log::error!(target: "app", "set_dns.sh not found");
logging!(error, Type::Config, "set_dns.sh not found");
return;
}
let script = script.to_string_lossy().into_owned();
@@ -28,14 +35,14 @@ pub async fn set_public_dns(dns_server: String) {
{
Ok(status) => {
if status.success() {
log::info!(target: "app", "set system dns successfully");
logging!(info, Type::Config, "set system dns successfully");
} else {
let code = status.code().unwrap_or(-1);
log::error!(target: "app", "set system dns failed: {code}");
logging!(error, Type::Config, "set system dns failed: {code}");
}
}
Err(err) => {
log::error!(target: "app", "set system dns failed: {err}");
logging!(error, Type::Config, "set system dns failed: {err}");
}
}
}
@@ -45,17 +52,22 @@ pub async fn restore_public_dns() {
use crate::{core::handle, utils::dirs};
use tauri_plugin_shell::ShellExt;
let app_handle = handle::Handle::app_handle();
log::info!(target: "app", "try to unset system dns");
logging!(info, Type::Config, "try to unset system dns");
let resource_dir = match dirs::app_resources_dir() {
Ok(dir) => dir,
Err(e) => {
log::error!(target: "app", "Failed to get resource directory: {}", e);
logging!(
error,
Type::Config,
"Failed to get resource directory: {}",
e
);
return;
}
};
let script = resource_dir.join("unset_dns.sh");
if !script.exists() {
log::error!(target: "app", "unset_dns.sh not found");
logging!(error, Type::Config, "unset_dns.sh not found");
return;
}
let script = script.to_string_lossy().into_owned();
@@ -69,14 +81,14 @@ pub async fn restore_public_dns() {
{
Ok(status) => {
if status.success() {
log::info!(target: "app", "unset system dns successfully");
logging!(info, Type::Config, "unset system dns successfully");
} else {
let code = status.code().unwrap_or(-1);
log::error!(target: "app", "unset system dns failed: {code}");
logging!(error, Type::Config, "unset system dns failed: {code}");
}
}
Err(err) => {
log::error!(target: "app", "unset system dns failed: {err}");
logging!(error, Type::Config, "unset system dns failed: {err}");
}
}
}

View File

@@ -11,7 +11,7 @@ use crate::{
};
pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
log::info!(target:"app", "received deep link: {param}");
logging!(info, Type::Config, "received deep link: {param}");
let param_str = if param.starts_with("[") && param.len() > 4 {
param
@@ -50,8 +50,8 @@ pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
match url_param {
Some(ref url) => {
log::info!(target:"app", "decoded subscription url: {url}");
match PrfItem::from_url(url, name, None, None).await {
logging!(info, Type::Config, "decoded subscription url: {url}");
match PrfItem::from_url(url.as_ref(), name, None, None).await {
Ok(mut item) => {
let uid = match item.uid.clone() {
Some(uid) => uid,

View File

@@ -51,7 +51,11 @@ pub async fn check_singleton() -> Result<()> {
.send()
.await?;
}
log::error!("failed to setup singleton listen server");
logging!(
error,
Type::Window,
"failed to setup singleton listen server"
);
bail!("app exists");
}
Ok(())
@@ -129,7 +133,7 @@ pub fn embed_server() {
}
pub fn shutdown_embedded_server() {
log::info!("shutting down embedded server");
logging!(info, Type::Window, "shutting down embedded server");
if let Some(sender) = SHUTDOWN_SENDER.get()
&& let Some(sender) = sender.lock().take()
{

View File

@@ -58,7 +58,11 @@ fn get_window_operation_debounce() -> &'static Mutex<Instant> {
fn should_handle_window_operation() -> bool {
if WINDOW_OPERATION_IN_PROGRESS.load(Ordering::Acquire) {
log::warn!(target: "app", "[防抖] 窗口操作已在进行中,跳过重复调用");
logging!(
warn,
Type::Window,
"Warning: [防抖] 窗口操作已在进行中,跳过重复调用"
);
return false;
}
@@ -67,17 +71,27 @@ fn should_handle_window_operation() -> bool {
let now = Instant::now();
let elapsed = now.duration_since(*last_operation);
log::debug!(target: "app", "[防抖] 检查窗口操作间隔: {}ms (需要>={}ms)",
elapsed.as_millis(), WINDOW_OPERATION_DEBOUNCE_MS);
logging!(
debug,
Type::Window,
"[防抖] 检查窗口操作间隔: {}ms (需要>={}ms)",
elapsed.as_millis(),
WINDOW_OPERATION_DEBOUNCE_MS
);
if elapsed >= Duration::from_millis(WINDOW_OPERATION_DEBOUNCE_MS) {
*last_operation = now;
WINDOW_OPERATION_IN_PROGRESS.store(true, Ordering::Release);
log::info!(target: "app", "[防抖] 窗口操作被允许执行");
logging!(info, Type::Window, "[防抖] 窗口操作被允许执行");
true
} else {
log::warn!(target: "app", "[防抖] 窗口操作被防抖机制忽略,距离上次操作 {}ms < {}ms",
elapsed.as_millis(), WINDOW_OPERATION_DEBOUNCE_MS);
logging!(
warn,
Type::Window,
"Warning: [防抖] 窗口操作被防抖机制忽略,距离上次操作 {}ms < {}ms",
elapsed.as_millis(),
WINDOW_OPERATION_DEBOUNCE_MS
);
false
}
}