rework (error) logging

This commit is contained in:
2025-05-16 10:03:48 +01:00
parent 9dbfbc4419
commit 10e69eaac3
6 changed files with 78 additions and 18 deletions
+3 -17
View File
@@ -3,28 +3,14 @@ use crate::{
app::{App, WINDOW_SIZE},
tools,
},
util::{consts::path, persistent_state::PersistentState, win},
util::{log, persistent_state::PersistentState, win},
};
use eframe::egui;
use std::{
fs::File,
io::Write,
time::{SystemTime, UNIX_EPOCH},
};
fn panic_hook(panic_info: &std::panic::PanicHookInfo<'_>) {
let mut file = File::options()
.create(true)
.append(true)
.open(path::APP_ERROR.as_path())
.unwrap();
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let backtrace = std::backtrace::Backtrace::capture();
let message = format!("[{timestamp}]\n{panic_info}\nstack backtrace:\n{backtrace}\n");
file.write_all(message.as_bytes()).unwrap();
let message = format!("{panic_info}\nstack backtrace:\n{backtrace}\n");
log::log(log::LogLevel::Panic, message);
}
fn app_creator(
+1
View File
@@ -1,6 +1,7 @@
mod codeberg;
pub mod consts;
pub mod countdown;
pub mod log;
pub mod meta;
pub mod persistent_state;
pub mod win;
+1 -1
View File
@@ -5,7 +5,7 @@ pub mod path {
.join("GTA Tools")
});
pub static APP_CONFIG: LazyLock<PathBuf> = LazyLock::new(|| APP_STORAGE.join("config.json"));
pub static APP_ERROR: LazyLock<PathBuf> = LazyLock::new(|| APP_STORAGE.join("error.log"));
pub static APP_LOG: LazyLock<PathBuf> = LazyLock::new(|| APP_STORAGE.join("gta-tools.log"));
}
pub mod game {
+20
View File
@@ -0,0 +1,20 @@
use crate::util::consts::path;
use std::{fs::File, io::Write};
use strum::Display;
#[derive(Display)]
pub enum LogLevel {
Error,
Panic,
}
pub fn log(level: LogLevel, message: String) {
let mut file = File::options()
.create(true)
.append(true)
.open(path::APP_LOG.as_path())
.unwrap();
let timestamp = chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, false);
let message = format!("[{timestamp}] [{level}]\n{message}\n\n");
file.write_all(message.as_bytes()).unwrap();
}