SimpleCloudNotifier/scnserver/test/util/log.go

48 lines
921 B
Go
Raw Permalink Normal View History

2022-12-09 00:40:50 +01:00
package util
import (
2022-12-20 13:55:09 +01:00
"blackforestbytes.com/simplecloudnotifier/api/ginext"
2022-12-09 00:40:50 +01:00
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
var buflogger *BufferWriter = nil
func SetBufLogger() {
buflogger = &BufferWriter{cw: createConsoleWriter()}
log.Logger = createLogger(buflogger)
gin.SetMode(gin.ReleaseMode)
ginext.SuppressGinLogs = true
}
func ClearBufLogger(dump bool) {
size := len(buflogger.buffer)
if dump {
buflogger.Dump()
}
log.Logger = createLogger(createConsoleWriter())
buflogger = nil
gin.SetMode(gin.TestMode)
ginext.SuppressGinLogs = false
if !dump {
log.Info().Msgf("Suppressed %d logmessages / printf-statements", size)
}
}
func TPrintf(format string, a ...any) {
if buflogger != nil {
buflogger.Printf(format, a...)
} else {
fmt.Printf(format, a...)
}
}
func TPrintln(a ...any) {
if buflogger != nil {
buflogger.Println(a...)
} else {
fmt.Println(a...)
}
}