Skip to content

Commit

Permalink
Better logging settings in the AppConfig struct
Browse files Browse the repository at this point in the history
  • Loading branch information
fschutt committed Oct 11, 2018
1 parent 85fd0b4 commit 495e68a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
22 changes: 16 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ pub struct AppConfig {
/// Path to the output log if the logger is enabled
#[cfg(feature = "logging")]
pub log_file_path: Option<String>,
/// If the app crashes / panics, a window with a message box pops up
/// Additionally, the error + backtrace gets logged to the output
/// file (if logging is enabled).
/// If the app crashes / panics, a window with a message box pops up.
/// Setting this to `false` disables the popup box.
#[cfg(feature = "logging")]
pub enable_visual_panic_hook: bool,
/// If this is set to `true` (the default), a backtrace + error information
/// gets logged to stdout and the logging file (only if logging is enabled).
#[cfg(feature = "logging")]
pub enable_logging_on_panic: bool,
}

impl Default for AppConfig {
Expand All @@ -123,6 +126,8 @@ impl Default for AppConfig {
log_file_path: None,
#[cfg(feature = "logging")]
enable_visual_panic_hook: true,
#[cfg(feature = "logging")]
enable_logging_on_panic: true,
}
}
}
Expand All @@ -135,10 +140,15 @@ impl<T: Layout> App<T> {
#[cfg(feature = "logging")] {
if let Some(log_level) = config.enable_logging {
::logging::set_up_logging(config.log_file_path, log_level);
}

if config.enable_visual_panic_hook {
::logging::set_up_panic_hooks();
if config.enable_logging_on_panic {
::logging::set_up_panic_hooks();
}

if config.enable_visual_panic_hook {
use std::sync::atomic::Ordering;
::logging::SHOULD_ENABLE_PANIC_HOOK.store(true, Ordering::SeqCst);
}
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use backtrace::{Backtrace, BacktraceFrame};
use dialogs::msg_box_ok;
use log::LevelFilter;
use std::sync::atomic::{Ordering, AtomicBool};

pub(crate) static SHOULD_ENABLE_PANIC_HOOK: AtomicBool = AtomicBool::new(false);

pub(crate) fn set_up_logging(log_file_path: Option<String>, log_level: LevelFilter) {

Expand Down Expand Up @@ -92,7 +95,7 @@ pub(crate) fn set_up_panic_hooks() {
let thread_name = thread.name().unwrap_or("<unnamed thread>");

let error_str = format!(
"We are sorry, an unexpected panic ocurred, the program has to exit.\r\n\
"An unexpected panic ocurred, the program has to exit.\r\n\
Please report this error and attach the log file found in the directory of the executable.\r\n\
\r\n\
The error ocurred in: {} in thread {}\r\n\
Expand All @@ -115,10 +118,12 @@ pub(crate) fn set_up_panic_hooks() {
// TODO: invoke external app crash handler with the location to the log file
error!("{}", error_str);

#[cfg(not(target_os = "linux"))]
msg_box_ok("Unexpected fatal error", &error_str, ::tinyfiledialogs::MessageBoxIcon::Error);
#[cfg(target_os = "linux")]
msg_box_ok("Unexpected fatal error", &error_str_clone, ::tinyfiledialogs::MessageBoxIcon::Error);
if SHOULD_ENABLE_PANIC_HOOK.load(Ordering::SeqCst) {
#[cfg(not(target_os = "linux"))]
msg_box_ok("Unexpected fatal error", &error_str, ::tinyfiledialogs::MessageBoxIcon::Error);
#[cfg(target_os = "linux")]
msg_box_ok("Unexpected fatal error", &error_str_clone, ::tinyfiledialogs::MessageBoxIcon::Error);
}
}

panic::set_hook(Box::new(panic_fn));
Expand Down

0 comments on commit 495e68a

Please sign in to comment.