mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-04 23:43:44 +08:00
refact(profile+core): replace println with logging! macros for structured logging
This commit is contained in:
parent
8fdcffc731
commit
e2046f3e48
@ -2,8 +2,8 @@ use super::CmdResult;
|
|||||||
use crate::{
|
use crate::{
|
||||||
config::*,
|
config::*,
|
||||||
core::*,
|
core::*,
|
||||||
feat, log_err, ret_err,
|
feat, log_err, logging, ret_err,
|
||||||
utils::{dirs, help},
|
utils::{dirs, help, logging::Type},
|
||||||
wrap_err,
|
wrap_err,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -77,20 +77,19 @@ pub async fn delete_profile(index: String) -> CmdResult {
|
|||||||
/// 修改profiles的配置
|
/// 修改profiles的配置
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
|
pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
|
||||||
println!("[cmd配置patch] 开始修改配置文件");
|
logging!(info, Type::CMD, true, "开始修改配置文件");
|
||||||
|
|
||||||
// 保存当前配置,以便在验证失败时恢复
|
// 保存当前配置,以便在验证失败时恢复
|
||||||
let current_profile = Config::profiles().latest().current.clone();
|
let current_profile = Config::profiles().latest().current.clone();
|
||||||
println!("[cmd配置patch] 当前配置: {:?}", current_profile);
|
logging!(info, Type::CMD, true, "当前配置: {:?}", current_profile);
|
||||||
|
|
||||||
// 更新profiles配置
|
// 更新profiles配置
|
||||||
println!("[cmd配置patch] 正在更新配置草稿");
|
logging!(info, Type::CMD, true, "正在更新配置草稿");
|
||||||
wrap_err!({ Config::profiles().draft().patch_config(profiles) })?;
|
wrap_err!({ Config::profiles().draft().patch_config(profiles) })?;
|
||||||
|
|
||||||
// 更新配置并进行验证
|
// 更新配置并进行验证
|
||||||
match CoreManager::global().update_config().await {
|
match CoreManager::global().update_config().await {
|
||||||
Ok((true, _)) => {
|
Ok((true, _)) => {
|
||||||
println!("[cmd配置patch] 配置更新成功");
|
logging!(info, Type::CMD, true, "配置更新成功");
|
||||||
handle::Handle::refresh_clash();
|
handle::Handle::refresh_clash();
|
||||||
let _ = tray::Tray::global().update_tooltip();
|
let _ = tray::Tray::global().update_tooltip();
|
||||||
Config::profiles().apply();
|
Config::profiles().apply();
|
||||||
@ -98,12 +97,17 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
|
|||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
Ok((false, error_msg)) => {
|
Ok((false, error_msg)) => {
|
||||||
println!("[cmd配置patch] 配置验证失败: {}", error_msg);
|
logging!(warn, Type::CMD, true, "配置验证失败: {}", error_msg);
|
||||||
Config::profiles().discard();
|
Config::profiles().discard();
|
||||||
|
|
||||||
// 如果验证失败,恢复到之前的配置
|
// 如果验证失败,恢复到之前的配置
|
||||||
if let Some(prev_profile) = current_profile {
|
if let Some(prev_profile) = current_profile {
|
||||||
println!("[cmd配置patch] 尝试恢复到之前的配置: {}", prev_profile);
|
logging!(
|
||||||
|
info,
|
||||||
|
Type::CMD,
|
||||||
|
true,
|
||||||
|
"尝试恢复到之前的配置: {}",
|
||||||
|
prev_profile
|
||||||
|
);
|
||||||
let restore_profiles = IProfiles {
|
let restore_profiles = IProfiles {
|
||||||
current: Some(prev_profile),
|
current: Some(prev_profile),
|
||||||
items: None,
|
items: None,
|
||||||
@ -112,7 +116,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
|
|||||||
wrap_err!({ Config::profiles().draft().patch_config(restore_profiles) })?;
|
wrap_err!({ Config::profiles().draft().patch_config(restore_profiles) })?;
|
||||||
Config::profiles().apply();
|
Config::profiles().apply();
|
||||||
wrap_err!(Config::profiles().data().save_file())?;
|
wrap_err!(Config::profiles().data().save_file())?;
|
||||||
println!("[cmd配置patch] 成功恢复到之前的配置");
|
logging!(info, Type::CMD, true, "成功恢复到之前的配置");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送验证错误通知
|
// 发送验证错误通知
|
||||||
@ -120,7 +124,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
|
|||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("[cmd配置patch] 更新过程发生错误: {}", e);
|
logging!(warn, Type::CMD, true, "更新过程发生错误: {}", e);
|
||||||
Config::profiles().discard();
|
Config::profiles().discard();
|
||||||
handle::Handle::notice_message("config_validate::boot_error", e.to_string());
|
handle::Handle::notice_message("config_validate::boot_error", e.to_string());
|
||||||
Ok(false)
|
Ok(false)
|
||||||
|
@ -6,7 +6,7 @@ use crate::{
|
|||||||
handle,
|
handle,
|
||||||
service::{self},
|
service::{self},
|
||||||
},
|
},
|
||||||
log_err, logging, logging_error,
|
logging, logging_error,
|
||||||
module::mihomo::MihomoManager,
|
module::mihomo::MihomoManager,
|
||||||
utils::{
|
utils::{
|
||||||
dirs,
|
dirs,
|
||||||
@ -63,7 +63,14 @@ impl CoreManager {
|
|||||||
let content = match std::fs::read_to_string(path) {
|
let content = match std::fs::read_to_string(path) {
|
||||||
Ok(content) => content,
|
Ok(content) => content,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::warn!(target: "app", "无法读取文件以检测类型: {}, 错误: {}", path, err);
|
logging!(
|
||||||
|
warn,
|
||||||
|
Type::Config,
|
||||||
|
true,
|
||||||
|
"无法读取文件以检测类型: {}, 错误: {}",
|
||||||
|
path,
|
||||||
|
err
|
||||||
|
);
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow::anyhow!(
|
||||||
"Failed to read file to detect type: {}",
|
"Failed to read file to detect type: {}",
|
||||||
err
|
err
|
||||||
@ -114,7 +121,13 @@ impl CoreManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 默认情况:无法确定时,假设为非脚本文件(更安全)
|
// 默认情况:无法确定时,假设为非脚本文件(更安全)
|
||||||
log::debug!(target: "app", "无法确定文件类型,默认当作YAML处理: {}", path);
|
logging!(
|
||||||
|
debug,
|
||||||
|
Type::Config,
|
||||||
|
true,
|
||||||
|
"无法确定文件类型,默认当作YAML处理: {}",
|
||||||
|
path
|
||||||
|
);
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
/// 使用默认配置
|
/// 使用默认配置
|
||||||
@ -147,7 +160,7 @@ impl CoreManager {
|
|||||||
) -> Result<(bool, String)> {
|
) -> Result<(bool, String)> {
|
||||||
// 检查程序是否正在退出,如果是则跳过验证
|
// 检查程序是否正在退出,如果是则跳过验证
|
||||||
if handle::Handle::global().is_exiting() {
|
if handle::Handle::global().is_exiting() {
|
||||||
println!("[core配置验证] 应用正在退出,跳过验证");
|
logging!(info, Type::Core, true, "应用正在退出,跳过验证");
|
||||||
return Ok((true, String::new()));
|
return Ok((true, String::new()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,8 +173,11 @@ impl CoreManager {
|
|||||||
|
|
||||||
// 如果是合并文件且不是强制验证,执行语法检查但不进行完整验证
|
// 如果是合并文件且不是强制验证,执行语法检查但不进行完整验证
|
||||||
if is_merge_file.unwrap_or(false) {
|
if is_merge_file.unwrap_or(false) {
|
||||||
println!(
|
logging!(
|
||||||
"[core配置验证] 检测到Merge文件,仅进行语法检查: {}",
|
info,
|
||||||
|
Type::Config,
|
||||||
|
true,
|
||||||
|
"检测到Merge文件,仅进行语法检查: {}",
|
||||||
config_path
|
config_path
|
||||||
);
|
);
|
||||||
return self.validate_file_syntax(config_path).await;
|
return self.validate_file_syntax(config_path).await;
|
||||||
@ -175,19 +191,38 @@ impl CoreManager {
|
|||||||
Ok(result) => result,
|
Ok(result) => result,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
// 如果无法确定文件类型,尝试使用Clash内核验证
|
// 如果无法确定文件类型,尝试使用Clash内核验证
|
||||||
log::warn!(target: "app", "无法确定文件类型: {}, 错误: {}", config_path, err);
|
logging!(
|
||||||
|
warn,
|
||||||
|
Type::Config,
|
||||||
|
true,
|
||||||
|
"无法确定文件类型: {}, 错误: {}",
|
||||||
|
config_path,
|
||||||
|
err
|
||||||
|
);
|
||||||
return self.validate_config_internal(config_path).await;
|
return self.validate_config_internal(config_path).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if is_script {
|
if is_script {
|
||||||
log::info!(target: "app", "检测到脚本文件,使用JavaScript验证: {}", config_path);
|
logging!(
|
||||||
|
info,
|
||||||
|
Type::Config,
|
||||||
|
true,
|
||||||
|
"检测到脚本文件,使用JavaScript验证: {}",
|
||||||
|
config_path
|
||||||
|
);
|
||||||
return self.validate_script_file(config_path).await;
|
return self.validate_script_file(config_path).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 对YAML配置文件使用Clash内核验证
|
// 对YAML配置文件使用Clash内核验证
|
||||||
log::info!(target: "app", "使用Clash内核验证配置文件: {}", config_path);
|
logging!(
|
||||||
|
info,
|
||||||
|
Type::Config,
|
||||||
|
true,
|
||||||
|
"使用Clash内核验证配置文件: {}",
|
||||||
|
config_path
|
||||||
|
);
|
||||||
self.validate_config_internal(config_path).await
|
self.validate_config_internal(config_path).await
|
||||||
}
|
}
|
||||||
/// 内部验证配置文件的实现
|
/// 内部验证配置文件的实现
|
||||||
@ -234,7 +269,7 @@ impl CoreManager {
|
|||||||
logging!(info, Type::Config, true, "-------- 验证结果 --------");
|
logging!(info, Type::Config, true, "-------- 验证结果 --------");
|
||||||
|
|
||||||
if !stderr.is_empty() {
|
if !stderr.is_empty() {
|
||||||
logging!(info, Type::Core, true, "stderr输出:\n{}", stderr);
|
logging!(info, Type::Config, true, "stderr输出:\n{}", stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if has_error {
|
if has_error {
|
||||||
@ -259,29 +294,28 @@ impl CoreManager {
|
|||||||
}
|
}
|
||||||
/// 只进行文件语法检查,不进行完整验证
|
/// 只进行文件语法检查,不进行完整验证
|
||||||
async fn validate_file_syntax(&self, config_path: &str) -> Result<(bool, String)> {
|
async fn validate_file_syntax(&self, config_path: &str) -> Result<(bool, String)> {
|
||||||
println!("[core配置语法检查] 开始检查文件: {}", config_path);
|
logging!(info, Type::Config, true, "开始检查文件: {}", config_path);
|
||||||
|
|
||||||
// 读取文件内容
|
// 读取文件内容
|
||||||
let content = match std::fs::read_to_string(config_path) {
|
let content = match std::fs::read_to_string(config_path) {
|
||||||
Ok(content) => content,
|
Ok(content) => content,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let error_msg = format!("Failed to read file: {}", err);
|
let error_msg = format!("Failed to read file: {}", err);
|
||||||
println!("[core配置语法检查] 无法读取文件: {}", error_msg);
|
logging!(error, Type::Config, true, "无法读取文件: {}", error_msg);
|
||||||
return Ok((false, error_msg));
|
return Ok((false, error_msg));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 对YAML文件尝试解析,只检查语法正确性
|
// 对YAML文件尝试解析,只检查语法正确性
|
||||||
println!("[core配置语法检查] 进行YAML语法检查");
|
logging!(info, Type::Config, true, "进行YAML语法检查");
|
||||||
match serde_yaml::from_str::<serde_yaml::Value>(&content) {
|
match serde_yaml::from_str::<serde_yaml::Value>(&content) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
println!("[core配置语法检查] YAML语法检查通过");
|
logging!(info, Type::Config, true, "YAML语法检查通过");
|
||||||
Ok((true, String::new()))
|
Ok((true, String::new()))
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
// 使用标准化的前缀,以便错误处理函数能正确识别
|
// 使用标准化的前缀,以便错误处理函数能正确识别
|
||||||
let error_msg = format!("YAML syntax error: {}", err);
|
let error_msg = format!("YAML syntax error: {}", err);
|
||||||
println!("[core配置语法检查] YAML语法错误: {}", error_msg);
|
logging!(error, Type::Config, true, "YAML语法错误: {}", error_msg);
|
||||||
Ok((false, error_msg))
|
Ok((false, error_msg))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -293,13 +327,13 @@ impl CoreManager {
|
|||||||
Ok(content) => content,
|
Ok(content) => content,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let error_msg = format!("Failed to read script file: {}", err);
|
let error_msg = format!("Failed to read script file: {}", err);
|
||||||
log::warn!(target: "app", "脚本语法错误: {}", err);
|
logging!(warn, Type::Config, true, "脚本语法错误: {}", err);
|
||||||
//handle::Handle::notice_message("config_validate::script_syntax_error", &error_msg);
|
//handle::Handle::notice_message("config_validate::script_syntax_error", &error_msg);
|
||||||
return Ok((false, error_msg));
|
return Ok((false, error_msg));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
log::debug!(target: "app", "验证脚本文件: {}", path);
|
logging!(debug, Type::Config, true, "验证脚本文件: {}", path);
|
||||||
|
|
||||||
// 使用boa引擎进行基本语法检查
|
// 使用boa引擎进行基本语法检查
|
||||||
use boa_engine::{Context, Source};
|
use boa_engine::{Context, Source};
|
||||||
@ -309,7 +343,7 @@ impl CoreManager {
|
|||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
log::debug!(target: "app", "脚本语法验证通过: {}", path);
|
logging!(debug, Type::Config, true, "脚本语法验证通过: {}", path);
|
||||||
|
|
||||||
// 检查脚本是否包含main函数
|
// 检查脚本是否包含main函数
|
||||||
if !content.contains("function main")
|
if !content.contains("function main")
|
||||||
@ -317,7 +351,7 @@ impl CoreManager {
|
|||||||
&& !content.contains("let main")
|
&& !content.contains("let main")
|
||||||
{
|
{
|
||||||
let error_msg = "Script must contain a main function";
|
let error_msg = "Script must contain a main function";
|
||||||
log::warn!(target: "app", "脚本缺少main函数: {}", path);
|
logging!(warn, Type::Config, true, "脚本缺少main函数: {}", path);
|
||||||
//handle::Handle::notice_message("config_validate::script_missing_main", error_msg);
|
//handle::Handle::notice_message("config_validate::script_missing_main", error_msg);
|
||||||
return Ok((false, error_msg.to_string()));
|
return Ok((false, error_msg.to_string()));
|
||||||
}
|
}
|
||||||
@ -326,7 +360,7 @@ impl CoreManager {
|
|||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let error_msg = format!("Script syntax error: {}", err);
|
let error_msg = format!("Script syntax error: {}", err);
|
||||||
log::warn!(target: "app", "脚本语法错误: {}", err);
|
logging!(warn, Type::Config, true, "脚本语法错误: {}", err);
|
||||||
//handle::Handle::notice_message("config_validate::script_syntax_error", &error_msg);
|
//handle::Handle::notice_message("config_validate::script_syntax_error", &error_msg);
|
||||||
Ok((false, error_msg))
|
Ok((false, error_msg))
|
||||||
}
|
}
|
||||||
@ -336,39 +370,45 @@ impl CoreManager {
|
|||||||
pub async fn update_config(&self) -> Result<(bool, String)> {
|
pub async fn update_config(&self) -> Result<(bool, String)> {
|
||||||
// 检查程序是否正在退出,如果是则跳过完整验证流程
|
// 检查程序是否正在退出,如果是则跳过完整验证流程
|
||||||
if handle::Handle::global().is_exiting() {
|
if handle::Handle::global().is_exiting() {
|
||||||
println!("[core配置更新] 应用正在退出,跳过验证");
|
logging!(info, Type::Config, true, "应用正在退出,跳过验证");
|
||||||
return Ok((true, String::new()));
|
return Ok((true, String::new()));
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("[core配置更新] 开始更新配置");
|
logging!(info, Type::Config, true, "开始更新配置");
|
||||||
|
|
||||||
// 1. 先生成新的配置内容
|
// 1. 先生成新的配置内容
|
||||||
println!("[core配置更新] 生成新的配置内容");
|
logging!(info, Type::Config, true, "生成新的配置内容");
|
||||||
Config::generate().await?;
|
Config::generate().await?;
|
||||||
|
|
||||||
// 2. 生成临时文件并进行验证
|
// 2. 生成临时文件并进行验证
|
||||||
println!("[core配置更新] 生成临时配置文件用于验证");
|
logging!(info, Type::Config, true, "生成临时配置文件用于验证");
|
||||||
let temp_config = Config::generate_file(ConfigType::Check)?;
|
let temp_config = Config::generate_file(ConfigType::Check)?;
|
||||||
let temp_config = dirs::path_to_str(&temp_config)?;
|
let temp_config = dirs::path_to_str(&temp_config)?;
|
||||||
println!("[core配置更新] 临时配置文件路径: {}", temp_config);
|
logging!(
|
||||||
|
info,
|
||||||
|
Type::Config,
|
||||||
|
true,
|
||||||
|
"临时配置文件路径: {}",
|
||||||
|
temp_config
|
||||||
|
);
|
||||||
|
|
||||||
// 3. 验证配置
|
// 3. 验证配置
|
||||||
match self.validate_config().await {
|
match self.validate_config().await {
|
||||||
Ok((true, _)) => {
|
Ok((true, _)) => {
|
||||||
println!("[core配置更新] 配置验证通过");
|
logging!(info, Type::Config, true, "配置验证通过");
|
||||||
// 4. 验证通过后,生成正式的运行时配置
|
// 4. 验证通过后,生成正式的运行时配置
|
||||||
println!("[core配置更新] 生成运行时配置");
|
logging!(info, Type::Config, true, "生成运行时配置");
|
||||||
let run_path = Config::generate_file(ConfigType::Run)?;
|
let run_path = Config::generate_file(ConfigType::Run)?;
|
||||||
logging_error!(Type::Core, true, self.put_configs_force(run_path).await);
|
logging_error!(Type::Config, true, self.put_configs_force(run_path).await);
|
||||||
Ok((true, "something".into()))
|
Ok((true, "something".into()))
|
||||||
}
|
}
|
||||||
Ok((false, error_msg)) => {
|
Ok((false, error_msg)) => {
|
||||||
println!("[core配置更新] 配置验证失败: {}", error_msg);
|
logging!(warn, Type::Config, true, "配置验证失败: {}", error_msg);
|
||||||
Config::runtime().discard();
|
Config::runtime().discard();
|
||||||
Ok((false, error_msg))
|
Ok((false, error_msg))
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("[core配置更新] 验证过程发生错误: {}", e);
|
logging!(warn, Type::Config, true, "验证过程发生错误: {}", e);
|
||||||
Config::runtime().discard();
|
Config::runtime().discard();
|
||||||
Err(e)
|
Err(e)
|
||||||
}
|
}
|
||||||
@ -483,7 +523,7 @@ impl CoreManager {
|
|||||||
self.start_core().await?;
|
self.start_core().await?;
|
||||||
logging!(trace, Type::Core, "Initied core");
|
logging!(trace, Type::Core, "Initied core");
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
log_err!(Tray::global().subscribe_traffic().await);
|
logging_error!(Type::Core, true, Tray::global().subscribe_traffic().await);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user