引入日志服务

This commit is contained in:
2026-01-23 23:10:29 +08:00
parent ec77777bf3
commit 1bb2ca0f40
4 changed files with 205 additions and 47 deletions

View File

@@ -1,4 +1,5 @@
use ::log::{debug, error, info, trace, warn};
use encoding_rs::Encoding;
use tokio::runtime::Runtime;
mod log;
@@ -7,41 +8,46 @@ mod network;
#[cxx::bridge]
mod ffi {
extern "Rust" {
fn download_file(url: &str, path: &str) -> Result<()>;
fn download_file(url: &CxxString, path: &CxxString) -> Result<()>;
fn http_get(url: &str) -> Result<String>;
fn http_get(url: &CxxString) -> Result<String>;
fn init_log(is_debug: bool);
fn log_trace(msg: &str);
fn log_trace(msg: &CxxString);
fn log_debug(msg: &str);
fn log_debug(msg: &CxxString);
fn log_info(msg: &str);
fn log_info(msg: &CxxString);
fn log_warning(msg: &str);
fn log_warning(msg: &CxxString);
fn log_error(msg: &str);
fn log_error(msg: &CxxString);
}
}
fn log_error(msg: &str) {
fn log_error(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg);
error!("{}", msg);
}
fn log_warning(msg: &str) {
fn log_warning(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg);
warn!("{}", msg);
}
fn log_info(msg: &str) {
fn log_info(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg);
info!("{}", msg);
}
fn log_debug(msg: &str) {
fn log_debug(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg);
debug!("{}", msg);
}
fn log_trace(msg: &str) {
fn log_trace(msg: &cxx::CxxString) {
let msg = cxx_string_to_string(msg);
trace!("{}", msg);
}
@@ -53,13 +59,47 @@ fn get_runtime() -> Runtime {
Runtime::new().expect("创建Tokio运行时失败")
}
fn download_file(url: &str, path: &str) -> Result<(), Box<dyn std::error::Error>> {
fn download_file(
url: &cxx::CxxString,
path: &cxx::CxxString,
) -> Result<(), Box<dyn std::error::Error>> {
let rt = get_runtime();
rt.block_on(network::download_file(url, path))?;
let url = cxx_string_to_string(url);
let path = cxx_string_to_string(path);
rt.block_on(network::download_file(&url, &path))?;
Ok(())
}
fn http_get(url: &str) -> Result<String, Box<dyn std::error::Error>> {
fn http_get(url: &cxx::CxxString) -> Result<String, Box<dyn std::error::Error>> {
let rt = get_runtime();
rt.block_on(network::http_get(url))
let url = cxx_string_to_string(url);
rt.block_on(network::http_get(&url))
}
fn cxx_string_to_string(s: &cxx::CxxString) -> String {
match s.to_str() {
Ok(s) => return s.to_string(),
Err(_) => {}
};
// 不是UTF-8尝试转换
let candidates = [
"gb18030", // 覆盖 GBK/GB2312 的常见场景
"windows-1252", // 西欧常见
"shift_jis", // 日文常见
"big5", // 繁体中文常见
];
let bytes = s.as_bytes();
for label in candidates {
let enc = Encoding::for_label(label.as_bytes()).unwrap();
let (cow, _actual_used, had_errors) = enc.decode(bytes);
if !had_errors {
return cow.into_owned();
}
}
/// 编码全部没有命中的话,则丢弃无法解析的部分
s.to_string_lossy().into_owned()
}

View File

@@ -30,20 +30,6 @@ fn get_time() -> String {
}
pub fn init_log(is_debug: bool) {
let mut file_dispatch = Dispatch::new()
.chain(fern::log_file(get_log_path().join("app.log")).expect("无法写入日志文件"))
.format(|out, message, record| {
let title = match record.level() {
Level::Error => "Error",
Level::Warn => "Warning",
Level::Info => "Info",
Level::Debug => "Debug",
Level::Trace => "Trace",
};
out.finish(format_args!("[{} {}]\t{}", get_time(), title, message))
});
let mut console_dispatch = Dispatch::new()
.format(|out, message, record| {
let (color, title) = match record.level() {
@@ -61,28 +47,13 @@ pub fn init_log(is_debug: bool) {
message
))
})
.chain(fern::log_file(get_log_path().join("app.log")).expect("无法写入日志文件"))
.chain(std::io::stdout());
if is_debug {
console_dispatch = console_dispatch.level(log::LevelFilter::Trace);
file_dispatch = file_dispatch.level(log::LevelFilter::Trace);
} else {
console_dispatch = console_dispatch.level(log::LevelFilter::Info);
file_dispatch = file_dispatch.level(log::LevelFilter::Debug);
}
/*
#[cfg(debug_assertions)]
let console_dispatch = console_dispatch.level(log::LevelFilter::Debug);
#[cfg(debug_assertions)]
let file_dispatch = file_dispatch.level(log::LevelFilter::Off);
#[cfg(not(debug_assertions))]
let console_dispatch = console_dispatch.level(log::LevelFilter::Info);
#[cfg(not(debug_assertions))]
let file_dispatch = file_dispatch.level(log::LevelFilter::Debug);
*/
console_dispatch
.chain(file_dispatch)
.apply()
.expect("写入日志失败");
console_dispatch.apply().expect("写入日志失败");
}