From 09a42be3cfd6fee8a831941ae3073561148f290a Mon Sep 17 00:00:00 2001 From: Starpoles Date: Thu, 29 Jan 2026 20:37:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=97=A5=E5=BF=97=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E5=88=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.rs b/src/main.rs index 0ee01d0..8770193 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ use chrono::Local; use colored::{Color, Colorize}; use fern::Dispatch; use log::Level; +use std::env; +use std::path::PathBuf; #[tokio::main] async fn main() { @@ -23,6 +25,19 @@ fn get_time() -> String { now.format("%Y-%m-%d %H:%M:%S").to_string() } +pub fn get_current_dir() -> PathBuf { + PathBuf::from( + env::current_exe() + .expect("获取可执行文件路径失败!") + .parent() + .expect("获取可执行文件所在目录失败!"), + ) +} + +pub fn get_log_path() -> PathBuf { + get_current_dir().join("logs") +} + fn init_log() { let mut console_dispatch = Dispatch::new() .format(|out, message, record| { @@ -42,6 +57,7 @@ fn init_log() { )) }) .chain(std::io::stdout()) + .chain(fern::log_file(get_log_path().join("app.log")).expect("无法打开日志文件")) .level(log::LevelFilter::Debug); console_dispatch.apply().expect("写入日志失败"); }