diff --git a/Changelog.md b/Changelog.md index 2d704f5e3..c7e4c1498 100644 --- a/Changelog.md +++ b/Changelog.md @@ -11,6 +11,7 @@ - 修复首页当前节点图标语义显示不一致 - 修复使用 URL scheme 导入订阅时没有及时重载配置 - 修复规则界面里的行号展示逻辑 +- 修复 Windows 托盘打开日志失败
✨ 新增功能 diff --git a/src-tauri/src/cmd/app.rs b/src-tauri/src/cmd/app.rs index 9a2fe39ab..77a98ab69 100644 --- a/src-tauri/src/cmd/app.rs +++ b/src-tauri/src/cmd/app.rs @@ -45,14 +45,20 @@ pub fn open_web_url(url: String) -> CmdResult<()> { /// 打开 Verge 最新日志 #[tauri::command] pub async fn open_app_log() -> CmdResult<()> { - open::that(dirs::app_latest_log().stringify_err()?).stringify_err() + let log_path = dirs::app_latest_log().stringify_err()?; + #[cfg(target_os = "windows")] + let log_path = crate::utils::help::snapshot_path(&log_path).stringify_err()?; + open::that(log_path).stringify_err() } // TODO 后续可以为前端提供接口,当前作为托盘菜单使用 /// 打开 Clash 最新日志 #[tauri::command] pub async fn open_core_log() -> CmdResult<()> { - open::that(dirs::clash_latest_log().stringify_err()?).stringify_err() + let log_path = dirs::clash_latest_log().stringify_err()?; + #[cfg(target_os = "windows")] + let log_path = crate::utils::help::snapshot_path(&log_path).stringify_err()?; + open::that(log_path).stringify_err() } /// 打开/关闭开发者工具 diff --git a/src-tauri/src/utils/help.rs b/src-tauri/src/utils/help.rs index 7431a3870..c0d2e18c8 100644 --- a/src-tauri/src/utils/help.rs +++ b/src-tauri/src/utils/help.rs @@ -4,6 +4,8 @@ use clash_verge_logging::{Type, logging}; use nanoid::nanoid; use serde::{Serialize, de::DeserializeOwned}; use serde_yaml_ng::Mapping; +#[cfg(target_os = "windows")] +use std::path::Path; use std::{path::PathBuf, str::FromStr}; /// read data from yaml as struct T @@ -134,3 +136,24 @@ pub fn linux_elevator() -> String { Err(_) => "sudo".to_string(), } } + +#[cfg(target_os = "windows")] +/// copy the file to the dist path and return the dist path +pub fn snapshot_path(original_path: &Path) -> Result { + let temp_dir = original_path + .parent() + .ok_or_else(|| anyhow!("Invalid log path"))? + .join("temp"); + + std::fs::create_dir_all(&temp_dir)?; + + let temp_path = temp_dir.join(format!( + "{}_{}.log", + original_path.file_stem().unwrap_or_default().to_string_lossy(), + chrono::Local::now().format("%Y-%m-%d_%H-%M-%S") + )); + + std::fs::copy(original_path, &temp_path)?; + + Ok(temp_path) +} diff --git a/src-tauri/src/utils/init.rs b/src-tauri/src/utils/init.rs index c3daa5668..43b61c8f2 100644 --- a/src-tauri/src/utils/init.rs +++ b/src-tauri/src/utils/init.rs @@ -13,11 +13,35 @@ use crate::{ use anyhow::Result; use chrono::{Local, TimeZone as _}; use clash_verge_logging::Type; +#[cfg(target_os = "windows")] +use std::path::Path; use std::{path::PathBuf, str::FromStr as _}; use tauri_plugin_shell::ShellExt as _; use tokio::fs; use tokio::fs::DirEntry; +#[cfg(target_os = "windows")] +async fn delete_snapshot_logs(log_dir: &Path) -> Result<()> { + let temp_dirs = [ + log_dir.join("temp"), + log_dir.join("service").join("temp"), + log_dir.join("sidecar").join("temp"), + ]; + + for temp_dir in temp_dirs.iter().filter(|d| d.exists()) { + let mut entries = fs::read_dir(temp_dir).await?; + while let Some(entry) = entries.next_entry().await? { + let path = entry.path(); + if path.extension().and_then(|s| s.to_str()) == Some("log") { + let _ = path.remove_if_exists().await; + logging!(info, Type::Setup, "delete snapshot log file: {}", path.display()); + } + } + } + + Ok(()) +} + // TODO flexi_logger 提供了最大保留天数,或许我们应该用内置删除log文件 /// 删除log文件 pub async fn delete_log() -> Result<()> { @@ -26,6 +50,9 @@ pub async fn delete_log() -> Result<()> { return Ok(()); } + #[cfg(target_os = "windows")] + delete_snapshot_logs(&log_dir).await?; + let auto_log_clean = { let verge = Config::verge().await; let verge = verge.data_arc();