引入日志服务

This commit is contained in:
2026-01-23 21:40:09 +08:00
parent 677681e0f3
commit ec77777bf3
4 changed files with 433 additions and 3 deletions

View File

@@ -1,5 +1,7 @@
use ::log::{debug, error, info, trace, warn};
use tokio::runtime::Runtime;
mod log;
mod network;
#[cxx::bridge]
@@ -8,9 +10,45 @@ mod ffi {
fn download_file(url: &str, path: &str) -> Result<()>;
fn http_get(url: &str) -> Result<String>;
fn init_log(is_debug: bool);
fn log_trace(msg: &str);
fn log_debug(msg: &str);
fn log_info(msg: &str);
fn log_warning(msg: &str);
fn log_error(msg: &str);
}
}
fn log_error(msg: &str) {
error!("{}", msg);
}
fn log_warning(msg: &str) {
warn!("{}", msg);
}
fn log_info(msg: &str) {
info!("{}", msg);
}
fn log_debug(msg: &str) {
debug!("{}", msg);
}
fn log_trace(msg: &str) {
trace!("{}", msg);
}
fn init_log(is_debug: bool) {
log::init_log(is_debug);
}
fn get_runtime() -> Runtime {
Runtime::new().expect("创建Tokio运行时失败")
}

88
src/log.rs Normal file
View File

@@ -0,0 +1,88 @@
use chrono::Local;
use colored::{Color, Colorize};
use env_logger::fmt::Formatter;
use fern::Dispatch;
use log::Level;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::{env, fs};
/// 获取可执行文件所在目录
pub fn get_current_dir() -> PathBuf {
PathBuf::from(
env::current_exe()
.expect("获取可执行文件路径失败!")
.parent()
.expect("获取可执行文件所在目录失败!"),
)
}
pub fn get_log_path() -> PathBuf {
let exe_path = get_current_dir().join("log");
if !exe_path.exists() {
fs::create_dir_all(&exe_path).expect("创建日志目录失败!");
}
exe_path
}
fn get_time() -> String {
let now = Local::now();
now.format("%Y-%m-%d %H:%M:%S").to_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() {
Level::Error => (Color::Red, "Error"),
Level::Warn => (Color::Yellow, "Warning"),
Level::Info => (Color::Green, "Info"),
Level::Debug => (Color::BrightWhite, "Debug"),
Level::Trace => (Color::White, "Trace"),
};
out.finish(format_args!(
"[{} {}]\t{}",
get_time(),
title.color(color),
message
))
})
.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("写入日志失败");
}