2021-06-11 20:56:53 +00:00
package main
import (
2021-07-20 13:39:20 +00:00
"fmt"
2021-06-11 20:56:53 +00:00
"html/template"
"log"
"os"
2023-02-02 13:42:15 +00:00
"os/exec"
2021-06-11 20:56:53 +00:00
"path/filepath"
2021-07-20 13:57:48 +00:00
"runtime"
"runtime/debug"
"strings"
2021-06-11 20:56:53 +00:00
"time"
2023-06-12 14:48:07 +00:00
"github.com/robert-nix/ansihtml"
2021-06-11 20:56:53 +00:00
)
2021-07-20 13:57:48 +00:00
// https://gist.github.com/swdunlop/9629168
func identifyPanic ( ) string {
var name , file string
var line int
var pc [ 16 ] uintptr
n := runtime . Callers ( 4 , pc [ : ] )
for _ , pc := range pc [ : n ] {
fn := runtime . FuncForPC ( pc )
if fn == nil {
continue
}
file , line = fn . FileLine ( pc )
name = fn . Name ( )
if ! strings . HasPrefix ( name , "runtime." ) {
break
}
}
switch {
case name != "" :
return fmt . Sprintf ( "%v:%v" , name , line )
case file != "" :
return fmt . Sprintf ( "%v:%v" , file , line )
}
return fmt . Sprintf ( "pc:%x" , pc )
}
2023-02-02 13:42:15 +00:00
// OpenFile attempts to open a given file in the appropriate GUI application.
func OpenFile ( fpath string ) ( err error ) {
switch PLATFORM {
case "linux" :
err = exec . Command ( "xdg-open" , fpath ) . Start ( )
case "windows" :
err = exec . Command ( "rundll32" , "url.dll,FileProtocolHandler" , fpath ) . Start ( )
case "darwin" :
err = exec . Command ( "open" , fpath ) . Start ( )
default :
err = fmt . Errorf ( "unknown os" )
}
return
}
2021-06-11 20:56:53 +00:00
// Exit dumps the last 100 lines of output to a crash file in /tmp (or equivalent), and generates a prettier HTML file containing it that is opened in the browser if possible.
func Exit ( err interface { } ) {
tmpl , err2 := template . ParseFS ( localFS , "html/crash.html" , "html/header.html" )
if err2 != nil {
log . Fatalf ( "Failed to load template: %v" , err )
}
logCache := lineCache . String ( )
2021-08-25 17:10:06 +00:00
if err != nil {
fmt . Println ( err )
logCache += "\n" + fmt . Sprint ( err )
}
2021-07-20 13:57:48 +00:00
logCache += "\n" + string ( debug . Stack ( ) )
2021-06-11 22:14:16 +00:00
sanitized := sanitizeLog ( logCache )
2021-06-11 20:56:53 +00:00
data := map [ string ] interface { } {
2021-06-11 22:14:16 +00:00
"Log" : logCache ,
"SanitizedLog" : sanitized ,
2021-06-11 20:56:53 +00:00
}
if err != nil {
2021-07-20 13:57:48 +00:00
data [ "Err" ] = fmt . Sprintf ( "%s %v" , identifyPanic ( ) , err )
2021-06-11 20:56:53 +00:00
}
2023-02-02 13:42:15 +00:00
// Use dashes for time rather than colons for Windows
fpath := filepath . Join ( temp , "jfa-go-crash-" + time . Now ( ) . Local ( ) . Format ( "2006-01-02T15-04-05" ) )
2021-06-11 20:56:53 +00:00
err2 = os . WriteFile ( fpath + ".txt" , [ ] byte ( logCache ) , 0666 )
if err2 != nil {
log . Fatalf ( "Failed to write crash dump file: %v" , err2 )
}
2021-06-11 22:14:16 +00:00
log . Printf ( "\n------\nA crash report has been saved to \"%s\".\n------" , fpath + ".txt" )
2023-06-12 14:48:07 +00:00
// Render ANSI colors to HTML
data [ "Log" ] = template . HTML ( string ( ansihtml . ConvertToHTML ( [ ] byte ( data [ "Log" ] . ( string ) ) ) ) )
data [ "SanitizedLog" ] = template . HTML ( string ( ansihtml . ConvertToHTML ( [ ] byte ( data [ "SanitizedLog" ] . ( string ) ) ) ) )
data [ "Err" ] = template . HTML ( string ( ansihtml . ConvertToHTML ( [ ] byte ( data [ "Err" ] . ( string ) ) ) ) )
2021-06-11 20:56:53 +00:00
f , err2 := os . OpenFile ( fpath + ".html" , os . O_RDWR | os . O_CREATE | os . O_TRUNC , 0666 )
if err2 != nil {
log . Fatalf ( "Failed to open crash dump file: %v" , err2 )
}
defer f . Close ( )
err2 = tmpl . Execute ( f , data )
if err2 != nil {
log . Fatalf ( "Failed to execute template: %v" , err2 )
}
2023-02-02 13:42:15 +00:00
if err := OpenFile ( fpath + ".html" ) ; err != nil {
log . Printf ( "Failed to open browser, trying text file..." )
OpenFile ( fpath + ".txt" )
}
2021-08-16 19:41:07 +00:00
if TRAY {
2021-08-18 16:25:16 +00:00
QuitTray ( )
2021-08-16 19:41:07 +00:00
} else {
os . Exit ( 1 )
}
2021-06-11 20:56:53 +00:00
}