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:
committed by
GitHub
Unverified
parent
50567d9b97
commit
fb260fb33d
@@ -1,5 +1,6 @@
|
||||
#[cfg(target_os = "windows")]
|
||||
use crate::process::AsyncHandler;
|
||||
use crate::{logging, utils::logging::Type};
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::time::{Duration, timeout};
|
||||
@@ -41,15 +42,21 @@ impl AsyncProxyQuery {
|
||||
pub async fn get_auto_proxy() -> AsyncAutoproxy {
|
||||
match timeout(Duration::from_secs(3), Self::get_auto_proxy_impl()).await {
|
||||
Ok(Ok(proxy)) => {
|
||||
log::debug!(target: "app", "异步获取自动代理成功: enable={}, url={}", proxy.enable, proxy.url);
|
||||
logging!(
|
||||
debug,
|
||||
Type::Network,
|
||||
"异步获取自动代理成功: enable={}, url={}",
|
||||
proxy.enable,
|
||||
proxy.url
|
||||
);
|
||||
proxy
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
log::warn!(target: "app", "异步获取自动代理失败: {e}");
|
||||
logging!(warn, Type::Network, "Warning: 异步获取自动代理失败: {e}");
|
||||
AsyncAutoproxy::default()
|
||||
}
|
||||
Err(_) => {
|
||||
log::warn!(target: "app", "异步获取自动代理超时");
|
||||
logging!(warn, Type::Network, "Warning: 异步获取自动代理超时");
|
||||
AsyncAutoproxy::default()
|
||||
}
|
||||
}
|
||||
@@ -59,15 +66,22 @@ impl AsyncProxyQuery {
|
||||
pub async fn get_system_proxy() -> AsyncSysproxy {
|
||||
match timeout(Duration::from_secs(3), Self::get_system_proxy_impl()).await {
|
||||
Ok(Ok(proxy)) => {
|
||||
log::debug!(target: "app", "异步获取系统代理成功: enable={}, {}:{}", proxy.enable, proxy.host, proxy.port);
|
||||
logging!(
|
||||
debug,
|
||||
Type::Network,
|
||||
"异步获取系统代理成功: enable={}, {}:{}",
|
||||
proxy.enable,
|
||||
proxy.host,
|
||||
proxy.port
|
||||
);
|
||||
proxy
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
log::warn!(target: "app", "异步获取系统代理失败: {e}");
|
||||
logging!(warn, Type::Network, "Warning: 异步获取系统代理失败: {e}");
|
||||
AsyncSysproxy::default()
|
||||
}
|
||||
Err(_) => {
|
||||
log::warn!(target: "app", "异步获取系统代理超时");
|
||||
logging!(warn, Type::Network, "Warning: 异步获取系统代理超时");
|
||||
AsyncSysproxy::default()
|
||||
}
|
||||
}
|
||||
@@ -99,7 +113,7 @@ impl AsyncProxyQuery {
|
||||
RegOpenKeyExW(HKEY_CURRENT_USER, key_path.as_ptr(), 0, KEY_READ, &mut hkey);
|
||||
|
||||
if result != 0 {
|
||||
log::debug!(target: "app", "无法打开注册表项");
|
||||
logging!(debug, Type::Network, "无法打开注册表项");
|
||||
return Ok(AsyncAutoproxy::default());
|
||||
}
|
||||
|
||||
@@ -125,7 +139,7 @@ impl AsyncProxyQuery {
|
||||
.position(|&x| x == 0)
|
||||
.unwrap_or(url_buffer.len());
|
||||
pac_url = String::from_utf16_lossy(&url_buffer[..end_pos]);
|
||||
log::debug!(target: "app", "从注册表读取到PAC URL: {pac_url}");
|
||||
logging!(debug, Type::Network, "从注册表读取到PAC URL: {pac_url}");
|
||||
}
|
||||
|
||||
// 2. 检查自动检测设置是否启用
|
||||
@@ -150,7 +164,11 @@ impl AsyncProxyQuery {
|
||||
|| (detect_query_result == 0 && detect_value_type == REG_DWORD && auto_detect != 0);
|
||||
|
||||
if pac_enabled {
|
||||
log::debug!(target: "app", "PAC配置启用: URL={pac_url}, AutoDetect={auto_detect}");
|
||||
logging!(
|
||||
debug,
|
||||
Type::Network,
|
||||
"PAC配置启用: URL={pac_url}, AutoDetect={auto_detect}"
|
||||
);
|
||||
|
||||
if pac_url.is_empty() && auto_detect != 0 {
|
||||
pac_url = "auto-detect".into();
|
||||
@@ -161,7 +179,7 @@ impl AsyncProxyQuery {
|
||||
url: pac_url,
|
||||
})
|
||||
} else {
|
||||
log::debug!(target: "app", "PAC配置未启用");
|
||||
logging!(debug, Type::Network, "PAC配置未启用");
|
||||
Ok(AsyncAutoproxy::default())
|
||||
}
|
||||
}
|
||||
@@ -177,7 +195,11 @@ impl AsyncProxyQuery {
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
log::debug!(target: "app", "scutil output: {stdout}");
|
||||
crate::logging!(
|
||||
debug,
|
||||
crate::utils::logging::Type::Network,
|
||||
"scutil output: {stdout}"
|
||||
);
|
||||
|
||||
let mut pac_enabled = false;
|
||||
let mut pac_url = String::new();
|
||||
@@ -196,7 +218,11 @@ impl AsyncProxyQuery {
|
||||
}
|
||||
}
|
||||
|
||||
log::debug!(target: "app", "解析结果: pac_enabled={pac_enabled}, pac_url={pac_url}");
|
||||
crate::logging!(
|
||||
debug,
|
||||
crate::utils::logging::Type::Network,
|
||||
"解析结果: pac_enabled={pac_enabled}, pac_url={pac_url}"
|
||||
);
|
||||
|
||||
Ok(AsyncAutoproxy {
|
||||
enable: pac_enabled && !pac_url.is_empty(),
|
||||
@@ -363,7 +389,11 @@ impl AsyncProxyQuery {
|
||||
(proxy_server, 8080)
|
||||
};
|
||||
|
||||
log::debug!(target: "app", "从注册表读取到代理设置: {host}:{port}, bypass: {bypass_list}");
|
||||
logging!(
|
||||
debug,
|
||||
Type::Network,
|
||||
"从注册表读取到代理设置: {host}:{port}, bypass: {bypass_list}"
|
||||
);
|
||||
|
||||
Ok(AsyncSysproxy {
|
||||
enable: true,
|
||||
@@ -386,7 +416,7 @@ impl AsyncProxyQuery {
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
log::debug!(target: "app", "scutil proxy output: {stdout}");
|
||||
logging!(debug, Type::Network, "scutil proxy output: {stdout}");
|
||||
|
||||
let mut http_enabled = false;
|
||||
let mut http_host = String::new();
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
use crate::{config::Config, process::AsyncHandler, utils::dirs};
|
||||
use crate::{
|
||||
config::Config,
|
||||
logging,
|
||||
process::AsyncHandler,
|
||||
utils::{dirs, logging::Type},
|
||||
};
|
||||
use anyhow::Error;
|
||||
use once_cell::sync::OnceCell;
|
||||
use parking_lot::Mutex;
|
||||
@@ -137,9 +142,14 @@ impl WebDavClient {
|
||||
.is_err()
|
||||
{
|
||||
match client.mkcol(dirs::BACKUP_DIR).await {
|
||||
Ok(_) => log::info!("Successfully created backup directory"),
|
||||
Ok(_) => logging!(info, Type::Backup, "Successfully created backup directory"),
|
||||
Err(e) => {
|
||||
log::warn!("Failed to create backup directory: {}", e);
|
||||
logging!(
|
||||
warn,
|
||||
Type::Backup,
|
||||
"Warning: Failed to create backup directory: {}",
|
||||
e
|
||||
);
|
||||
// 清除缓存,强制下次重新尝试
|
||||
self.reset();
|
||||
return Err(anyhow::Error::msg(format!(
|
||||
@@ -180,7 +190,11 @@ impl WebDavClient {
|
||||
|
||||
match upload_result {
|
||||
Err(_) => {
|
||||
log::warn!("Upload timed out, retrying once");
|
||||
logging!(
|
||||
warn,
|
||||
Type::Backup,
|
||||
"Warning: Upload timed out, retrying once"
|
||||
);
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
timeout(
|
||||
Duration::from_secs(TIMEOUT_UPLOAD),
|
||||
@@ -191,7 +205,11 @@ impl WebDavClient {
|
||||
}
|
||||
|
||||
Ok(Err(e)) => {
|
||||
log::warn!("Upload failed, retrying once: {e}");
|
||||
logging!(
|
||||
warn,
|
||||
Type::Backup,
|
||||
"Warning: Upload failed, retrying once: {e}"
|
||||
);
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
timeout(
|
||||
Duration::from_secs(TIMEOUT_UPLOAD),
|
||||
|
||||
@@ -7,6 +7,7 @@ use tokio_stream::{StreamExt, wrappers::UnboundedReceiverStream};
|
||||
use crate::config::{Config, IVerge};
|
||||
use crate::core::{async_proxy_query::AsyncProxyQuery, handle};
|
||||
use crate::process::AsyncHandler;
|
||||
use crate::{logging, utils::logging::Type};
|
||||
use once_cell::sync::Lazy;
|
||||
use smartstring::alias::String;
|
||||
use sysproxy::{Autoproxy, Sysproxy};
|
||||
@@ -104,14 +105,14 @@ impl EventDrivenProxyManager {
|
||||
let query = QueryRequest { response_tx: tx };
|
||||
|
||||
if self.query_sender.send(query).is_err() {
|
||||
log::error!(target: "app", "发送查询请求失败,返回缓存数据");
|
||||
logging!(error, Type::Network, "发送查询请求失败,返回缓存数据");
|
||||
return self.get_auto_proxy_cached().await;
|
||||
}
|
||||
|
||||
match timeout(Duration::from_secs(5), rx).await {
|
||||
Ok(Ok(result)) => result,
|
||||
_ => {
|
||||
log::warn!(target: "app", "查询超时,返回缓存数据");
|
||||
logging!(warn, Type::Network, "Warning: 查询超时,返回缓存数据");
|
||||
self.get_auto_proxy_cached().await
|
||||
}
|
||||
}
|
||||
@@ -134,7 +135,7 @@ impl EventDrivenProxyManager {
|
||||
|
||||
fn send_event(&self, event: ProxyEvent) {
|
||||
if let Err(e) = self.event_sender.send(event) {
|
||||
log::error!(target: "app", "发送代理事件失败: {e}");
|
||||
logging!(error, Type::Network, "发送代理事件失败: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,7 +144,7 @@ impl EventDrivenProxyManager {
|
||||
event_rx: mpsc::UnboundedReceiver<ProxyEvent>,
|
||||
query_rx: mpsc::UnboundedReceiver<QueryRequest>,
|
||||
) {
|
||||
log::info!(target: "app", "事件驱动代理管理器启动");
|
||||
logging!(info, Type::Network, "事件驱动代理管理器启动");
|
||||
|
||||
// 将 mpsc 接收器包装成 Stream,避免每次循环创建 future
|
||||
let mut event_stream = UnboundedReceiverStream::new(event_rx);
|
||||
@@ -158,7 +159,7 @@ impl EventDrivenProxyManager {
|
||||
loop {
|
||||
tokio::select! {
|
||||
Some(event) = event_stream.next() => {
|
||||
log::debug!(target: "app", "处理代理事件: {event:?}");
|
||||
logging!(debug, Type::Network, "处理代理事件: {event:?}");
|
||||
let event_clone = event.clone(); // 保存一份副本用于后续检查
|
||||
Self::handle_event(&state, event).await;
|
||||
|
||||
@@ -179,13 +180,13 @@ impl EventDrivenProxyManager {
|
||||
// 定时检查代理设置
|
||||
let config = Self::get_proxy_config().await;
|
||||
if config.guard_enabled && config.sys_enabled {
|
||||
log::debug!(target: "app", "定时检查代理设置");
|
||||
logging!(debug, Type::Network, "定时检查代理设置");
|
||||
Self::check_and_restore_proxy(&state).await;
|
||||
}
|
||||
}
|
||||
else => {
|
||||
// 两个通道都关闭时退出
|
||||
log::info!(target: "app", "事件或查询通道关闭,代理管理器停止");
|
||||
logging!(info, Type::Network, "事件或查询通道关闭,代理管理器停止");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -201,7 +202,7 @@ impl EventDrivenProxyManager {
|
||||
Self::initialize_proxy_state(state).await;
|
||||
}
|
||||
ProxyEvent::AppStopping => {
|
||||
log::info!(target: "app", "清理代理状态");
|
||||
logging!(info, Type::Network, "清理代理状态");
|
||||
Self::update_state_timestamp(state, |s| {
|
||||
s.sys_enabled = false;
|
||||
s.pac_enabled = false;
|
||||
@@ -224,7 +225,7 @@ impl EventDrivenProxyManager {
|
||||
}
|
||||
|
||||
async fn initialize_proxy_state(state: &Arc<RwLock<ProxyState>>) {
|
||||
log::info!(target: "app", "初始化代理状态");
|
||||
logging!(info, Type::Network, "初始化代理状态");
|
||||
|
||||
let config = Self::get_proxy_config().await;
|
||||
let auto_proxy = Self::get_auto_proxy_with_timeout().await;
|
||||
@@ -239,11 +240,17 @@ impl EventDrivenProxyManager {
|
||||
})
|
||||
.await;
|
||||
|
||||
log::info!(target: "app", "代理状态初始化完成: sys={}, pac={}", config.sys_enabled, config.pac_enabled);
|
||||
logging!(
|
||||
info,
|
||||
Type::Network,
|
||||
"代理状态初始化完成: sys={}, pac={}",
|
||||
config.sys_enabled,
|
||||
config.pac_enabled
|
||||
);
|
||||
}
|
||||
|
||||
async fn update_proxy_config(state: &Arc<RwLock<ProxyState>>) {
|
||||
log::debug!(target: "app", "更新代理配置");
|
||||
logging!(debug, Type::Network, "更新代理配置");
|
||||
|
||||
let config = Self::get_proxy_config().await;
|
||||
|
||||
@@ -260,7 +267,7 @@ impl EventDrivenProxyManager {
|
||||
|
||||
async fn check_and_restore_proxy(state: &Arc<RwLock<ProxyState>>) {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过系统代理守卫检查");
|
||||
logging!(debug, Type::Network, "应用正在退出,跳过系统代理守卫检查");
|
||||
return;
|
||||
}
|
||||
let (sys_enabled, pac_enabled) = {
|
||||
@@ -272,7 +279,7 @@ impl EventDrivenProxyManager {
|
||||
return;
|
||||
}
|
||||
|
||||
log::debug!(target: "app", "检查代理状态");
|
||||
logging!(debug, Type::Network, "检查代理状态");
|
||||
|
||||
if pac_enabled {
|
||||
Self::check_and_restore_pac_proxy(state).await;
|
||||
@@ -283,7 +290,7 @@ impl EventDrivenProxyManager {
|
||||
|
||||
async fn check_and_restore_pac_proxy(state: &Arc<RwLock<ProxyState>>) {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过PAC代理恢复检查");
|
||||
logging!(debug, Type::Network, "应用正在退出,跳过PAC代理恢复检查");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -296,9 +303,9 @@ impl EventDrivenProxyManager {
|
||||
.await;
|
||||
|
||||
if !current.enable || current.url != expected.url {
|
||||
log::info!(target: "app", "PAC代理设置异常,正在恢复...");
|
||||
logging!(info, Type::Network, "PAC代理设置异常,正在恢复...");
|
||||
if let Err(e) = Self::restore_pac_proxy(&expected.url).await {
|
||||
log::error!(target: "app", "恢复PAC代理失败: {}", e);
|
||||
logging!(error, Type::Network, "恢复PAC代理失败: {}", e);
|
||||
}
|
||||
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
@@ -314,7 +321,7 @@ impl EventDrivenProxyManager {
|
||||
|
||||
async fn check_and_restore_sys_proxy(state: &Arc<RwLock<ProxyState>>) {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过系统代理恢复检查");
|
||||
logging!(debug, Type::Network, "应用正在退出,跳过系统代理恢复检查");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -327,9 +334,9 @@ impl EventDrivenProxyManager {
|
||||
.await;
|
||||
|
||||
if !current.enable || current.host != expected.host || current.port != expected.port {
|
||||
log::info!(target: "app", "系统代理设置异常,正在恢复...");
|
||||
logging!(info, Type::Network, "系统代理设置异常,正在恢复...");
|
||||
if let Err(e) = Self::restore_sys_proxy(&expected).await {
|
||||
log::error!(target: "app", "恢复系统代理失败: {}", e);
|
||||
logging!(error, Type::Network, "恢复系统代理失败: {}", e);
|
||||
}
|
||||
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
@@ -457,7 +464,7 @@ impl EventDrivenProxyManager {
|
||||
#[cfg(target_os = "windows")]
|
||||
async fn restore_pac_proxy(expected_url: &str) -> Result<(), anyhow::Error> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过PAC代理恢复");
|
||||
logging!(debug, Type::Network, "应用正在退出,跳过PAC代理恢复");
|
||||
return Ok(());
|
||||
}
|
||||
Self::execute_sysproxy_command(&["pac", expected_url]).await
|
||||
@@ -481,7 +488,7 @@ impl EventDrivenProxyManager {
|
||||
#[cfg(target_os = "windows")]
|
||||
async fn restore_sys_proxy(expected: &Sysproxy) -> Result<(), anyhow::Error> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过系统代理恢复");
|
||||
logging!(debug, Type::Network, "应用正在退出,跳过系统代理恢复");
|
||||
return Ok(());
|
||||
}
|
||||
let address = format!("{}:{}", expected.host, expected.port);
|
||||
@@ -502,8 +509,9 @@ impl EventDrivenProxyManager {
|
||||
#[cfg(target_os = "windows")]
|
||||
async fn execute_sysproxy_command(args: &[&str]) -> Result<(), anyhow::Error> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(
|
||||
target: "app",
|
||||
logging!(
|
||||
debug,
|
||||
Type::Network,
|
||||
"应用正在退出,取消调用 sysproxy.exe,参数: {:?}",
|
||||
args
|
||||
);
|
||||
@@ -518,14 +526,14 @@ impl EventDrivenProxyManager {
|
||||
let binary_path = match dirs::service_path() {
|
||||
Ok(path) => path,
|
||||
Err(e) => {
|
||||
log::error!(target: "app", "获取服务路径失败: {e}");
|
||||
logging!(error, Type::Network, "获取服务路径失败: {e}");
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
||||
let sysproxy_exe = binary_path.with_file_name("sysproxy.exe");
|
||||
if !sysproxy_exe.exists() {
|
||||
log::error!(target: "app", "sysproxy.exe 不存在");
|
||||
logging!(error, Type::Network, "sysproxy.exe 不存在");
|
||||
}
|
||||
anyhow::ensure!(sysproxy_exe.exists(), "sysproxy.exe does not exist");
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ impl Sysopt {
|
||||
let proxy_manager = EventDrivenProxyManager::global();
|
||||
proxy_manager.notify_app_started();
|
||||
|
||||
log::info!(target: "app", "已启用事件驱动代理守卫");
|
||||
logging!(info, Type::Core, "已启用事件驱动代理守卫");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -224,14 +224,22 @@ impl Sysopt {
|
||||
let mut sysproxy: Sysproxy = match Sysproxy::get_system_proxy() {
|
||||
Ok(sp) => sp,
|
||||
Err(e) => {
|
||||
log::warn!(target: "app", "重置代理时获取系统代理配置失败: {e}, 使用默认配置");
|
||||
logging!(
|
||||
warn,
|
||||
Type::Core,
|
||||
"Warning: 重置代理时获取系统代理配置失败: {e}, 使用默认配置"
|
||||
);
|
||||
Sysproxy::default()
|
||||
}
|
||||
};
|
||||
let mut autoproxy = match Autoproxy::get_auto_proxy() {
|
||||
Ok(ap) => ap,
|
||||
Err(e) => {
|
||||
log::warn!(target: "app", "重置代理时获取自动代理配置失败: {e}, 使用默认配置");
|
||||
logging!(
|
||||
warn,
|
||||
Type::Core,
|
||||
"Warning: 重置代理时获取自动代理配置失败: {e}, 使用默认配置"
|
||||
);
|
||||
Autoproxy::default()
|
||||
}
|
||||
};
|
||||
@@ -265,14 +273,14 @@ impl Sysopt {
|
||||
{
|
||||
if is_enable {
|
||||
if let Err(e) = startup_shortcut::create_shortcut().await {
|
||||
log::error!(target: "app", "创建启动快捷方式失败: {e}");
|
||||
logging!(error, Type::Setup, "创建启动快捷方式失败: {e}");
|
||||
// 如果快捷方式创建失败,回退到原来的方法
|
||||
self.try_original_autostart_method(is_enable);
|
||||
} else {
|
||||
return Ok(());
|
||||
}
|
||||
} else if let Err(e) = startup_shortcut::remove_shortcut().await {
|
||||
log::error!(target: "app", "删除启动快捷方式失败: {e}");
|
||||
logging!(error, Type::Setup, "删除启动快捷方式失败: {e}");
|
||||
self.try_original_autostart_method(is_enable);
|
||||
} else {
|
||||
return Ok(());
|
||||
@@ -307,11 +315,11 @@ impl Sysopt {
|
||||
{
|
||||
match startup_shortcut::is_shortcut_enabled() {
|
||||
Ok(enabled) => {
|
||||
log::info!(target: "app", "快捷方式自启动状态: {enabled}");
|
||||
logging!(info, Type::System, "快捷方式自启动状态: {enabled}");
|
||||
return Ok(enabled);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!(target: "app", "检查快捷方式失败,尝试原来的方法: {e}");
|
||||
logging!(error, Type::System, "检查快捷方式失败,尝试原来的方法: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -322,11 +330,11 @@ impl Sysopt {
|
||||
|
||||
match autostart_manager.is_enabled() {
|
||||
Ok(status) => {
|
||||
log::info!(target: "app", "Auto launch status: {status}");
|
||||
logging!(info, Type::System, "Auto launch status: {status}");
|
||||
Ok(status)
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!(target: "app", "Failed to get auto launch status: {e}");
|
||||
logging!(error, Type::System, "Failed to get auto launch status: {e}");
|
||||
Err(anyhow::anyhow!("Failed to get auto launch status: {}", e))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,8 +62,12 @@ fn should_handle_tray_click() -> bool {
|
||||
*last_click = now;
|
||||
true
|
||||
} else {
|
||||
log::debug!(target: "app", "托盘点击被防抖机制忽略,距离上次点击 {:?}ms",
|
||||
now.duration_since(*last_click).as_millis());
|
||||
logging!(
|
||||
debug,
|
||||
Type::Tray,
|
||||
"托盘点击被防抖机制忽略,距离上次点击 {}ms",
|
||||
now.duration_since(*last_click).as_millis()
|
||||
);
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -207,7 +211,7 @@ singleton_lazy!(Tray, TRAY, Tray::default);
|
||||
impl Tray {
|
||||
pub async fn init(&self) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘初始化");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘初始化");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -215,11 +219,15 @@ impl Tray {
|
||||
|
||||
match self.create_tray_from_handle(app_handle).await {
|
||||
Ok(_) => {
|
||||
log::info!(target: "app", "System tray created successfully");
|
||||
logging!(info, Type::Tray, "System tray created successfully");
|
||||
}
|
||||
Err(e) => {
|
||||
// Don't return error, let application continue running without tray
|
||||
log::warn!(target: "app", "System tray creation failed: {}, Application will continue running without tray icon", e);
|
||||
logging!(
|
||||
warn,
|
||||
Type::Tray,
|
||||
"System tray creation failed: {e}, Application will continue running without tray icon",
|
||||
);
|
||||
}
|
||||
}
|
||||
// TODO: 初始化时,暂时使用此方法更新系统托盘菜单,有效避免代理节点菜单空白
|
||||
@@ -230,7 +238,7 @@ impl Tray {
|
||||
/// 更新托盘点击行为
|
||||
pub async fn update_click_behavior(&self) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘点击行为更新");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘点击行为更新");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -250,7 +258,7 @@ impl Tray {
|
||||
/// 更新托盘菜单
|
||||
pub async fn update_menu(&self) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘菜单更新");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘菜单更新");
|
||||
return Ok(());
|
||||
}
|
||||
// 调整最小更新间隔,确保状态及时刷新
|
||||
@@ -332,11 +340,15 @@ impl Tray {
|
||||
)
|
||||
.await?,
|
||||
));
|
||||
log::debug!(target: "app", "托盘菜单更新成功");
|
||||
logging!(debug, Type::Tray, "托盘菜单更新成功");
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
log::warn!(target: "app", "更新托盘菜单失败: 托盘不存在");
|
||||
logging!(
|
||||
warn,
|
||||
Type::Tray,
|
||||
"Failed to update tray menu: tray not found"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -346,7 +358,7 @@ impl Tray {
|
||||
#[cfg(target_os = "macos")]
|
||||
pub async fn update_icon(&self) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘图标更新");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘图标更新");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -355,7 +367,11 @@ impl Tray {
|
||||
let tray = match app_handle.tray_by_id("main") {
|
||||
Some(tray) => tray,
|
||||
None => {
|
||||
log::warn!(target: "app", "更新托盘图标失败: 托盘不存在");
|
||||
logging!(
|
||||
warn,
|
||||
Type::Tray,
|
||||
"Failed to update tray icon: tray not found"
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
@@ -385,7 +401,7 @@ impl Tray {
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
pub async fn update_icon(&self) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘图标更新");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘图标更新");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -394,7 +410,11 @@ impl Tray {
|
||||
let tray = match app_handle.tray_by_id("main") {
|
||||
Some(tray) => tray,
|
||||
None => {
|
||||
log::warn!(target: "app", "更新托盘图标失败: 托盘不存在");
|
||||
logging!(
|
||||
warn,
|
||||
Type::Tray,
|
||||
"Failed to update tray icon: tray not found"
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
@@ -417,7 +437,7 @@ impl Tray {
|
||||
/// 更新托盘显示状态的函数
|
||||
pub async fn update_tray_display(&self) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘显示状态更新");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘显示状态更新");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -435,7 +455,7 @@ impl Tray {
|
||||
/// 更新托盘提示
|
||||
pub async fn update_tooltip(&self) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘提示更新");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘提示更新");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -491,7 +511,11 @@ impl Tray {
|
||||
if let Some(tray) = app_handle.tray_by_id("main") {
|
||||
let _ = tray.set_tooltip(Some(&tooltip));
|
||||
} else {
|
||||
log::warn!(target: "app", "更新托盘提示失败: 托盘不存在");
|
||||
logging!(
|
||||
warn,
|
||||
Type::Tray,
|
||||
"Failed to update tray tooltip: tray not found"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -499,7 +523,7 @@ impl Tray {
|
||||
|
||||
pub async fn update_part(&self) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘局部更新");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘局部更新");
|
||||
return Ok(());
|
||||
}
|
||||
// self.update_menu().await?;
|
||||
@@ -512,11 +536,11 @@ impl Tray {
|
||||
|
||||
pub async fn create_tray_from_handle(&self, app_handle: &AppHandle) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘创建");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘创建");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
log::info!(target: "app", "正在从AppHandle创建系统托盘");
|
||||
logging!(info, Type::Tray, "正在从AppHandle创建系统托盘");
|
||||
|
||||
// 获取图标
|
||||
let icon_bytes = TrayState::get_common_tray_icon().await.1;
|
||||
@@ -562,7 +586,7 @@ impl Tray {
|
||||
AsyncHandler::spawn(|| async move {
|
||||
let tray_event = { Config::verge().await.latest_ref().tray_event.clone() };
|
||||
let tray_event: String = tray_event.unwrap_or_else(|| "main_window".into());
|
||||
log::debug!(target: "app", "tray event: {tray_event:?}");
|
||||
logging!(debug, Type::Tray, "tray event: {tray_event:?}");
|
||||
|
||||
if let TrayIconEvent::Click {
|
||||
button: MouseButton::Left,
|
||||
@@ -597,14 +621,13 @@ impl Tray {
|
||||
});
|
||||
});
|
||||
tray.on_menu_event(on_menu_event);
|
||||
log::info!(target: "app", "系统托盘创建成功");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 托盘统一的状态更新函数
|
||||
pub async fn update_all_states(&self) -> Result<()> {
|
||||
if handle::Handle::global().is_exiting() {
|
||||
log::debug!(target: "app", "应用正在退出,跳过托盘状态更新");
|
||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘状态更新");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -731,7 +754,9 @@ fn create_subcreate_proxy_menu_item(
|
||||
is_selected,
|
||||
None::<&str>,
|
||||
)
|
||||
.map_err(|e| log::warn!(target: "app", "创建代理菜单项失败: {}", e))
|
||||
.map_err(|e| {
|
||||
logging!(warn, Type::Tray, "Failed to create proxy menu item: {}", e)
|
||||
})
|
||||
.ok()
|
||||
})
|
||||
.collect();
|
||||
@@ -773,7 +798,12 @@ fn create_subcreate_proxy_menu_item(
|
||||
let insertion_index = submenus.len();
|
||||
submenus.push((group_name.into(), insertion_index, submenu));
|
||||
} else {
|
||||
log::warn!(target: "app", "创建代理组子菜单失败: {}", group_name);
|
||||
logging!(
|
||||
warn,
|
||||
Type::Tray,
|
||||
"Failed to create proxy group submenu: {}",
|
||||
group_name
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1166,7 +1196,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
|
||||
feat::change_clash_mode(mode.into()).await;
|
||||
}
|
||||
MenuIds::DASHBOARD => {
|
||||
log::info!(target: "app", "托盘菜单点击: 打开窗口");
|
||||
logging!(info, Type::Tray, "托盘菜单点击: 打开窗口");
|
||||
|
||||
if !should_handle_tray_click() {
|
||||
return;
|
||||
@@ -1183,7 +1213,11 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
|
||||
}
|
||||
MenuIds::CLOSE_ALL_CONNECTIONS => {
|
||||
if let Err(err) = handle::Handle::mihomo().await.close_all_connections().await {
|
||||
log::error!(target: "app", "Failed to close all connections from tray: {err}");
|
||||
logging!(
|
||||
error,
|
||||
Type::Tray,
|
||||
"Failed to close all connections from tray: {err}"
|
||||
);
|
||||
}
|
||||
}
|
||||
MenuIds::COPY_ENV => feat::copy_clash_env().await,
|
||||
@@ -1236,12 +1270,25 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
log::info!(target: "app", "切换代理成功: {} -> {}", group_name, proxy_name);
|
||||
logging!(
|
||||
info,
|
||||
Type::Tray,
|
||||
"切换代理成功: {} -> {}",
|
||||
group_name,
|
||||
proxy_name
|
||||
);
|
||||
let _ = handle::Handle::app_handle()
|
||||
.emit("verge://refresh-proxy-config", ());
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!(target: "app", "切换代理失败: {} -> {}, 错误: {:?}", group_name, proxy_name, e);
|
||||
logging!(
|
||||
error,
|
||||
Type::Tray,
|
||||
"切换代理失败: {} -> {}, 错误: {:?}",
|
||||
group_name,
|
||||
proxy_name,
|
||||
e
|
||||
);
|
||||
|
||||
// Fallback to IPC update
|
||||
if (handle::Handle::mihomo()
|
||||
@@ -1250,7 +1297,13 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
|
||||
.await)
|
||||
.is_ok()
|
||||
{
|
||||
log::info!(target: "app", "代理切换回退成功: {} -> {}", group_name, proxy_name);
|
||||
logging!(
|
||||
info,
|
||||
Type::Tray,
|
||||
"代理切换回退成功: {} -> {}",
|
||||
group_name,
|
||||
proxy_name
|
||||
);
|
||||
|
||||
let app_handle = handle::Handle::app_handle();
|
||||
let _ = app_handle.emit("verge://force-refresh-proxies", ());
|
||||
@@ -1264,7 +1317,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
|
||||
|
||||
// Ensure tray state update is awaited and properly handled
|
||||
if let Err(e) = Tray::global().update_all_states().await {
|
||||
log::warn!(target: "app", "更新托盘状态失败: {e}");
|
||||
logging!(warn, Type::Tray, "Failed to update tray state: {e}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user