mirror of
https://github.com/0PandaDEV/Qopy.git
synced 2025-04-22 05:34:04 +02:00
feat: add logging for debugging
This commit is contained in:
parent
00749a9d3a
commit
14a13f44bb
5 changed files with 54 additions and 0 deletions
1
src-tauri/Cargo.lock
generated
1
src-tauri/Cargo.lock
generated
|
@ -4187,6 +4187,7 @@ dependencies = [
|
||||||
"image",
|
"image",
|
||||||
"include_dir",
|
"include_dir",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
"log",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"rdev",
|
"rdev",
|
||||||
"regex",
|
"regex",
|
||||||
|
|
|
@ -40,6 +40,7 @@ lazy_static = "1.5.0"
|
||||||
time = "0.3.37"
|
time = "0.3.37"
|
||||||
global-hotkey = "0.6.3"
|
global-hotkey = "0.6.3"
|
||||||
chrono = { version = "0.4.39", features = ["serde"] }
|
chrono = { version = "0.4.39", features = ["serde"] }
|
||||||
|
log = { version = "0.4.22", features = ["std"] }
|
||||||
uuid = "1.11.0"
|
uuid = "1.11.0"
|
||||||
active-win-pos-rs = "0.8.3"
|
active-win-pos-rs = "0.8.3"
|
||||||
include_dir = "0.7.4"
|
include_dir = "0.7.4"
|
||||||
|
|
|
@ -34,6 +34,8 @@ fn main() {
|
||||||
)
|
)
|
||||||
.setup(|app| {
|
.setup(|app| {
|
||||||
let app_data_dir = app.path().app_data_dir().unwrap();
|
let app_data_dir = app.path().app_data_dir().unwrap();
|
||||||
|
utils::logger::init_logger(&app_data_dir).expect("Failed to initialize logger");
|
||||||
|
|
||||||
fs::create_dir_all(&app_data_dir).expect("Failed to create app data directory");
|
fs::create_dir_all(&app_data_dir).expect("Failed to create app data directory");
|
||||||
|
|
||||||
let db_path = app_data_dir.join("data.db");
|
let db_path = app_data_dir.join("data.db");
|
||||||
|
|
49
src-tauri/src/utils/logger.rs
Normal file
49
src-tauri/src/utils/logger.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
use chrono;
|
||||||
|
use log::{LevelFilter, SetLoggerError};
|
||||||
|
use std::fs::{File, OpenOptions};
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
pub struct FileLogger {
|
||||||
|
file: File,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl log::Log for FileLogger {
|
||||||
|
fn enabled(&self, _metadata: &log::Metadata) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn log(&self, record: &log::Record) {
|
||||||
|
if self.enabled(record.metadata()) {
|
||||||
|
let mut file = self.file.try_clone().expect("Failed to clone file handle");
|
||||||
|
writeln!(
|
||||||
|
file,
|
||||||
|
"{} - {}: {}",
|
||||||
|
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
|
||||||
|
record.level(),
|
||||||
|
record.args()
|
||||||
|
)
|
||||||
|
.expect("Failed to write to log file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&self) {
|
||||||
|
self.file.sync_all().expect("Failed to flush log file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_logger(app_data_dir: &std::path::Path) -> Result<(), SetLoggerError> {
|
||||||
|
let logs_dir = app_data_dir.join("logs");
|
||||||
|
std::fs::create_dir_all(&logs_dir).expect("Failed to create logs directory");
|
||||||
|
|
||||||
|
let log_path = logs_dir.join("app.log");
|
||||||
|
let file = OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.append(true)
|
||||||
|
.open(log_path)
|
||||||
|
.expect("Failed to open log file");
|
||||||
|
|
||||||
|
let logger = Box::new(FileLogger { file });
|
||||||
|
unsafe { log::set_logger_racy(Box::leak(logger))? };
|
||||||
|
log::set_max_level(LevelFilter::Debug);
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod favicon;
|
pub mod favicon;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
pub mod logger;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue