human-panic icon indicating copy to clipboard operation
human-panic copied to clipboard

🙋 Option to print the report

Open dignifiedquire opened this issue 5 years ago • 2 comments

While on desktop applications it makes sense to store the crash report in a file, on mobile devices it can be challenging to actually find the report file, especially for end users. So for these cases I would like to print the whole report, after message, such that it is easier to report. Is this sth you would consider? Alternatively having a hook to customize the file writing would likely solve this issue as well. Thank you

dignifiedquire avatar Jul 06 '19 09:07 dignifiedquire

Maybe not ideal, but you could get the filename via handle_dump, and then read it and display it to the user.

Another option is to use code similar to https://github.com/rust-cli/human-panic/issues/55#issuecomment-473926139 and not use human_panic. Instead of logging to a file you just build and use the string. Then you can actually get the panic message as well.

For reference, this is code I'm using in my own project:

use std::panic::{self, PanicInfo};

use backtrace::Backtrace;

pub fn setup() {
  panic::set_hook(Box::new(move |info: &PanicInfo| {
    let mut msg = String::new();

    let os = if cfg!(target_os = "windows") {
      "Windows"
    } else if cfg!(target_os = "linux") {
      "Linux"
    } else if cfg!(target_os = "macos") {
      "Mac OS"
    } else if cfg!(target_os = "android") {
      "Android"
    } else {
      "Unknown"
    };

    msg.push_str(&format!("Name: {}\n", env!("CARGO_PKG_NAME")));
    msg.push_str(&format!("Version: {}\n", env!("CARGO_PKG_VERSION")));
    msg.push_str(&format!("Operating System: {}\n", os));

    if let Some(inner) = info.payload().downcast_ref::<&str>() {
      msg.push_str(&format!("Cause: {}.\n", &inner));
    }

    match info.location() {
      Some(location) => msg.push_str(&format!(
        "Location: in file '{}' at line {}\n",
        location.file(),
        location.line()
      )),
      None => msg.push_str("Panic location unknown.\n"),
    };

    msg.push_str(&format!("Message: {}\n", info));

    msg.push_str(&format!("\n{:#?}\n", Backtrace::new()));

    // Do something with msg...
  }));
}

kdar avatar Jul 17 '19 19:07 kdar

thanks @kdar this is really helpful, will try this out

dignifiedquire avatar Jul 17 '19 19:07 dignifiedquire