refactor(logger): replace ClashLogger with CLASH_LOGGER and update log handling; improve log retrieval and management

This commit is contained in:
Tunglies
2025-10-29 17:58:02 +08:00
Unverified
parent 2e9f6dd174
commit f4de4738f1
8 changed files with 120 additions and 96 deletions

1
.gitignore vendored
View File

@@ -11,3 +11,4 @@ scripts/_env.sh
.idea
.old
.eslintcache
target

149
src-tauri/Cargo.lock generated
View File

@@ -147,11 +147,17 @@ dependencies = [
"objc2-foundation 0.3.2",
"parking_lot 0.12.5",
"percent-encoding",
"windows-sys 0.59.0",
"windows-sys 0.52.0",
"wl-clipboard-rs",
"x11rb",
]
[[package]]
name = "arraydeque"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236"
[[package]]
name = "arrayvec"
version = "0.7.6"
@@ -1164,18 +1170,21 @@ dependencies = [
[[package]]
name = "clash_verge_logger"
version = "0.1.0"
source = "git+https://github.com/clash-verge-rev/clash-verge-logger#256dc7441f3d0a0c1faa89e345379b32308bc815"
version = "0.2.0"
source = "git+https://github.com/clash-verge-rev/clash-verge-logger#9bb189b5b5c4c2eee35168ff4997e8fb10901c81"
dependencies = [
"arraydeque",
"compact_str",
"flexi_logger",
"log",
"nu-ansi-term",
"tokio",
]
[[package]]
name = "clash_verge_service_ipc"
version = "2.0.18"
source = "git+https://github.com/clash-verge-rev/clash-verge-service-ipc#381fee14ce5c69274c547b6b18819452d97fb2b2"
version = "2.0.19"
source = "git+https://github.com/clash-verge-rev/clash-verge-service-ipc#1d9b8a6f5ea9a7f8c52ffef814b51f48d6cdad33"
dependencies = [
"anyhow",
"compact_str",
@@ -1234,7 +1243,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
dependencies = [
"lazy_static",
"windows-sys 0.59.0",
"windows-sys 0.48.0",
]
[[package]]
@@ -1256,9 +1265,11 @@ dependencies = [
"castaway 0.2.4",
"cfg-if",
"itoa",
"rkyv",
"rustversion",
"ryu",
"serde",
"smallvec",
"static_assertions",
]
@@ -1924,7 +1935,7 @@ dependencies = [
"libc",
"option-ext",
"redox_users 0.5.2",
"windows-sys 0.59.0",
"windows-sys 0.60.2",
]
[[package]]
@@ -2180,7 +2191,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.59.0",
"windows-sys 0.52.0",
]
[[package]]
@@ -3365,7 +3376,7 @@ dependencies = [
"js-sys",
"log",
"wasm-bindgen",
"windows-core 0.62.2",
"windows-core 0.58.0",
]
[[package]]
@@ -4244,6 +4255,26 @@ dependencies = [
"windows-sys 0.60.2",
]
[[package]]
name = "munge"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e17401f259eba956ca16491461b6e8f72913a0a114e39736ce404410f915a0c"
dependencies = [
"munge_macro",
]
[[package]]
name = "munge_macro"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4568f25ccbd45ab5d5603dc34318c1ec56b117531781260002151b8530a9f931"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.108",
]
[[package]]
name = "nanoid"
version = "0.4.0"
@@ -4425,7 +4456,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys 0.59.0",
"windows-sys 0.60.2",
]
[[package]]
@@ -5644,6 +5675,26 @@ version = "2.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33cb294fe86a74cbcf50d4445b37da762029549ebeea341421c7c70370f86cac"
[[package]]
name = "ptr_meta"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b9a0cf95a1196af61d4f1cbdab967179516d9a4a4312af1f31948f8f6224a79"
dependencies = [
"ptr_meta_derive",
]
[[package]]
name = "ptr_meta_derive"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7347867d0a7e1208d93b46767be83e2b8f978c3dad35f775ac8d8847551d6fe1"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.108",
]
[[package]]
name = "publicsuffix"
version = "2.3.0"
@@ -5739,7 +5790,7 @@ dependencies = [
"once_cell",
"socket2 0.5.10",
"tracing",
"windows-sys 0.59.0",
"windows-sys 0.52.0",
]
[[package]]
@@ -5757,6 +5808,15 @@ version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rancor"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a063ea72381527c2a0561da9c80000ef822bdd7c3241b1cc1b12100e3df081ee"
dependencies = [
"ptr_meta",
]
[[package]]
name = "rand"
version = "0.7.3"
@@ -5998,6 +6058,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "rend"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cadadef317c2f20755a64d7fdc48f9e7178ee6b0e1f7fce33fa60f1d68a276e6"
[[package]]
name = "reqwest"
version = "0.12.24"
@@ -6117,6 +6183,30 @@ dependencies = [
"portable-atomic-util",
]
[[package]]
name = "rkyv"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35a640b26f007713818e9a9b65d34da1cf58538207b052916a83d80e43f3ffa4"
dependencies = [
"munge",
"ptr_meta",
"rancor",
"rend",
"rkyv_derive",
]
[[package]]
name = "rkyv_derive"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd83f5f173ff41e00337d97f6572e416d022ef8a19f371817259ae960324c482"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.108",
]
[[package]]
name = "rs-snowflake"
version = "0.6.0"
@@ -6193,7 +6283,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.4.15",
"windows-sys 0.59.0",
"windows-sys 0.52.0",
]
[[package]]
@@ -6206,7 +6296,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.11.0",
"windows-sys 0.59.0",
"windows-sys 0.52.0",
]
[[package]]
@@ -7709,7 +7799,7 @@ dependencies = [
"getrandom 0.3.4",
"once_cell",
"rustix 1.1.2",
"windows-sys 0.59.0",
"windows-sys 0.52.0",
]
[[package]]
@@ -9157,19 +9247,6 @@ dependencies = [
"windows-strings 0.4.2",
]
[[package]]
name = "windows-core"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement 0.60.2",
"windows-interface 0.59.3",
"windows-link 0.2.1",
"windows-result 0.4.1",
"windows-strings 0.5.1",
]
[[package]]
name = "windows-future"
version = "0.2.1"
@@ -9276,15 +9353,6 @@ dependencies = [
"windows-link 0.1.3",
]
[[package]]
name = "windows-result"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
dependencies = [
"windows-link 0.2.1",
]
[[package]]
name = "windows-strings"
version = "0.1.0"
@@ -9304,15 +9372,6 @@ dependencies = [
"windows-link 0.1.3",
]
[[package]]
name = "windows-strings"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
dependencies = [
"windows-link 0.2.1",
]
[[package]]
name = "windows-sys"
version = "0.45.0"

View File

@@ -88,7 +88,7 @@ tauri-plugin-mihomo = { git = "https://github.com/clash-verge-rev/tauri-plugin-m
clash_verge_logger = { git = "https://github.com/clash-verge-rev/clash-verge-logger" }
async-trait = "0.1.89"
smartstring = { version = "1.0.1", features = ["serde"] }
clash_verge_service_ipc = { version = "2.0.18", features = [
clash_verge_service_ipc = { version = "2.0.19", features = [
"client",
], git = "https://github.com/clash-verge-rev/clash-verge-service-ipc" }

View File

@@ -1,5 +1,3 @@
use std::collections::VecDeque;
use super::CmdResult;
use crate::{
cmd::StringifyErr,
@@ -275,7 +273,7 @@ pub async fn validate_dns_config() -> CmdResult<(bool, String)> {
}
#[tauri::command]
pub async fn get_clash_logs() -> CmdResult<VecDeque<CompactString>> {
pub async fn get_clash_logs() -> CmdResult<Vec<CompactString>> {
let logs = CoreManager::global()
.get_clash_logs()
.await

View File

@@ -1,38 +1,6 @@
use std::{collections::VecDeque, sync::Arc};
use std::sync::Arc;
use compact_str::CompactString;
use once_cell::sync::OnceCell;
use parking_lot::{RwLock, RwLockReadGuard};
use clash_verge_logger::AsyncLogger;
use once_cell::sync::Lazy;
const LOGS_QUEUE_LEN: usize = 100;
pub struct ClashLogger {
logs: Arc<RwLock<VecDeque<CompactString>>>,
}
impl ClashLogger {
pub fn global() -> &'static ClashLogger {
static LOGGER: OnceCell<ClashLogger> = OnceCell::new();
LOGGER.get_or_init(|| ClashLogger {
logs: Arc::new(RwLock::new(VecDeque::with_capacity(LOGS_QUEUE_LEN + 10))),
})
}
pub fn get_logs(&self) -> RwLockReadGuard<'_, VecDeque<CompactString>> {
self.logs.read()
}
pub fn append_log(&self, text: CompactString) {
let mut logs = self.logs.write();
if logs.len() > LOGS_QUEUE_LEN {
logs.pop_front();
}
logs.push_back(text);
}
pub fn clear_logs(&self) {
let mut logs = self.logs.write();
logs.clear();
}
}
pub static CLASH_LOGGER: Lazy<Arc<AsyncLogger>> = Lazy::new(|| Arc::new(AsyncLogger::new()));

View File

@@ -1,7 +1,7 @@
use super::{CoreManager, RunningMode};
use crate::{
core::{
logger::ClashLogger,
logger::CLASH_LOGGER,
service::{SERVICE_MANAGER, ServiceStatus},
},
logging,
@@ -21,7 +21,7 @@ impl CoreManager {
}
pub async fn stop_core(&self) -> Result<()> {
ClashLogger::global().clear_logs();
CLASH_LOGGER.clear_logs().await;
match *self.get_running_mode() {
RunningMode::Service => self.stop_core_by_service().await,

View File

@@ -2,7 +2,7 @@ use super::{CoreManager, RunningMode};
use crate::{
AsyncHandler,
config::Config,
core::{handle, logger::ClashLogger, service},
core::{handle, logger::CLASH_LOGGER, service},
logging,
process::CommandChildGuard,
utils::{
@@ -16,15 +16,14 @@ use compact_str::CompactString;
use flexi_logger::DeferredNow;
use log::Level;
use scopeguard::defer;
use std::collections::VecDeque;
use tauri_plugin_shell::ShellExt;
impl CoreManager {
pub async fn get_clash_logs(&self) -> Result<VecDeque<CompactString>> {
pub async fn get_clash_logs(&self) -> Result<Vec<CompactString>> {
match *self.get_running_mode() {
RunningMode::Service => service::get_clash_logs_by_service().await,
RunningMode::Sidecar => Ok(ClashLogger::global().get_logs().clone()),
RunningMode::NotRunning => Ok(VecDeque::new()),
RunningMode::Sidecar => Ok(CLASH_LOGGER.get_logs().await),
RunningMode::NotRunning => Ok(Vec::new()),
}
}
@@ -65,7 +64,7 @@ impl CoreManager {
let message = CompactString::from(String::from_utf8_lossy(&line).as_ref());
let w = shared_writer.lock().await;
write_sidecar_log(w, &mut now, Level::Error, &message);
ClashLogger::global().append_log(message);
CLASH_LOGGER.append_log(message).await;
}
tauri_plugin_shell::process::CommandEvent::Terminated(term) => {
let mut now = DeferredNow::default();
@@ -78,7 +77,7 @@ impl CoreManager {
};
let w = shared_writer.lock().await;
write_sidecar_log(w, &mut now, Level::Info, &message);
ClashLogger::global().clear_logs();
CLASH_LOGGER.clear_logs().await;
break;
}
_ => {}

View File

@@ -8,7 +8,6 @@ use clash_verge_service_ipc::CoreConfig;
use compact_str::CompactString;
use once_cell::sync::Lazy;
use std::{
collections::VecDeque,
env::current_exe,
path::{Path, PathBuf},
process::Command as StdCommand,
@@ -394,7 +393,7 @@ pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> {
start_with_existing_service(config_file).await
}
pub(super) async fn get_clash_logs_by_service() -> Result<VecDeque<CompactString>> {
pub(super) async fn get_clash_logs_by_service() -> Result<Vec<CompactString>> {
logging!(info, Type::Service, "正在获取服务模式下的 Clash 日志");
let response = clash_verge_service_ipc::get_clash_logs()