refactor: logger fetch logic

This commit is contained in:
huzibaca
2024-11-18 05:58:06 +08:00
parent 132641f269
commit 221b732472
9 changed files with 50 additions and 126 deletions

View File

@@ -8,7 +8,7 @@ use crate::{ret_err, wrap_err};
use anyhow::{Context, Result};
use network_interface::NetworkInterface;
use serde_yaml::Mapping;
use std::collections::{HashMap, VecDeque};
use std::collections::HashMap;
use sysproxy::{Autoproxy, Sysproxy};
type CmdResult<T = ()> = Result<T, String>;
use reqwest_dav::list_cmd::ListFile;
@@ -216,11 +216,6 @@ pub fn get_auto_proxy() -> CmdResult<Mapping> {
Ok(map)
}
#[tauri::command]
pub fn get_clash_logs() -> CmdResult<VecDeque<String>> {
Ok(logger::Logger::global().get_log())
}
#[tauri::command]
pub fn open_app_dir() -> CmdResult<()> {
let app_dir = wrap_err!(dirs::app_home_dir())?;

View File

@@ -96,8 +96,7 @@ pub fn parse_log(log: String) -> String {
log
}
/// 缩短clash -t的错误输出
/// 仅适配 clash p核 8-26、clash meta 1.13.1
#[allow(dead_code)]
pub fn parse_check_output(log: String) -> String {
let t = log.find("time=");
let m = log.find("msg=");

View File

@@ -1,5 +1,5 @@
use crate::config::*;
use crate::core::{clash_api, handle, logger::Logger, service};
use crate::core::{clash_api, handle, service};
use crate::log_err;
use crate::utils::dirs;
use anyhow::{bail, Result};
@@ -43,24 +43,13 @@ impl CoreManager {
let test_dir = dirs::path_to_str(&test_dir)?;
let app_handle = handle::Handle::global().app_handle().unwrap();
let output = app_handle
let _ = app_handle
.shell()
.sidecar(clash_core)?
.args(["-t", "-d", test_dir, "-f", config_path])
.output()
.await?;
if !output.status.success() {
let stdout = String::from_utf8(output.stdout).unwrap_or_default();
let error = clash_api::parse_check_output(stdout.clone());
let error = match !error.is_empty() {
true => error,
false => stdout.clone(),
};
Logger::global().set_log(stdout.clone());
bail!("{error}");
}
Ok(())
}
@@ -136,9 +125,6 @@ impl CoreManager {
self.check_config().await?;
// 清掉旧日志
Logger::global().clear_log();
match self.restart_core().await {
Ok(_) => {
Config::verge().apply();

View File

@@ -1,36 +0,0 @@
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use std::{collections::VecDeque, sync::Arc};
const LOGS_QUEUE_LEN: usize = 100;
pub struct Logger {
log_data: Arc<Mutex<VecDeque<String>>>,
}
impl Logger {
pub fn global() -> &'static Logger {
static LOGGER: OnceCell<Logger> = OnceCell::new();
LOGGER.get_or_init(|| Logger {
log_data: Arc::new(Mutex::new(VecDeque::with_capacity(LOGS_QUEUE_LEN + 10))),
})
}
pub fn get_log(&self) -> VecDeque<String> {
self.log_data.lock().clone()
}
pub fn set_log(&self, text: String) {
let mut logs = self.log_data.lock();
if logs.len() > LOGS_QUEUE_LEN {
logs.pop_front();
}
logs.push_back(text);
}
pub fn clear_log(&self) {
let mut logs = self.log_data.lock();
logs.clear();
}
}

View File

@@ -4,7 +4,6 @@ pub mod clash_api;
mod core;
pub mod handle;
pub mod hotkey;
pub mod logger;
pub mod service;
pub mod sysopt;
pub mod timer;

View File

@@ -88,7 +88,6 @@ pub fn run() {
cmds::restart_app,
// clash
cmds::get_clash_info,
cmds::get_clash_logs,
cmds::patch_clash_config,
cmds::change_clash_core,
cmds::get_runtime_config,