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
parent 2e9f6dd174
commit f4de4738f1
8 changed files with 120 additions and 96 deletions

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