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...)
// FIXME: Potential cause if last log line doesn't get printed sometimes.
os.Stdout, os.Stderr = w, w
log.SetOutput(writer)
gin.DefaultWriter, gin.DefaultErrorWriter = writer, writer

View File

@ -11,16 +11,17 @@ import (
c "github.com/fatih/color"
)
type Logger interface {
Printf(format string, v ...interface{})
Print(v ...interface{})
Println(v ...interface{})
Fatal(v ...interface{})
Fatalf(format string, v ...interface{})
SetFatalFunc(f func(err interface{}))
}
// type Logger interface {
// Printf(format string, v ...interface{})
// Print(v ...interface{})
// Println(v ...interface{})
// Fatal(v ...interface{})
// Fatalf(format string, v ...interface{})
// SetFatalFunc(f func(err interface{}))
// }
type logger struct {
type Logger struct {
empty bool
logger *log.Logger
shortfile bool
printer *c.Color
@ -46,7 +47,8 @@ func Lshortfile() string {
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
if flag&log.Lshortfile != 0 {
flag -= log.Lshortfile
@ -58,7 +60,12 @@ func NewLogger(out io.Writer, prefix string, flag int, color c.Attribute) (l log
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
if l.shortfile {
out = Lshortfile()
@ -67,7 +74,10 @@ func (l logger) Printf(format string, v ...interface{}) {
l.logger.Print(out)
}
func (l logger) Print(v ...interface{}) {
func (l *Logger) Print(v ...interface{}) {
if l.empty {
return
}
var out string
if l.shortfile {
out = Lshortfile()
@ -76,7 +86,10 @@ func (l logger) Print(v ...interface{}) {
l.logger.Print(out)
}
func (l logger) Println(v ...interface{}) {
func (l *Logger) Println(v ...interface{}) {
if l.empty {
return
}
var out string
if l.shortfile {
out = Lshortfile()
@ -85,7 +98,10 @@ func (l logger) Println(v ...interface{}) {
l.logger.Print(out)
}
func (l logger) Fatal(v ...interface{}) {
func (l *Logger) Fatal(v ...interface{}) {
if l.empty {
return
}
var out string
if l.shortfile {
out = Lshortfile()
@ -94,29 +110,22 @@ func (l logger) Fatal(v ...interface{}) {
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
if l.shortfile {
out = Lshortfile()
}
out += " " + l.printer.Sprintf(format, v...)
if l.fatalFunc != nil {
l.logger.Print(out)
l.fatalFunc(errors.New(out))
} else {
l.logger.Fatal(out)
}
}
func (l logger) SetFatalFunc(f func(err interface{})) {
func (l *Logger) SetFatalFunc(f func(err interface{})) {
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
discord *DiscordDaemon
matrix *MatrixDaemon
info, debug, err logger.Logger
info, debug, err *logger.Logger
host string
port int
version string
@ -234,7 +234,7 @@ func start(asDaemon, firstCall bool) {
if debugMode {
app.debug = logger.NewLogger(os.Stdout, "[DEBUG] ", log.Ltime|log.Lshortfile, color.FgYellow)
} else {
app.debug = logger.EmptyLogger(false)
app.debug = logger.NewEmptyLogger()
}
if *PPROF {
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",
"typescript": "^4.0.3",
"uncss": "^0.17.3"
},
"devDependencies": {
"live-server": "^1.2.1"
}
}