mirror of
https://github.com/hrfee/jfa-go.git
synced 2026-01-18 16:47:42 +01:00
Generate crash report txt and webpage
The last 100 lines of logs are now cached, and when a crash occurs, they
are saved to a file in the temp directory ("/tmp" on *nix), and pretty
HTML version is also created and opened in the browser.
* Currently only handles panics, will be included in more places soon
* Copy button and button to generate a GH issue will be added
This commit is contained in:
69
log.go
Normal file
69
log.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/hrfee/jfa-go/linecache"
|
||||
)
|
||||
|
||||
var logPath string = filepath.Join(temp, "jfa-go.log")
|
||||
var lineCache = linecache.NewLineCache(100)
|
||||
|
||||
func logOutput() (closeFunc func()) {
|
||||
old := os.Stdout
|
||||
log.Printf("Logging to \"%s\"", logPath)
|
||||
writers := []io.Writer{old, colorStripper{lineCache}}
|
||||
wExit := make(chan bool)
|
||||
r, w, _ := os.Pipe()
|
||||
if TRAY {
|
||||
f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
closeFunc = func() {}
|
||||
return
|
||||
}
|
||||
writers = append(writers, colorStripper{f})
|
||||
closeFunc = func() {
|
||||
w.Close()
|
||||
<-wExit
|
||||
f.Close()
|
||||
}
|
||||
} else {
|
||||
closeFunc = func() {
|
||||
w.Close()
|
||||
<-wExit
|
||||
}
|
||||
}
|
||||
writer := io.MultiWriter(writers...)
|
||||
os.Stdout, os.Stderr = w, w
|
||||
log.SetOutput(writer)
|
||||
gin.DefaultWriter, gin.DefaultErrorWriter = writer, writer
|
||||
go func() {
|
||||
io.Copy(writer, r)
|
||||
wExit <- true
|
||||
}()
|
||||
return
|
||||
}
|
||||
|
||||
// Regex that removes ANSI color escape sequences. Used for outputting to log file and log cache.
|
||||
var stripColors = func() *regexp.Regexp {
|
||||
r, err := regexp.Compile("\\x1b\\[[0-9;]*m")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to compile color escape regexp: %v", err)
|
||||
}
|
||||
return r
|
||||
}()
|
||||
|
||||
type colorStripper struct {
|
||||
file io.Writer
|
||||
}
|
||||
|
||||
func (c colorStripper) Write(p []byte) (n int, err error) {
|
||||
_, err = c.file.Write(stripColors.ReplaceAll(p, []byte("")))
|
||||
n = len(p)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user