Skip to content

Commit

Permalink
add custom panic handler
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Nov 17, 2023
1 parent 1eff1b0 commit 587700e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ mod regex;
mod xmr;
mod macros;
mod free;
mod panic;
use {macros::*,crate::regex::*,ferris::*,constants::*,node::*,disk::*,update::*,gupax::*,helper::*};

// Sudo (dummy values for Windows)
Expand Down Expand Up @@ -1116,9 +1117,16 @@ fn cmp_f64(a: f64, b: f64) -> std::cmp::Ordering {
//---------------------------------------------------------------------------------------------------- Main [App] frame
fn main() {
let now = Instant::now();

// Set custom panic hook.
crate::panic::set_panic_hook(now);

// Init logger.
init_logger(now);
let mut app = App::new(now);
init_auto(&mut app);

// Init GUI stuff.
let selected_width = app.state.gupax.selected_width as f32;
let selected_height = app.state.gupax.selected_height as f32;
let initial_window_size = if selected_width > APP_MAX_WIDTH || selected_height > APP_MAX_HEIGHT {
Expand All @@ -1128,10 +1136,14 @@ fn main() {
Some(Vec2::new(app.state.gupax.selected_width as f32, app.state.gupax.selected_height as f32))
};
let options = init_options(initial_window_size);

// Gupax folder cleanup.
match clean_dir() {
Ok(_) => info!("Temporary folder cleanup ... OK"),
Err(e) => warn!("Could not cleanup [gupax_tmp] folders: {}", e),
}

// Run Gupax.
info!("/*************************************/ Init ... OK /*************************************/");
eframe::run_native(&app.name_version.clone(), options, Box::new(|cc| Box::new(App::cc(cc, app))),);
}
Expand Down
50 changes: 50 additions & 0 deletions src/panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//---------------------------------------------------------------------------------------------------- Use
use crate::constants::{
GUPAX_VERSION,
P2POOL_VERSION,
XMRIG_VERSION,
OS_NAME,
COMMIT,
};

//----------------------------------------------------------------------------------------------------
/// Set custom panic hook.
pub(crate) fn set_panic_hook(now: std::time::Instant) {
std::panic::set_hook(Box::new(move |panic_info| {
// Set stack-trace.
let stack_trace = std::backtrace::Backtrace::force_capture();
let args = std::env::args_os();
let uptime = now.elapsed().as_secs_f32();

// Re-format panic info.
let panic_info = format!(
"{panic_info:#?}
info:
OS | {OS_NAME}
args | {args:?}
commit | {COMMIT}
gupax | {GUPAX_VERSION}
p2pool | {P2POOL_VERSION} (bundled)
xmrig | {XMRIG_VERSION} (bundled)
uptime | {uptime} seconds
stack backtrace:\n{stack_trace}",
);

// Attempt to write panic info to disk.
match crate::disk::get_gupax_data_path() {
Ok(mut path) => {
path.push("crash.txt");
match std::fs::write(&path, &panic_info) {
Ok(_) => eprintln!("\nmass_panic!() - Saved panic log to: {}\n", path.display()),
Err(e) => eprintln!("\nmass_panic!() - Could not save panic log: {e}\n"),
}
},
Err(e) => eprintln!("panic_hook PATH error: {e}"),
}

// Exit all threads.
benri::mass_panic!(panic_info);
}));
}

0 comments on commit 587700e

Please sign in to comment.