Panic hook to generate crash dumps #65 (#66)

This commit is contained in:
DecDuck
2025-07-18 16:35:02 +10:00
committed by GitHub
parent c477dd4872
commit 495d93705e
5 changed files with 42 additions and 23 deletions

View File

@ -82,7 +82,7 @@ pub fn fetch_system_data() -> SystemData {
SystemData::new(
db_handle.auth.as_ref().unwrap().client_id.clone(),
db_handle.base_url.clone(),
DATA_ROOT_DIR.lock().unwrap().to_string_lossy().to_string(),
DATA_ROOT_DIR.to_string_lossy().to_string(),
std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string()),
)
}

View File

@ -1,9 +1,5 @@
use std::{
fs::{self, create_dir_all},
mem::ManuallyDrop,
ops::{Deref, DerefMut},
path::PathBuf,
sync::{LazyLock, Mutex, RwLockReadGuard, RwLockWriteGuard},
fs::{self, create_dir_all}, mem::ManuallyDrop, ops::{Deref, DerefMut}, path::PathBuf, rc::Rc, sync::{Arc, LazyLock, Mutex, RwLockReadGuard, RwLockWriteGuard}
};
use chrono::Utc;
@ -17,8 +13,8 @@ use crate::DB;
use super::models::data::Database;
pub static DATA_ROOT_DIR: LazyLock<Mutex<PathBuf>> =
LazyLock::new(|| Mutex::new(dirs::data_dir().unwrap().join("drop")));
pub static DATA_ROOT_DIR: LazyLock<Arc<PathBuf>> =
LazyLock::new(|| Arc::new(dirs::data_dir().unwrap().join("drop")));
// Custom JSON serializer to support everything we need
#[derive(Debug, Default, Clone)]
@ -52,15 +48,14 @@ pub trait DatabaseImpls {
}
impl DatabaseImpls for DatabaseInterface {
fn set_up_database() -> DatabaseInterface {
let data_root_dir = DATA_ROOT_DIR.lock().unwrap();
let db_path = data_root_dir.join("drop.db");
let games_base_dir = data_root_dir.join("games");
let logs_root_dir = data_root_dir.join("logs");
let cache_dir = data_root_dir.join("cache");
let pfx_dir = data_root_dir.join("pfx");
let db_path = DATA_ROOT_DIR.join("drop.db");
let games_base_dir = DATA_ROOT_DIR.join("games");
let logs_root_dir = DATA_ROOT_DIR.join("logs");
let cache_dir = DATA_ROOT_DIR.join("cache");
let pfx_dir = DATA_ROOT_DIR.join("pfx");
debug!("creating data directory at {:?}", data_root_dir);
create_dir_all(data_root_dir.clone()).unwrap();
debug!("creating data directory at {:?}", DATA_ROOT_DIR);
create_dir_all(DATA_ROOT_DIR.as_path()).unwrap();
create_dir_all(&games_base_dir).unwrap();
create_dir_all(&logs_root_dir).unwrap();
create_dir_all(&cache_dir).unwrap();

View File

@ -1,3 +1,5 @@
#![feature(fn_traits)]
mod database;
mod games;
@ -49,14 +51,18 @@ use remote::commands::{
use remote::fetch_object::{fetch_object, fetch_object_offline};
use remote::server_proto::{handle_server_proto, handle_server_proto_offline};
use serde::{Deserialize, Serialize};
use std::env;
use std::fs::File;
use std::io::Write;
use std::panic::PanicHookInfo;
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;
use std::time::SystemTime;
use std::{
collections::HashMap,
sync::{LazyLock, Mutex},
};
use std::{env, panic};
use tauri::menu::{Menu, MenuItem, PredefinedMenuItem};
use tauri::tray::TrayIconBuilder;
use tauri::{AppHandle, Manager, RunEvent, WindowEvent};
@ -103,7 +109,7 @@ fn setup(handle: AppHandle) -> AppState<'static> {
"{d} | {l} | {f}:{L} - {m}{n}",
)))
.append(false)
.build(DATA_ROOT_DIR.lock().unwrap().join("./drop.log"))
.build(DATA_ROOT_DIR.join("./drop.log"))
.unwrap();
let console = ConsoleAppender::builder()
@ -209,8 +215,29 @@ fn setup(handle: AppHandle) -> AppState<'static> {
pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(DatabaseInterface::set_up_database);
pub fn custom_panic_handler(e: &PanicHookInfo) -> Option<()> {
let crash_file = DATA_ROOT_DIR.join(format!(
"crash-{}.log",
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.ok()?
.as_secs()
));
let mut file = File::create_new(crash_file).ok()?;
file.write_all(format!("Drop crashed with the following panic:\n{}", e).as_bytes()).ok()?;
drop(file);
Some(())
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
panic::set_hook(Box::new(|e| {
let _ = custom_panic_handler(e);
let dft = panic::take_hook();
dft.call((e,));
}));
let mut builder = tauri::Builder::default()
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_dialog::init());
@ -302,7 +329,7 @@ pub fn run() {
.inner_size(1536.0, 864.0)
.decorations(false)
.shadow(false)
.data_directory(DATA_ROOT_DIR.lock().unwrap().join(".webview"))
.data_directory(DATA_ROOT_DIR.join(".webview"))
.build()
.unwrap();

View File

@ -39,9 +39,7 @@ pub struct ProcessManager<'a> {
impl ProcessManager<'_> {
pub fn new(app_handle: AppHandle) -> Self {
let root_dir_lock = DATA_ROOT_DIR.lock().unwrap();
let log_output_dir = root_dir_lock.join("logs");
drop(root_dir_lock);
let log_output_dir = DATA_ROOT_DIR.join("logs");
ProcessManager {
#[cfg(target_os = "windows")]