Skip to content

Commit

Permalink
fix: add a panic hook to collect logs and show a dialog (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Dec 25, 2023
1 parent 3c48bb6 commit f23eb73
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions backend/tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ fn main() -> std::io::Result<()> {

crate::log_err!(init::init_config());

// Panic Hook to show a panic dialog and save logs
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
error!(format!("panic hook: {:?}", info));
utils::dialog::panic_dialog(&format!("{:?}", info));
default_panic(info);
}));

#[allow(unused_mut)]
let mut builder = tauri::Builder::default()
.system_tray(SystemTray::new())
Expand Down
18 changes: 18 additions & 0 deletions backend/tauri/src/utils/dialog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::sync::{Arc, Barrier};
use tauri::api::dialog::{MessageDialogBuilder, MessageDialogButtons, MessageDialogKind};

pub fn panic_dialog(msg: &str) {
let msg = format!(
"{}\n\nPlease report this issue to Github issue tracker.",
msg
);
let barrier = Arc::new(Barrier::new(2));
let barrier_ref = barrier.clone();
MessageDialogBuilder::new("Error", msg)
.kind(MessageDialogKind::Error)
.buttons(MessageDialogButtons::Ok)
.show(move |_| {
barrier_ref.wait();
});
barrier.wait();
}
2 changes: 1 addition & 1 deletion backend/tauri/src/utils/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static APP_DIR: &str = "clash-verge-dev";
static CLASH_CONFIG: &str = "config.yaml";
static VERGE_CONFIG: &str = "verge.yaml";
static PROFILE_YAML: &str = "profiles.yaml";
static STORAGE_DB: &str = "storage.db";
static STORAGE_DB: &str = "storage";

static mut RESOURCE_DIR: Option<PathBuf> = None;

Expand Down
1 change: 1 addition & 0 deletions backend/tauri/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod candy;
pub mod dialog;
pub mod dirs;
pub mod help;
pub mod init;
Expand Down

0 comments on commit f23eb73

Please sign in to comment.