Skip to content

Commit

Permalink
add panic hook
Browse files Browse the repository at this point in the history
  • Loading branch information
IceSentry committed Sep 1, 2023
1 parent b6de76a commit cb0b5d0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/bevy_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ pub struct FileAppenderSettings {
pub path: PathBuf,
/// The prefix added when creating a file
pub prefix: String,
/// When this is enabled, a panic hook will be used and any panic will be logged as an error
pub use_panic_hook: bool,
}

impl Default for FileAppenderSettings {
Expand All @@ -169,6 +171,7 @@ impl Default for FileAppenderSettings {
rolling: Rolling::Never,
path: PathBuf::from("."),
prefix: String::from("log"),
use_panic_hook: true,
}
}
}
Expand Down Expand Up @@ -253,6 +256,16 @@ impl Plugin for LogPlugin {
};

let file_appender_layer = if let Some(settings) = &self.file_appender_settings {
if settings.use_panic_hook {
std::panic::set_hook(Box::new(|panic_info| {
if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
error!("panic occurred: {s:?}");
} else {
error!("panic occurred");
}
}));
}

if settings.rolling == Rolling::Never && settings.prefix.is_empty() {
panic!("Using the Rolling::Never variant with no prefix will result in an empty filename which is invalid");
}
Expand Down

0 comments on commit cb0b5d0

Please sign in to comment.