fix(tray): resolve tray open log failed (#6301)

* fix(tray): resolve tray open log failed

* fix(ci): allow missing_const_for_fn

* fix(windows): run snapshot log cleanup unconditionally and cover all temp dirs

* refactor: simplify snapshot_path

* fix: clippy

* refactor: delete_snapshot_logs

---------

Co-authored-by: Slinetrac <realakayuki@gmail.com>
This commit is contained in:
AetherWing 2026-02-12 11:53:36 +08:00 committed by GitHub
parent 81c56d46c1
commit 847a0a6afd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 2 deletions

View File

@ -11,6 +11,7 @@
- 修复首页当前节点图标语义显示不一致
- 修复使用 URL scheme 导入订阅时没有及时重载配置
- 修复规则界面里的行号展示逻辑
- 修复 Windows 托盘打开日志失败
<details>
<summary><strong> ✨ 新增功能 </strong></summary>

View File

@ -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()
}
/// 打开/关闭开发者工具

View File

@ -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<PathBuf> {
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)
}

View File

@ -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();