GithubHelp home page GithubHelp logo

Comments (2)

dignifiedquire avatar dignifiedquire commented on May 28, 2024 1

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

from human-panic.

kdar avatar kdar commented on May 28, 2024

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 #55 (comment) 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...
  }));
}

from human-panic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.