mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
refactor: enhance logging system and add new development commands (#4803)
* refactor: enhance logging system and add new development commands * refactor: add cfg-if dependency and improve logging configuration
This commit is contained in:
@@ -1,4 +1,14 @@
|
||||
use std::fmt;
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "tauri-dev")] {
|
||||
use std::fmt;
|
||||
} else {
|
||||
#[cfg(feature = "verge-dev")]
|
||||
use nu_ansi_term::Color;
|
||||
use std::{fmt, io::Write, thread};
|
||||
use flexi_logger::DeferredNow;
|
||||
use log::{LevelFilter, Record};
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Type {
|
||||
@@ -107,7 +117,8 @@ macro_rules! wrap_err {
|
||||
macro_rules! logging {
|
||||
// 带 println 的版本(支持格式化参数)
|
||||
($level:ident, $type:expr, true, $($arg:tt)*) => {
|
||||
println!("{} {}", $type, format_args!($($arg)*));
|
||||
// We dont need println here anymore
|
||||
// println!("{} {}", $type, format_args!($($arg)*));
|
||||
log::$level!(target: "app", "{} {}", $type, format_args!($($arg)*));
|
||||
};
|
||||
|
||||
@@ -157,3 +168,56 @@ macro_rules! logging_error {
|
||||
logging_error!($type, false, $fmt $(, $arg)*);
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "tauri-dev"))]
|
||||
pub fn get_log_level(log_level: &LevelFilter) -> String {
|
||||
#[cfg(feature = "verge-dev")]
|
||||
match log_level {
|
||||
LevelFilter::Off => Color::Fixed(8).paint("OFF").to_string(),
|
||||
LevelFilter::Error => Color::Red.paint("ERROR").to_string(),
|
||||
LevelFilter::Warn => Color::Yellow.paint("WARN ").to_string(),
|
||||
LevelFilter::Info => Color::Green.paint("INFO ").to_string(),
|
||||
LevelFilter::Debug => Color::Blue.paint("DEBUG").to_string(),
|
||||
LevelFilter::Trace => Color::Purple.paint("TRACE").to_string(),
|
||||
}
|
||||
#[cfg(not(feature = "verge-dev"))]
|
||||
log_level.to_string()
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "tauri-dev"))]
|
||||
pub fn console_colored_format(
|
||||
w: &mut dyn Write,
|
||||
now: &mut DeferredNow,
|
||||
record: &log::Record,
|
||||
) -> std::io::Result<()> {
|
||||
let current_thread = thread::current();
|
||||
let thread_name = current_thread.name().unwrap_or("unnamed");
|
||||
|
||||
let level = get_log_level(&record.level().to_level_filter());
|
||||
let line = record.line().unwrap_or(0);
|
||||
write!(
|
||||
w,
|
||||
"[{}] {} [{}:{}] T[{}] {}",
|
||||
now.format("%H:%M:%S%.3f"),
|
||||
level,
|
||||
record.module_path().unwrap_or("<unnamed>"),
|
||||
line,
|
||||
thread_name,
|
||||
record.args(),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "tauri-dev"))]
|
||||
pub fn file_format(
|
||||
w: &mut dyn Write,
|
||||
now: &mut DeferredNow,
|
||||
record: &Record,
|
||||
) -> std::io::Result<()> {
|
||||
write!(
|
||||
w,
|
||||
"[{}] {} {}",
|
||||
now.format("%Y-%m-%d %H:%M:%S%.3f"),
|
||||
record.level(),
|
||||
record.args(),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user