Refactor logging statements to use the new formatting syntax for improved readability and consistency across the codebase. This includes updating error, warning, and info logs in various modules such as system commands, configuration, core functionalities, and utilities. Additionally, minor adjustments were made to string formatting in backup and proxy features to enhance clarity.

This commit is contained in:
Tunglies
2025-06-27 23:30:35 +08:00
parent cf437e6d94
commit a574ced428
31 changed files with 153 additions and 162 deletions

View File

@@ -147,7 +147,7 @@ pub async fn download_icon_cache(url: String, name: String) -> CmdResult<String>
Ok(icon_path.to_string_lossy().to_string())
} else {
let _ = std::fs::remove_file(&temp_path);
Err(format!("下载的内容不是有效图片: {}", url))
Err(format!("下载的内容不是有效图片: {url}"))
}
}
@@ -217,7 +217,7 @@ pub fn notify_ui_ready() -> CmdResult<()> {
/// UI加载阶段
#[tauri::command]
pub fn update_ui_stage(stage: String) -> CmdResult<()> {
log::info!(target: "app", "UI加载阶段更新: {}", stage);
log::info!(target: "app", "UI加载阶段更新: {stage}");
use crate::utils::resolve::UiReadyStage;
@@ -228,8 +228,8 @@ pub fn update_ui_stage(stage: String) -> CmdResult<()> {
"ResourcesLoaded" => UiReadyStage::ResourcesLoaded,
"Ready" => UiReadyStage::Ready,
_ => {
log::warn!(target: "app", "未知的UI加载阶段: {}", stage);
return Err(format!("未知的UI加载阶段: {}", stage));
log::warn!(target: "app", "未知的UI加载阶段: {stage}");
return Err(format!("未知的UI加载阶段: {stage}"));
}
};

View File

@@ -49,8 +49,8 @@ pub async fn change_clash_core(clash_core: String) -> CmdResult<Option<String>>
Ok(None)
}
Err(err) => {
let error_msg = format!("Core changed but failed to restart: {}", err);
log::error!(target: "app", "{}", error_msg);
let error_msg = format!("Core changed but failed to restart: {err}");
log::error!(target: "app", "{error_msg}");
handle::Handle::notice_message("config_core::change_error", &error_msg);
Ok(Some(error_msg))
}
@@ -116,7 +116,7 @@ pub async fn save_dns_config(dns_config: Mapping) -> CmdResult {
// 保存DNS配置到文件
let yaml_str = serde_yaml::to_string(&dns_config).map_err(|e| e.to_string())?;
fs::write(&dns_path, yaml_str).map_err(|e| e.to_string())?;
log::info!(target: "app", "DNS config saved to {:?}", dns_path);
log::info!(target: "app", "DNS config saved to {dns_path:?}");
Ok(())
}
@@ -137,7 +137,7 @@ pub fn apply_dns_config(apply: bool) -> CmdResult {
let dns_path = match dirs::app_home_dir() {
Ok(path) => path.join("dns_config.yaml"),
Err(e) => {
log::error!(target: "app", "Failed to get home dir: {}", e);
log::error!(target: "app", "Failed to get home dir: {e}");
return;
}
};
@@ -150,7 +150,7 @@ pub fn apply_dns_config(apply: bool) -> CmdResult {
let dns_yaml = match std::fs::read_to_string(&dns_path) {
Ok(content) => content,
Err(e) => {
log::error!(target: "app", "Failed to read DNS config: {}", e);
log::error!(target: "app", "Failed to read DNS config: {e}");
return;
}
};
@@ -163,7 +163,7 @@ pub fn apply_dns_config(apply: bool) -> CmdResult {
patch
}
Err(e) => {
log::error!(target: "app", "Failed to parse DNS config: {}", e);
log::error!(target: "app", "Failed to parse DNS config: {e}");
return;
}
};
@@ -178,13 +178,13 @@ pub fn apply_dns_config(apply: bool) -> CmdResult {
// 首先重新生成配置
if let Err(err) = Config::generate().await {
log::error!(target: "app", "Failed to regenerate config with DNS: {}", err);
log::error!(target: "app", "Failed to regenerate config with DNS: {err}");
return;
}
// 然后应用新配置
if let Err(err) = CoreManager::global().update_config().await {
log::error!(target: "app", "Failed to apply config with DNS: {}", err);
log::error!(target: "app", "Failed to apply config with DNS: {err}");
} else {
log::info!(target: "app", "DNS config successfully applied");
handle::Handle::refresh_clash();
@@ -196,7 +196,7 @@ pub fn apply_dns_config(apply: bool) -> CmdResult {
// 重新生成配置
if let Err(err) = Config::generate().await {
log::error!(target: "app", "Failed to regenerate config: {}", err);
log::error!(target: "app", "Failed to regenerate config: {err}");
return;
}
@@ -207,7 +207,7 @@ pub fn apply_dns_config(apply: bool) -> CmdResult {
handle::Handle::refresh_clash();
}
Err(err) => {
log::error!(target: "app", "Failed to apply regenerated config: {}", err);
log::error!(target: "app", "Failed to apply regenerated config: {err}");
}
}
}

View File

@@ -37,7 +37,7 @@ fn country_code_to_emoji(country_code: &str) -> String {
let c2 = 0x1F1E6 + (bytes[1] as u32) - ('A' as u32);
char::from_u32(c1)
.and_then(|c1| char::from_u32(c2).map(|c2| format!("{}{}", c1, c2)))
.and_then(|c1| char::from_u32(c2).map(|c2| format!("{c1}{c2}")))
.unwrap_or_default()
}
@@ -163,7 +163,7 @@ async fn check_chatgpt_combined(client: &Client) -> Vec<UnlockItem> {
map.get("loc").map(|loc| {
let emoji = country_code_to_emoji(loc);
format!("{}{}", emoji, loc)
format!("{emoji}{loc}")
})
} else {
None
@@ -255,7 +255,7 @@ async fn check_gemini(client: &Client) -> UnlockItem {
caps.get(1).map(|m| {
let country_code = m.as_str();
let emoji = country_code_to_emoji(country_code);
format!("{}{}", emoji, country_code)
format!("{emoji}{country_code}")
})
});
@@ -308,7 +308,7 @@ async fn check_youtube_premium(client: &Client) -> UnlockItem {
caps.get(1).map(|m| {
let country_code = m.as_str().trim();
let emoji = country_code_to_emoji(country_code);
format!("{}{}", emoji, country_code)
format!("{emoji}{country_code}")
})
});
@@ -384,10 +384,8 @@ async fn check_bahamut_anime(client: &Client) -> UnlockItem {
}
// 第二步使用设备ID检查访问权限 (使用相同的Cookie)
let url = format!(
"https://ani.gamer.com.tw/ajax/token.php?adID=89422&sn=37783&device={}",
device_id
);
let url =
format!("https://ani.gamer.com.tw/ajax/token.php?adID=89422&sn=37783&device={device_id}");
let token_result = match client_with_cookies.get(&url).send().await {
Ok(response) => {
@@ -431,7 +429,7 @@ async fn check_bahamut_anime(client: &Client) -> UnlockItem {
.map(|m| {
let country_code = m.as_str();
let emoji = country_code_to_emoji(country_code);
format!("{}{}", emoji, country_code)
format!("{emoji}{country_code}")
})
}
Err(_) => None,
@@ -470,7 +468,7 @@ async fn check_netflix(client: &Client) -> UnlockItem {
// 检查连接失败情况
if let Err(e) = &result1 {
eprintln!("Netflix请求错误: {}", e);
eprintln!("Netflix请求错误: {e}");
return UnlockItem {
name: "Netflix".to_string(),
status: "Failed".to_string(),
@@ -487,7 +485,7 @@ async fn check_netflix(client: &Client) -> UnlockItem {
.await;
if let Err(e) = &result2 {
eprintln!("Netflix请求错误: {}", e);
eprintln!("Netflix请求错误: {e}");
return UnlockItem {
name: "Netflix".to_string(),
status: "Failed".to_string(),
@@ -541,7 +539,7 @@ async fn check_netflix(client: &Client) -> UnlockItem {
return UnlockItem {
name: "Netflix".to_string(),
status: "Yes".to_string(),
region: Some(format!("{}{}", emoji, region_code)),
region: Some(format!("{emoji}{region_code}")),
check_time: Some(get_local_date_string()),
};
}
@@ -557,7 +555,7 @@ async fn check_netflix(client: &Client) -> UnlockItem {
}
}
Err(e) => {
eprintln!("获取Netflix区域信息失败: {}", e);
eprintln!("获取Netflix区域信息失败: {e}");
UnlockItem {
name: "Netflix".to_string(),
status: "Yes (但无法获取区域)".to_string(),
@@ -570,7 +568,7 @@ async fn check_netflix(client: &Client) -> UnlockItem {
// 其他未知错误状态
UnlockItem {
name: "Netflix".to_string(),
status: format!("Failed (状态码: {}_{}", status1, status2),
status: format!("Failed (状态码: {status1}_{status2}"),
region: None,
check_time: Some(get_local_date_string()),
}
@@ -614,7 +612,7 @@ async fn check_netflix_cdn(client: &Client) -> UnlockItem {
return UnlockItem {
name: "Netflix".to_string(),
status: "Yes".to_string(),
region: Some(format!("{}{}", emoji, country)),
region: Some(format!("{emoji}{country}")),
check_time: Some(get_local_date_string()),
};
}
@@ -631,7 +629,7 @@ async fn check_netflix_cdn(client: &Client) -> UnlockItem {
}
}
Err(e) => {
eprintln!("解析Fast.com API响应失败: {}", e);
eprintln!("解析Fast.com API响应失败: {e}");
UnlockItem {
name: "Netflix".to_string(),
status: "Failed (解析错误)".to_string(),
@@ -642,7 +640,7 @@ async fn check_netflix_cdn(client: &Client) -> UnlockItem {
}
}
Err(e) => {
eprintln!("Fast.com API请求失败: {}", e);
eprintln!("Fast.com API请求失败: {e}");
UnlockItem {
name: "Netflix".to_string(),
status: "Failed (CDN API)".to_string(),
@@ -884,7 +882,7 @@ async fn check_disney_plus(client: &Client) -> UnlockItem {
return UnlockItem {
name: "Disney+".to_string(),
status: "Yes".to_string(),
region: Some(format!("{}{} (from main page)", emoji, region)),
region: Some(format!("{emoji}{region} (from main page)")),
check_time: Some(get_local_date_string()),
};
}
@@ -947,7 +945,7 @@ async fn check_disney_plus(client: &Client) -> UnlockItem {
return UnlockItem {
name: "Disney+".to_string(),
status: "Yes".to_string(),
region: Some(format!("{}{} (from main page)", emoji, region)),
region: Some(format!("{emoji}{region} (from main page)")),
check_time: Some(get_local_date_string()),
};
}
@@ -968,7 +966,7 @@ async fn check_disney_plus(client: &Client) -> UnlockItem {
return UnlockItem {
name: "Disney+".to_string(),
status: "Yes".to_string(),
region: Some(format!("{}{}", emoji, region)),
region: Some(format!("{emoji}{region}")),
check_time: Some(get_local_date_string()),
};
}
@@ -990,7 +988,7 @@ async fn check_disney_plus(client: &Client) -> UnlockItem {
UnlockItem {
name: "Disney+".to_string(),
status: "Soon".to_string(),
region: Some(format!("{}{}(即将上线)", emoji, region)),
region: Some(format!("{emoji}{region}(即将上线)")),
check_time: Some(get_local_date_string()),
}
}
@@ -999,13 +997,13 @@ async fn check_disney_plus(client: &Client) -> UnlockItem {
UnlockItem {
name: "Disney+".to_string(),
status: "Yes".to_string(),
region: Some(format!("{}{}", emoji, region)),
region: Some(format!("{emoji}{region}")),
check_time: Some(get_local_date_string()),
}
}
None => UnlockItem {
name: "Disney+".to_string(),
status: format!("Failed (Error: Unknown region status for {})", region),
status: format!("Failed (Error: Unknown region status for {region})"),
region: None,
check_time: Some(get_local_date_string()),
},
@@ -1056,7 +1054,7 @@ async fn check_prime_video(client: &Client) -> UnlockItem {
return UnlockItem {
name: "Prime Video".to_string(),
status: "Yes".to_string(),
region: Some(format!("{}{}", emoji, region)),
region: Some(format!("{emoji}{region}")),
check_time: Some(get_local_date_string()),
};
}
@@ -1170,7 +1168,7 @@ pub async fn check_media_unlock() -> Result<Vec<UnlockItem>, String> {
.connection_verbose(true) // 详细连接信息
.build() {
Ok(client) => client,
Err(e) => return Err(format!("创建HTTP客户端失败: {}", e)),
Err(e) => return Err(format!("创建HTTP客户端失败: {e}")),
};
// 创建共享的结果集
@@ -1284,7 +1282,7 @@ pub async fn check_media_unlock() -> Result<Vec<UnlockItem>, String> {
// 等待所有任务完成
while let Some(res) = tasks.join_next().await {
if let Err(e) = res {
eprintln!("任务执行失败: {}", e);
eprintln!("任务执行失败: {e}");
}
}

View File

@@ -54,7 +54,7 @@ pub fn get_system_hostname() -> CmdResult<String> {
Ok(name) => name,
Err(os_string) => {
// 对于包含非UTF-8的主机名使用调试格式化
let fallback = format!("{:?}", os_string);
let fallback = format!("{os_string:?}");
// 去掉可能存在的引号
fallback.trim_matches('"').to_string()
}

View File

@@ -290,7 +290,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
logging!(info, Type::Cmd, true, "目标配置文件语法正确");
}
Ok(Err(err)) => {
let error_msg = format!(" {}", err);
let error_msg = format!(" {err}");
logging!(
error,
Type::Cmd,
@@ -305,7 +305,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
return Ok(false);
}
Err(join_err) => {
let error_msg = format!("YAML解析任务失败: {}", join_err);
let error_msg = format!("YAML解析任务失败: {join_err}");
logging!(error, Type::Cmd, true, "{}", error_msg);
handle::Handle::notice_message(
"config_validate::yaml_parse_error",
@@ -316,7 +316,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
}
}
Ok(Err(err)) => {
let error_msg = format!("无法读取目标配置文件: {}", err);
let error_msg = format!("无法读取目标配置文件: {err}");
logging!(error, Type::Cmd, true, "{}", error_msg);
handle::Handle::notice_message(
"config_validate::file_read_error",
@@ -437,22 +437,22 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
// 强制刷新代理缓存确保profile切换后立即获取最新节点数据
crate::process::AsyncHandler::spawn(|| async move {
if let Err(e) = super::proxy::force_refresh_proxies().await {
log::warn!(target: "app", "强制刷新代理缓存失败: {}", e);
log::warn!(target: "app", "强制刷新代理缓存失败: {e}");
}
});
crate::process::AsyncHandler::spawn(|| async move {
if let Err(e) = Tray::global().update_tooltip() {
log::warn!(target: "app", "异步更新托盘提示失败: {}", e);
log::warn!(target: "app", "异步更新托盘提示失败: {e}");
}
if let Err(e) = Tray::global().update_menu() {
log::warn!(target: "app", "异步更新托盘菜单失败: {}", e);
log::warn!(target: "app", "异步更新托盘菜单失败: {e}");
}
// 保存配置文件
if let Err(e) = Config::profiles().data().save_file() {
log::warn!(target: "app", "异步保存配置文件失败: {}", e);
log::warn!(target: "app", "异步保存配置文件失败: {e}");
}
});
@@ -495,7 +495,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
crate::process::AsyncHandler::spawn(|| async move {
if let Err(e) = Config::profiles().data().save_file() {
log::warn!(target: "app", "异步保存恢复配置文件失败: {}", e);
log::warn!(target: "app", "异步保存恢复配置文件失败: {e}");
}
});

View File

@@ -24,7 +24,7 @@ static APP_START_TIME: Lazy<AtomicI64> = Lazy::new(|| {
#[tauri::command]
pub async fn export_diagnostic_info() -> CmdResult<()> {
let sysinfo = PlatformSpecification::new_async().await;
let info = format!("{:?}", sysinfo);
let info = format!("{sysinfo:?}");
let app_handle = handle::Handle::global().app_handle().unwrap();
let cliboard = app_handle.clipboard();
@@ -37,7 +37,7 @@ pub async fn export_diagnostic_info() -> CmdResult<()> {
#[tauri::command]
pub async fn get_system_info() -> CmdResult<String> {
let sysinfo = PlatformSpecification::new_async().await;
let info = format!("{:?}", sysinfo);
let info = format!("{sysinfo:?}");
Ok(info)
}