2025-10-13 11:08:44 +08:00
|
|
|
use compact_str::CompactString;
|
2025-10-13 13:10:55 +08:00
|
|
|
use flexi_logger::DeferredNow;
|
|
|
|
|
#[cfg(not(feature = "tauri-dev"))]
|
|
|
|
|
use flexi_logger::filter::LogLineFilter;
|
2025-10-08 18:05:43 +08:00
|
|
|
use flexi_logger::writers::FileLogWriter;
|
2025-10-13 11:08:44 +08:00
|
|
|
use flexi_logger::writers::LogWriter;
|
|
|
|
|
use log::Level;
|
2025-10-08 18:05:43 +08:00
|
|
|
use log::Record;
|
|
|
|
|
use std::{fmt, sync::Arc};
|
2025-10-13 11:08:44 +08:00
|
|
|
use tokio::sync::{Mutex, MutexGuard};
|
2025-10-08 18:05:43 +08:00
|
|
|
|
|
|
|
|
pub type SharedWriter = Arc<Mutex<FileLogWriter>>;
|
2025-03-25 23:05:09 +08:00
|
|
|
|
2025-10-28 19:16:42 +08:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2025-03-25 23:05:09 +08:00
|
|
|
pub enum Type {
|
2025-03-28 03:39:21 +08:00
|
|
|
Cmd,
|
2025-03-25 23:05:09 +08:00
|
|
|
Core,
|
2025-03-28 03:39:21 +08:00
|
|
|
Config,
|
|
|
|
|
Setup,
|
|
|
|
|
System,
|
2025-03-25 23:05:09 +08:00
|
|
|
Service,
|
2025-03-26 01:18:28 +08:00
|
|
|
Hotkey,
|
|
|
|
|
Window,
|
2025-03-28 03:39:21 +08:00
|
|
|
Tray,
|
|
|
|
|
Timer,
|
|
|
|
|
Frontend,
|
|
|
|
|
Backup,
|
2025-10-14 17:55:48 +08:00
|
|
|
File,
|
2025-03-28 03:39:21 +08:00
|
|
|
Lightweight,
|
2025-04-30 21:30:28 +08:00
|
|
|
Network,
|
2025-05-12 19:04:08 +08:00
|
|
|
ProxyMode,
|
2025-10-18 10:57:57 +08:00
|
|
|
Validate,
|
2025-09-20 23:24:43 +08:00
|
|
|
ClashVergeRev,
|
2025-03-25 23:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Type {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
2025-03-28 03:39:21 +08:00
|
|
|
Type::Cmd => write!(f, "[Cmd]"),
|
2025-03-25 23:05:09 +08:00
|
|
|
Type::Core => write!(f, "[Core]"),
|
2025-03-28 03:39:21 +08:00
|
|
|
Type::Config => write!(f, "[Config]"),
|
|
|
|
|
Type::Setup => write!(f, "[Setup]"),
|
|
|
|
|
Type::System => write!(f, "[System]"),
|
2025-03-25 23:05:09 +08:00
|
|
|
Type::Service => write!(f, "[Service]"),
|
2025-03-26 01:18:28 +08:00
|
|
|
Type::Hotkey => write!(f, "[Hotkey]"),
|
|
|
|
|
Type::Window => write!(f, "[Window]"),
|
2025-03-28 03:39:21 +08:00
|
|
|
Type::Tray => write!(f, "[Tray]"),
|
|
|
|
|
Type::Timer => write!(f, "[Timer]"),
|
|
|
|
|
Type::Frontend => write!(f, "[Frontend]"),
|
|
|
|
|
Type::Backup => write!(f, "[Backup]"),
|
2025-10-14 17:55:48 +08:00
|
|
|
Type::File => write!(f, "[File]"),
|
2025-03-28 03:39:21 +08:00
|
|
|
Type::Lightweight => write!(f, "[Lightweight]"),
|
2025-04-30 21:30:28 +08:00
|
|
|
Type::Network => write!(f, "[Network]"),
|
2025-05-12 19:04:08 +08:00
|
|
|
Type::ProxyMode => write!(f, "[ProxMode]"),
|
2025-10-18 10:57:57 +08:00
|
|
|
Type::Validate => write!(f, "[Validate]"),
|
2025-09-20 23:24:43 +08:00
|
|
|
Type::ClashVergeRev => write!(f, "[ClashVergeRev]"),
|
2025-03-25 23:05:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! error {
|
|
|
|
|
($result: expr) => {
|
|
|
|
|
log::error!(target: "app", "{}", $result);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! log_err {
|
|
|
|
|
($result: expr) => {
|
|
|
|
|
if let Err(err) = $result {
|
|
|
|
|
log::error!(target: "app", "{err}");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
($result: expr, $err_str: expr) => {
|
|
|
|
|
if let Err(_) = $result {
|
|
|
|
|
log::error!(target: "app", "{}", $err_str);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! trace_err {
|
|
|
|
|
($result: expr, $err_str: expr) => {
|
|
|
|
|
if let Err(err) = $result {
|
|
|
|
|
log::trace!(target: "app", "{}, err {}", $err_str, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// wrap the anyhow error
|
|
|
|
|
/// transform the error to String
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! wrap_err {
|
2025-08-26 01:49:51 +08:00
|
|
|
// Case 1: Future<Result<T, E>>
|
|
|
|
|
($stat:expr, async) => {{
|
|
|
|
|
match $stat.await {
|
2025-10-22 16:25:44 +08:00
|
|
|
Ok(a) => Ok::<_, ::anyhow::Error>(a),
|
2025-08-26 01:49:51 +08:00
|
|
|
Err(err) => {
|
|
|
|
|
log::error!(target: "app", "{}", err);
|
2025-10-22 16:25:44 +08:00
|
|
|
Err(::anyhow::Error::msg(err.to_string()))
|
2025-03-25 23:05:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
}};
|
2025-03-25 23:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! logging {
|
2025-04-05 11:34:51 +08:00
|
|
|
// 不带 print 参数的版本(默认不打印)
|
2025-03-25 23:05:09 +08:00
|
|
|
($level:ident, $type:expr, $($arg:tt)*) => {
|
2025-10-21 17:51:12 +08:00
|
|
|
log::$level!(target: "app", "{} {}", $type, format_args!($($arg)*))
|
2025-03-25 23:05:09 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! logging_error {
|
Refactor logging macros to remove print control parameter
- Updated logging macros to eliminate the boolean parameter for print control, simplifying the logging calls throughout the codebase.
- Adjusted all logging calls in various modules (lib.rs, lightweight.rs, help.rs, init.rs, logging.rs, resolve/mod.rs, resolve/scheme.rs, resolve/ui.rs, resolve/window.rs, server.rs, singleton.rs, window_manager.rs) to reflect the new macro structure.
- Ensured consistent logging behavior across the application by standardizing the logging format.
2025-10-10 13:05:01 +08:00
|
|
|
// Handle Result<T, E>
|
2025-03-25 23:05:09 +08:00
|
|
|
($type:expr, $expr:expr) => {
|
|
|
|
|
if let Err(err) = $expr {
|
2025-04-05 11:34:51 +08:00
|
|
|
log::error!(target: "app", "[{}] {}", $type, err);
|
2025-03-25 23:05:09 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
Refactor logging macros to remove print control parameter
- Updated logging macros to eliminate the boolean parameter for print control, simplifying the logging calls throughout the codebase.
- Adjusted all logging calls in various modules (lib.rs, lightweight.rs, help.rs, init.rs, logging.rs, resolve/mod.rs, resolve/scheme.rs, resolve/ui.rs, resolve/window.rs, server.rs, singleton.rs, window_manager.rs) to reflect the new macro structure.
- Ensured consistent logging behavior across the application by standardizing the logging format.
2025-10-10 13:05:01 +08:00
|
|
|
// Handle formatted message: always print to stdout and log as error
|
2025-04-05 11:34:51 +08:00
|
|
|
($type:expr, $fmt:literal $(, $arg:expr)*) => {
|
Refactor logging macros to remove print control parameter
- Updated logging macros to eliminate the boolean parameter for print control, simplifying the logging calls throughout the codebase.
- Adjusted all logging calls in various modules (lib.rs, lightweight.rs, help.rs, init.rs, logging.rs, resolve/mod.rs, resolve/scheme.rs, resolve/ui.rs, resolve/window.rs, server.rs, singleton.rs, window_manager.rs) to reflect the new macro structure.
- Ensured consistent logging behavior across the application by standardizing the logging format.
2025-10-10 13:05:01 +08:00
|
|
|
log::error!(target: "app", "[{}] {}", $type, format_args!($fmt $(, $arg)*));
|
2025-03-25 23:05:09 +08:00
|
|
|
};
|
|
|
|
|
}
|
2025-09-20 00:04:46 +08:00
|
|
|
|
2025-10-13 11:08:44 +08:00
|
|
|
pub fn write_sidecar_log(
|
|
|
|
|
writer: MutexGuard<'_, FileLogWriter>,
|
|
|
|
|
now: &mut DeferredNow,
|
|
|
|
|
level: Level,
|
|
|
|
|
message: &CompactString,
|
|
|
|
|
) {
|
|
|
|
|
let args = format_args!("{}", message);
|
|
|
|
|
|
|
|
|
|
let record = Record::builder()
|
|
|
|
|
.args(args)
|
|
|
|
|
.level(level)
|
|
|
|
|
.target("sidecar")
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let _ = writer.write(now, &record);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-21 01:31:08 +08:00
|
|
|
#[cfg(not(feature = "tauri-dev"))]
|
2025-10-08 18:05:43 +08:00
|
|
|
pub struct NoModuleFilter<'a>(pub &'a [&'a str]);
|
|
|
|
|
|
2025-09-21 01:31:08 +08:00
|
|
|
#[cfg(not(feature = "tauri-dev"))]
|
2025-10-08 18:05:43 +08:00
|
|
|
impl<'a> NoModuleFilter<'a> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn filter(&self, record: &Record) -> bool {
|
|
|
|
|
if let Some(module) = record.module_path() {
|
|
|
|
|
for blocked in self.0 {
|
|
|
|
|
if module.len() >= blocked.len()
|
|
|
|
|
&& module.as_bytes()[..blocked.len()] == blocked.as_bytes()[..]
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-21 01:31:08 +08:00
|
|
|
#[cfg(not(feature = "tauri-dev"))]
|
2025-10-08 18:05:43 +08:00
|
|
|
impl<'a> LogLineFilter for NoModuleFilter<'a> {
|
2025-09-21 01:31:08 +08:00
|
|
|
fn write(
|
|
|
|
|
&self,
|
|
|
|
|
now: &mut DeferredNow,
|
|
|
|
|
record: &Record,
|
2025-10-08 18:05:43 +08:00
|
|
|
writer: &dyn flexi_logger::filter::LogLineWriter,
|
2025-09-21 01:31:08 +08:00
|
|
|
) -> std::io::Result<()> {
|
2025-10-08 18:05:43 +08:00
|
|
|
if !self.filter(record) {
|
|
|
|
|
return Ok(());
|
2025-09-21 01:31:08 +08:00
|
|
|
}
|
2025-10-08 18:05:43 +08:00
|
|
|
writer.write(now, record)
|
2025-09-21 01:31:08 +08:00
|
|
|
}
|
|
|
|
|
}
|