- 移除 anyhow,使用 thiserror 定义强类型 Error 枚举 - 支持 #[from] 自动转换 mlua::Error,关键路径保留源码与上下文 - LuaRuntimeBuilder 采用惰性错误收集,简化链式初始化调用 - MirrorRegistry 实现规范化 FromLuaConversionError 错误转换
60 lines
2.3 KiB
Rust
60 lines
2.3 KiB
Rust
use anyhow::{Context, Result};
|
|
use mirror_core::{Layout, LogConfig, LogLevel};
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
// Rust 原生处理 log 命令逻辑
|
|
pub(crate) fn log_handle(layout: &Layout, matches: &clap::ArgMatches) -> Result<()> {
|
|
let log_ini_path = layout.base_dir.join("mirror.ini");
|
|
|
|
let new_level = matches.get_one::<LogLevel>("level").copied();
|
|
let new_dir = matches.get_one::<String>("dir").map(|s| s.trim());
|
|
// 1. 反序列化:读取并解析现有配置;若文件不存在则使用默认配置
|
|
let mut config: LogConfig = if log_ini_path.is_file() {
|
|
let content = fs::read_to_string(&log_ini_path)
|
|
.with_context(|| format!("读取配置文件失败: {}", log_ini_path.display()))?;
|
|
content.parse().unwrap_or_default()
|
|
} else {
|
|
LogConfig::default()
|
|
};
|
|
// 2. 修改配置并写盘(仅更新用户指定的字段,未指定的字段保留原值)
|
|
if new_level.is_some() || new_dir.is_some() {
|
|
if let Some(lvl) = new_level {
|
|
config.level = lvl;
|
|
}
|
|
if let Some(dir) = new_dir {
|
|
config.log_dir = Some(PathBuf::from(dir));
|
|
}
|
|
|
|
// 序列化写回
|
|
fs::write(&log_ini_path, config.to_string())
|
|
.with_context(|| format!("写入日志配置文件失败: {}", log_ini_path.display()))?;
|
|
|
|
let actual_dir = config.resolve_log_dir(&layout.base_dir);
|
|
println!("✔ 日志配置已更新:");
|
|
println!(" 日志级别: {}", config.level.as_str());
|
|
println!(" 日志目录: {}", actual_dir.display());
|
|
println!(" 配置文件: {}", log_ini_path.display());
|
|
return Ok(());
|
|
}
|
|
|
|
// 3. 无修改参数时,展示当前配置状态
|
|
println!("=== Mirror 日志配置 ===");
|
|
println!("配置文件: {}", log_ini_path.display());
|
|
println!("---------------------");
|
|
|
|
if log_ini_path.is_file() {
|
|
print!("{config}");
|
|
} else {
|
|
println!("状态: 配置文件尚未创建");
|
|
println!("当前生效级别: {} (默认)", config.level.as_str());
|
|
println!(
|
|
"默认日志目录: {}",
|
|
config.resolve_log_dir(&layout.base_dir).display()
|
|
);
|
|
println!("\n提示: 可使用 'mr log -l debug' 或 'mr log -d my_logs' 进行配置修改");
|
|
}
|
|
|
|
Ok(())
|
|
}
|