1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

fix missing last log line

Sometimes calls to app.err.Fatalf would fail to print the error to the
console, and fail to show "A crash report has been saves to...". Both of
these should be fixed now.
This commit is contained in:
Harvey Tindall 2021-09-18 13:43:11 +01:00
parent 548dceda28
commit fbe3553b25
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
5 changed files with 4599 additions and 36 deletions

1
log.go
View File

@ -39,6 +39,7 @@ func logOutput() (closeFunc func()) {
} }
} }
writer := io.MultiWriter(writers...) writer := io.MultiWriter(writers...)
// FIXME: Potential cause if last log line doesn't get printed sometimes.
os.Stdout, os.Stderr = w, w os.Stdout, os.Stderr = w, w
log.SetOutput(writer) log.SetOutput(writer)
gin.DefaultWriter, gin.DefaultErrorWriter = writer, writer gin.DefaultWriter, gin.DefaultErrorWriter = writer, writer

View File

@ -11,16 +11,17 @@ import (
c "github.com/fatih/color" c "github.com/fatih/color"
) )
type Logger interface { // type Logger interface {
Printf(format string, v ...interface{}) // Printf(format string, v ...interface{})
Print(v ...interface{}) // Print(v ...interface{})
Println(v ...interface{}) // Println(v ...interface{})
Fatal(v ...interface{}) // Fatal(v ...interface{})
Fatalf(format string, v ...interface{}) // Fatalf(format string, v ...interface{})
SetFatalFunc(f func(err interface{})) // SetFatalFunc(f func(err interface{}))
} // }
type logger struct { type Logger struct {
empty bool
logger *log.Logger logger *log.Logger
shortfile bool shortfile bool
printer *c.Color printer *c.Color
@ -46,7 +47,8 @@ func Lshortfile() string {
return file + ":" + lineString + ":" return file + ":" + lineString + ":"
} }
func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l logger) { func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l *Logger) {
l = &Logger{}
// Use reimplemented Lshortfile since wrapping the log functions messes them up // Use reimplemented Lshortfile since wrapping the log functions messes them up
if flag&log.Lshortfile != 0 { if flag&log.Lshortfile != 0 {
flag -= log.Lshortfile flag -= log.Lshortfile
@ -58,7 +60,12 @@ func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l log
return l return l
} }
func (l logger) Printf(format string, v ...interface{}) { func NewEmptyLogger() (l *Logger) { l.empty = true; return }
func (l *Logger) Printf(format string, v ...interface{}) {
if l.empty {
return
}
var out string var out string
if l.shortfile { if l.shortfile {
out = Lshortfile() out = Lshortfile()
@ -67,7 +74,10 @@ func (l logger) Printf(format string, v ...interface{}) {
l.logger.Print(out) l.logger.Print(out)
} }
func (l logger) Print(v ...interface{}) { func (l *Logger) Print(v ...interface{}) {
if l.empty {
return
}
var out string var out string
if l.shortfile { if l.shortfile {
out = Lshortfile() out = Lshortfile()
@ -76,7 +86,10 @@ func (l logger) Print(v ...interface{}) {
l.logger.Print(out) l.logger.Print(out)
} }
func (l logger) Println(v ...interface{}) { func (l *Logger) Println(v ...interface{}) {
if l.empty {
return
}
var out string var out string
if l.shortfile { if l.shortfile {
out = Lshortfile() out = Lshortfile()
@ -85,7 +98,10 @@ func (l logger) Println(v ...interface{}) {
l.logger.Print(out) l.logger.Print(out)
} }
func (l logger) Fatal(v ...interface{}) { func (l *Logger) Fatal(v ...interface{}) {
if l.empty {
return
}
var out string var out string
if l.shortfile { if l.shortfile {
out = Lshortfile() out = Lshortfile()
@ -94,29 +110,22 @@ func (l logger) Fatal(v ...interface{}) {
l.logger.Fatal(out) l.logger.Fatal(out)
} }
func (l logger) Fatalf(format string, v ...interface{}) { func (l *Logger) Fatalf(format string, v ...interface{}) {
if l.empty {
return
}
var out string var out string
if l.shortfile { if l.shortfile {
out = Lshortfile() out = Lshortfile()
} }
out += " " + l.printer.Sprintf(format, v...) out += " " + l.printer.Sprintf(format, v...)
if l.fatalFunc != nil { if l.fatalFunc != nil {
l.logger.Print(out)
l.fatalFunc(errors.New(out)) l.fatalFunc(errors.New(out))
} else { } else {
l.logger.Fatal(out) l.logger.Fatal(out)
} }
} }
func (l logger) SetFatalFunc(f func(err interface{})) { func (l *Logger) SetFatalFunc(f func(err interface{})) {
l.fatalFunc = f l.fatalFunc = f
} }
type EmptyLogger bool
func (l EmptyLogger) Printf(format string, v ...interface{}) {}
func (l EmptyLogger) Print(v ...interface{}) {}
func (l EmptyLogger) Println(v ...interface{}) {}
func (l EmptyLogger) Fatal(v ...interface{}) {}
func (l EmptyLogger) Fatalf(format string, v ...interface{}) {}
func (l EmptyLogger) SetFatalFunc(f func(err interface{})) {}

View File

@ -98,7 +98,7 @@ type appContext struct {
telegram *TelegramDaemon telegram *TelegramDaemon
discord *DiscordDaemon discord *DiscordDaemon
matrix *MatrixDaemon matrix *MatrixDaemon
info, debug, err logger.Logger info, debug, err *logger.Logger
host string host string
port int port int
version string version string
@ -234,7 +234,7 @@ func start(asDaemon, firstCall bool) {
if debugMode { if debugMode {
app.debug = logger.NewLogger(os.Stdout, "[DEBUG] ", log.Ltime|log.Lshortfile, color.FgYellow) app.debug = logger.NewLogger(os.Stdout, "[DEBUG] ", log.Ltime|log.Lshortfile, color.FgYellow)
} else { } else {
app.debug = logger.EmptyLogger(false) app.debug = logger.NewEmptyLogger()
} }
if *PPROF { if *PPROF {
app.info.Print(warning("\n\nWARNING: Don't use pprof in production.\n\n")) app.info.Print(warning("\n\nWARNING: Don't use pprof in production.\n\n"))

4566
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -29,5 +29,8 @@
"remove-markdown": "^0.3.0", "remove-markdown": "^0.3.0",
"typescript": "^4.0.3", "typescript": "^4.0.3",
"uncss": "^0.17.3" "uncss": "^0.17.3"
},
"devDependencies": {
"live-server": "^1.2.1"
} }
} }