mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-10-15 05:40:47 +08:00
feat(logging): introduce ModuleFilter with exclusion support and logging tauri_plugin_mihomo (#7637)
* feat(logging): introduce ModuleFilter with exclusion support * refactor(logging): simplify exclude check using if-let chain
This commit is contained in:
@@ -97,23 +97,35 @@ pub fn write_sidecar_log(
|
||||
let _ = writer.write(now, &record);
|
||||
}
|
||||
|
||||
pub struct NoModuleFilter<'a>(pub Vec<&'a str>);
|
||||
pub struct ModuleFilter<'a> {
|
||||
block: Vec<&'a str>,
|
||||
exclude: Option<Vec<&'a str>>,
|
||||
}
|
||||
|
||||
impl<'a> ModuleFilter<'a> {
|
||||
pub fn new(block: Vec<&'a str>, exclude: Option<Vec<&'a str>>) -> Self {
|
||||
Self { block, exclude }
|
||||
}
|
||||
|
||||
impl<'a> NoModuleFilter<'a> {
|
||||
#[inline]
|
||||
pub fn filter(&self, record: &Record) -> bool {
|
||||
if let Some(module) = record.module_path() {
|
||||
for blocked in self.0.iter() {
|
||||
if module.len() >= blocked.len() && module.as_bytes()[..blocked.len()] == blocked.as_bytes()[..] {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
let Some(module) = record.module_path() else {
|
||||
return true;
|
||||
};
|
||||
|
||||
// 优先检查排除(白名单)
|
||||
if let Some(excludes) = &self.exclude
|
||||
&& excludes.iter().any(|e| module.starts_with(e))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
true
|
||||
|
||||
// 再检查阻止(黑名单)
|
||||
!self.block.iter().any(|b| module.starts_with(b))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> LogLineFilter for NoModuleFilter<'a> {
|
||||
impl<'a> LogLineFilter for ModuleFilter<'a> {
|
||||
#[inline]
|
||||
fn write(
|
||||
&self,
|
||||
|
||||
@@ -94,7 +94,10 @@ impl Logger {
|
||||
filter_modules.push("tauri");
|
||||
#[cfg(feature = "tracing")]
|
||||
filter_modules.extend(["tauri_plugin_mihomo", "kode_bridge"]);
|
||||
let logger = logger.filter(Box::new(clash_verge_logging::NoModuleFilter(filter_modules)));
|
||||
let logger = logger.filter(Box::new(clash_verge_logging::ModuleFilter::new(
|
||||
filter_modules,
|
||||
Some(vec!["tauri_plugin_mihomo"]),
|
||||
)));
|
||||
|
||||
let handle = logger.start()?;
|
||||
*self.handle.lock() = Some(handle);
|
||||
|
||||
Reference in New Issue
Block a user