1- #![ cfg( feature = "fancy" ) ]
2- use std:: fmt:: Write ;
3-
41use backtrace:: Backtrace ;
52use thiserror:: Error ;
63
@@ -29,20 +26,22 @@ pub fn set_panic_hook() {
2926}
3027
3128#[ derive( Debug , Error , Diagnostic ) ]
32- #[ error( "{0}{}" , self . maybe_collect_backtrace ( ) ) ]
29+ #[ error( "{0}{}" , Panic :: backtrace ( ) ) ]
3330#[ diagnostic( help( "set the `RUST_BACKTRACE=1` environment variable to display a backtrace." ) ) ]
3431struct Panic ( String ) ;
3532
3633impl Panic {
37- fn maybe_collect_backtrace ( & self ) -> String {
34+ fn backtrace ( ) -> String {
35+ use std:: fmt:: Write ;
3836 if let Ok ( var) = std:: env:: var ( "RUST_BACKTRACE" ) {
3937 if !var. is_empty ( ) && var != "0" {
40- // This is all taken from human-panic: https://github.com/rust-cli/human-panic/blob/master/src/report.rs#L55-L107
4138 const HEX_WIDTH : usize = std:: mem:: size_of :: < usize > ( ) + 2 ;
42- //Padding for next lines after frame's address
39+ // Padding for next lines after frame's address
4340 const NEXT_SYMBOL_PADDING : usize = HEX_WIDTH + 6 ;
4441 let mut backtrace = String :: new ( ) ;
45- for ( idx, frame) in Backtrace :: new ( ) . frames ( ) . iter ( ) . skip ( 26 ) . enumerate ( ) {
42+ let trace = Backtrace :: new ( ) ;
43+ let frames = backtrace_ext:: short_frames_strict ( & trace) . enumerate ( ) ;
44+ for ( idx, ( frame, sub_frames) ) in frames {
4645 let ip = frame. ip ( ) ;
4746 let _ = write ! ( backtrace, "\n {:4}: {:2$?}" , idx, ip, HEX_WIDTH ) ;
4847
@@ -52,10 +51,10 @@ impl Panic {
5251 continue ;
5352 }
5453
55- for ( idx, symbol) in symbols. iter ( ) . enumerate ( ) {
56- //Print symbols from this address,
57- //if there are several addresses
58- //we need to put it on next line
54+ for ( idx, symbol) in symbols[ sub_frames ] . iter ( ) . enumerate ( ) {
55+ // Print symbols from this address,
56+ // if there are several addresses
57+ // we need to put it on next line
5958 if idx != 0 {
6059 let _ = write ! ( backtrace, "\n {:1$}" , "" , NEXT_SYMBOL_PADDING ) ;
6160 }
@@ -66,7 +65,7 @@ impl Panic {
6665 let _ = write ! ( backtrace, " - <unknown>" ) ;
6766 }
6867
69- //See if there is debug information with file name and line
68+ // See if there is debug information with file name and line
7069 if let ( Some ( file) , Some ( line) ) = ( symbol. filename ( ) , symbol. lineno ( ) ) {
7170 let _ = write ! (
7271 backtrace,
0 commit comments