refactor: replace log macros with logging utility for IPC monitoring

This commit is contained in:
Tunglies
2025-08-15 20:41:45 +08:00
parent 2aa629ff5d
commit cf1fbb63c4

View File

@@ -4,8 +4,10 @@ use crate::{
core::*, core::*,
feat, feat,
ipc::{self, IpcManager}, ipc::{self, IpcManager},
logging,
process::AsyncHandler, process::AsyncHandler,
state::proxy::ProxyRequestCache, state::proxy::ProxyRequestCache,
utils::logging::Type,
wrap_err, wrap_err,
}; };
use serde_yaml::Mapping; use serde_yaml::Mapping;
@@ -399,7 +401,6 @@ pub async fn close_all_clash_connections() -> CmdResult {
/// 获取流量数据 (使用新的IPC流式监控) /// 获取流量数据 (使用新的IPC流式监控)
#[tauri::command] #[tauri::command]
pub async fn get_traffic_data() -> CmdResult<serde_json::Value> { pub async fn get_traffic_data() -> CmdResult<serde_json::Value> {
log::info!(target: "app", "开始获取流量数据 (IPC流式)");
let traffic = crate::ipc::get_current_traffic().await; let traffic = crate::ipc::get_current_traffic().await;
let result = serde_json::json!({ let result = serde_json::json!({
"up": traffic.total_up, "up": traffic.total_up,
@@ -408,15 +409,12 @@ pub async fn get_traffic_data() -> CmdResult<serde_json::Value> {
"down_rate": traffic.down_rate, "down_rate": traffic.down_rate,
"last_updated": traffic.last_updated.elapsed().as_secs() "last_updated": traffic.last_updated.elapsed().as_secs()
}); });
log::info!(target: "app", "获取流量数据结果: up={}, down={}, up_rate={}, down_rate={}",
traffic.total_up, traffic.total_down, traffic.up_rate, traffic.down_rate);
Ok(result) Ok(result)
} }
/// 获取内存数据 (使用新的IPC流式监控) /// 获取内存数据 (使用新的IPC流式监控)
#[tauri::command] #[tauri::command]
pub async fn get_memory_data() -> CmdResult<serde_json::Value> { pub async fn get_memory_data() -> CmdResult<serde_json::Value> {
log::info!(target: "app", "开始获取内存数据 (IPC流式)");
let memory = crate::ipc::get_current_memory().await; let memory = crate::ipc::get_current_memory().await;
let usage_percent = if memory.oslimit > 0 { let usage_percent = if memory.oslimit > 0 {
(memory.inuse as f64 / memory.oslimit as f64) * 100.0 (memory.inuse as f64 / memory.oslimit as f64) * 100.0
@@ -429,36 +427,34 @@ pub async fn get_memory_data() -> CmdResult<serde_json::Value> {
"usage_percent": usage_percent, "usage_percent": usage_percent,
"last_updated": memory.last_updated.elapsed().as_secs() "last_updated": memory.last_updated.elapsed().as_secs()
}); });
log::info!(target: "app", "获取内存数据结果: inuse={}, oslimit={}, usage={}%",
memory.inuse, memory.oslimit, usage_percent);
Ok(result) Ok(result)
} }
/// 启动流量监控服务 (IPC流式监控自动启动此函数为兼容性保留) /// 启动流量监控服务 (IPC流式监控自动启动此函数为兼容性保留)
#[tauri::command] #[tauri::command]
pub async fn start_traffic_service() -> CmdResult { pub async fn start_traffic_service() -> CmdResult {
log::info!(target: "app", "启动流量监控服务 (IPC流式监控)"); logging!(trace, Type::Ipc, "启动流量监控服务 (IPC流式监控)");
// 新的IPC监控在首次访问时自动启动 // 新的IPC监控在首次访问时自动启动
// 触发一次访问以确保监控器已初始化 // 触发一次访问以确保监控器已初始化
let _ = crate::ipc::get_current_traffic().await; let _ = crate::ipc::get_current_traffic().await;
let _ = crate::ipc::get_current_memory().await; let _ = crate::ipc::get_current_memory().await;
log::info!(target: "app", "IPC流式监控已激活"); logging!(info, Type::Ipc, "IPC流式监控已激活");
Ok(()) Ok(())
} }
/// 停止流量监控服务 (IPC流式监控无需显式停止此函数为兼容性保留) /// 停止流量监控服务 (IPC流式监控无需显式停止此函数为兼容性保留)
#[tauri::command] #[tauri::command]
pub async fn stop_traffic_service() -> CmdResult { pub async fn stop_traffic_service() -> CmdResult {
log::info!(target: "app", "停止流量监控服务请求 (IPC流式监控)"); logging!(trace, Type::Ipc, "停止流量监控服务请求 (IPC流式监控)");
// 新的IPC监控是持久的无需显式停止 // 新的IPC监控是持久的无需显式停止
log::info!(target: "app", "IPC流式监控继续运行"); logging!(info, Type::Ipc, "IPC流式监控继续运行");
Ok(()) Ok(())
} }
/// 获取格式化的流量数据 (包含单位,便于前端显示) /// 获取格式化的流量数据 (包含单位,便于前端显示)
#[tauri::command] #[tauri::command]
pub async fn get_formatted_traffic_data() -> CmdResult<serde_json::Value> { pub async fn get_formatted_traffic_data() -> CmdResult<serde_json::Value> {
log::info!(target: "app", "获取格式化流量数据"); logging!(trace, Type::Ipc, "获取格式化流量数据");
let (up_rate, down_rate, total_up, total_down, is_fresh) = let (up_rate, down_rate, total_up, total_down, is_fresh) =
crate::ipc::get_formatted_traffic().await; crate::ipc::get_formatted_traffic().await;
let result = serde_json::json!({ let result = serde_json::json!({
@@ -468,16 +464,18 @@ pub async fn get_formatted_traffic_data() -> CmdResult<serde_json::Value> {
"total_down_formatted": total_down, "total_down_formatted": total_down,
"is_fresh": is_fresh "is_fresh": is_fresh
}); });
log::debug!(target: "app", "格式化流量数据: ↑{up_rate}/s ↓{down_rate}/s (总计: ↑{total_up} ↓{total_down})"); logging!(
// Clippy: variables can be used directly in the format string debug,
// log::debug!(target: "app", "格式化流量数据: ↑{up_rate}/s ↓{down_rate}/s (总计: ↑{total_up} ↓{total_down})"); Type::Ipc,
"格式化流量数据: ↑{up_rate}/s ↓{down_rate}/s (总计: ↑{total_up} ↓{total_down})"
);
Ok(result) Ok(result)
} }
/// 获取格式化的内存数据 (包含单位,便于前端显示) /// 获取格式化的内存数据 (包含单位,便于前端显示)
#[tauri::command] #[tauri::command]
pub async fn get_formatted_memory_data() -> CmdResult<serde_json::Value> { pub async fn get_formatted_memory_data() -> CmdResult<serde_json::Value> {
log::info!(target: "app", "获取格式化内存数据"); logging!(info, Type::Ipc, "获取格式化内存数据");
let (inuse, oslimit, usage_percent, is_fresh) = crate::ipc::get_formatted_memory().await; let (inuse, oslimit, usage_percent, is_fresh) = crate::ipc::get_formatted_memory().await;
let result = serde_json::json!({ let result = serde_json::json!({
"inuse_formatted": inuse, "inuse_formatted": inuse,
@@ -485,16 +483,18 @@ pub async fn get_formatted_memory_data() -> CmdResult<serde_json::Value> {
"usage_percent": usage_percent, "usage_percent": usage_percent,
"is_fresh": is_fresh "is_fresh": is_fresh
}); });
log::debug!(target: "app", "格式化内存数据: {inuse} / {oslimit} ({usage_percent:.1}%)"); logging!(
// Clippy: variables can be used directly in the format string debug,
// log::debug!(target: "app", "格式化内存数据: {inuse} / {oslimit} ({usage_percent:.1}%)"); Type::Ipc,
"格式化内存数据: {inuse} / {oslimit} ({usage_percent:.1}%)"
);
Ok(result) Ok(result)
} }
/// 获取系统监控概览 (流量+内存,便于前端一次性获取所有状态) /// 获取系统监控概览 (流量+内存,便于前端一次性获取所有状态)
#[tauri::command] #[tauri::command]
pub async fn get_system_monitor_overview() -> CmdResult<serde_json::Value> { pub async fn get_system_monitor_overview() -> CmdResult<serde_json::Value> {
log::debug!(target: "app", "获取系统监控概览"); logging!(debug, Type::Ipc, "获取系统监控概览");
// 并发获取流量和内存数据 // 并发获取流量和内存数据
let (traffic, memory) = tokio::join!( let (traffic, memory) = tokio::join!(