log panics to a file

This commit is contained in:
2025-04-17 00:44:06 +01:00
parent 284c4bb46d
commit db4ca22f95
5 changed files with 73 additions and 9 deletions
+20 -2
View File
@@ -9,11 +9,15 @@ use crate::{
gui::{persistent_state::PersistentState, settings::Settings},
util::{
self,
consts::{ENHANCED, GTA_WINDOW_TITLE, LEGACY},
consts::{APP_STORAGE_PATH, ENHANCED, GTA_WINDOW_TITLE, LEGACY},
},
};
use eframe::egui;
use std::time::{Duration, Instant};
use std::{
fs,
io::Write,
time::{Duration, Instant},
};
const WINDOW_SIZE: [f32; 2] = [240.0, 245.0];
@@ -370,10 +374,24 @@ fn load_icon() -> egui::IconData {
}
}
fn panic_hook(panic_info: &std::panic::PanicHookInfo<'_>) {
let log_path = APP_STORAGE_PATH.join("panic.log");
let mut file = fs::File::options()
.create(true)
.append(true)
.open(log_path)
.unwrap();
let timestamp = chrono::Local::now().to_rfc3339();
let backtrace = std::backtrace::Backtrace::force_capture();
let message = format!("[{timestamp}]\n{panic_info}\n\n{backtrace}\n\n");
file.write_all(message.as_bytes()).unwrap();
}
#[allow(clippy::unnecessary_wraps)]
fn app_creator(
cc: &eframe::CreationContext<'_>,
) -> Result<Box<dyn eframe::App>, Box<dyn std::error::Error + Send + Sync>> {
std::panic::set_hook(Box::new(panic_hook));
let mut app = Box::<App>::default();
if let Some(persistent_state) = PersistentState::get() {
app.launch.selected = persistent_state.launcher;
+2 -7
View File
@@ -1,4 +1,4 @@
use crate::{features::launch::Platform, gui::settings::Settings};
use crate::{features::launch::Platform, gui::settings::Settings, util::consts::APP_STORAGE_PATH};
use serde::{Deserialize, Serialize};
use std::{
fs::{self, File},
@@ -7,12 +7,7 @@ use std::{
sync::LazyLock,
};
static CONFIG_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
dirs::config_local_dir()
.unwrap()
.join("GTA Tools")
.join("config.json")
});
static CONFIG_PATH: LazyLock<PathBuf> = LazyLock::new(|| APP_STORAGE_PATH.join("config.json"));
#[derive(Serialize, Deserialize)]
pub struct PersistentState {
+5
View File
@@ -1,4 +1,9 @@
use std::{path::PathBuf, sync::LazyLock};
pub const ENHANCED: &str = "GTA5_Enhanced.exe";
pub const LEGACY: &str = "GTA5.exe";
pub const GTA_WINDOW_TITLE: &str = "Grand Theft Auto V";
pub static APP_STORAGE_PATH: LazyLock<PathBuf> =
LazyLock::new(|| dirs::config_local_dir().unwrap().join("GTA Tools"));