From 517bc57e9c815bdd46556f06a5be5df126700872 Mon Sep 17 00:00:00 2001 From: tidwall Date: Tue, 28 Dec 2021 16:33:46 -0700 Subject: [PATCH] Ensure inlined logs Move the LogJSON check into the log function so that the caller function can be inlined. This is helpful for hot functions like `log.Debug` where it's likely that the `-vv` flag is not set thus the to avoid the extra function call. --- cmd/tile38-server/main.go | 10 ++--- internal/log/log.go | 78 +++++++++++---------------------------- 2 files changed, 27 insertions(+), 61 deletions(-) diff --git a/cmd/tile38-server/main.go b/cmd/tile38-server/main.go index fe16aa68..f2ea4a30 100644 --- a/cmd/tile38-server/main.go +++ b/cmd/tile38-server/main.go @@ -432,11 +432,11 @@ Developer Options: core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, saddr, os.Getpid()) } else { fmt.Fprintf(logw, ` - _____ _ _ ___ ___ - |_ _|_| |___|_ | . | Tile38 %s%s %d bit (%s/%s) - | | | | | -_|_ | . | %s%s, PID: %d - |_| |_|_|___|___|___| tile38.com - + _____ _ _ ___ ___ + |_ _|_| |___|_ | . | Tile38 %s%s %d bit (%s/%s) + | | | | | -_|_ | . | %s%s, PID: %d + |_| |_|_|___|___|___| tile38.com + Please consider sponsoring Tile38 development, especially if your company benefits from this software. Visit tile38.com/sponsor today to learn more. diff --git a/internal/log/log.go b/internal/log/log.go index bdcfe3bc..4250d84c 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -93,6 +93,27 @@ func log(level int, tag, color string, formatted bool, format string, args ...in if Level < level { return } + var msg string + if formatted { + msg = fmt.Sprintf(format, args...) + } else { + msg = fmt.Sprint(args...) + } + if LogJSON { + switch tag { + case "ERRO": + logger.Error(msg) + case "FATA": + logger.Fatal(msg) + case "WARN": + logger.Warn(msg) + case "DEBU": + logger.Debug(msg) + default: + logger.Info(msg) + } + return + } s := []byte(time.Now().Format("2006/01/02 15:04:05")) s = append(s, ' ') if tty { @@ -105,11 +126,7 @@ func log(level int, tag, color string, formatted bool, format string, args ...in s = append(s, "\x1b[0m"...) } s = append(s, ' ') - if formatted { - s = append(s, fmt.Sprintf(format, args...)...) - } else { - s = append(s, fmt.Sprint(args...)...) - } + s = append(s, msg...) if s[len(s)-1] != '\n' { s = append(s, '\n') } @@ -123,10 +140,6 @@ var emptyFormat string // Infof ... func Infof(format string, args ...interface{}) { if Level >= 1 { - if LogJSON { - logger.Infof(format, args...) - return - } log(1, "INFO", "\x1b[36m", true, format, args...) } } @@ -134,10 +147,6 @@ func Infof(format string, args ...interface{}) { // Info ... func Info(args ...interface{}) { if Level >= 1 { - if LogJSON { - logger.Info(args...) - return - } log(1, "INFO", "\x1b[36m", false, emptyFormat, args...) } } @@ -145,10 +154,6 @@ func Info(args ...interface{}) { // HTTPf ... func HTTPf(format string, args ...interface{}) { if Level >= 1 { - if LogJSON { - logger.Infof(format, args...) - return - } log(1, "HTTP", "\x1b[1m\x1b[30m", true, format, args...) } } @@ -156,10 +161,6 @@ func HTTPf(format string, args ...interface{}) { // HTTP ... func HTTP(args ...interface{}) { if Level >= 1 { - if LogJSON { - logger.Info(args...) - return - } log(1, "HTTP", "\x1b[1m\x1b[30m", false, emptyFormat, args...) } } @@ -167,10 +168,6 @@ func HTTP(args ...interface{}) { // Errorf ... func Errorf(format string, args ...interface{}) { if Level >= 1 { - if LogJSON { - logger.Errorf(format, args...) - return - } log(1, "ERRO", "\x1b[1m\x1b[31m", true, format, args...) } } @@ -178,10 +175,6 @@ func Errorf(format string, args ...interface{}) { // Error .. func Error(args ...interface{}) { if Level >= 1 { - if LogJSON { - logger.Errorf(emptyFormat, args...) - return - } log(1, "ERRO", "\x1b[1m\x1b[31m", false, emptyFormat, args...) } } @@ -189,10 +182,6 @@ func Error(args ...interface{}) { // Warnf ... func Warnf(format string, args ...interface{}) { if Level >= 1 { - if LogJSON { - logger.Warnf(format, args...) - return - } log(2, "WARN", "\x1b[33m", true, format, args...) } } @@ -200,10 +189,6 @@ func Warnf(format string, args ...interface{}) { // Warn ... func Warn(args ...interface{}) { if Level >= 1 { - if LogJSON { - logger.Warnf(emptyFormat, args...) - return - } log(2, "WARN", "\x1b[33m", false, emptyFormat, args...) } } @@ -211,10 +196,6 @@ func Warn(args ...interface{}) { // Debugf ... func Debugf(format string, args ...interface{}) { if Level >= 3 { - if LogJSON { - logger.Debugf(format, args...) - return - } log(3, "DEBU", "\x1b[35m", true, format, args...) } } @@ -222,10 +203,6 @@ func Debugf(format string, args ...interface{}) { // Debug ... func Debug(args ...interface{}) { if Level >= 3 { - if LogJSON { - logger.Debugf(emptyFormat, args...) - return - } log(3, "DEBU", "\x1b[35m", false, emptyFormat, args...) } } @@ -242,23 +219,12 @@ func Print(args ...interface{}) { // Fatalf ... func Fatalf(format string, args ...interface{}) { - if LogJSON { - logger.Fatalf(format, args...) - os.Exit(1) - return - } - log(1, "FATA", "\x1b[31m", true, format, args...) os.Exit(1) } // Fatal ... func Fatal(args ...interface{}) { - if LogJSON { - logger.Fatalf(emptyFormat, args...) - os.Exit(1) - return - } log(1, "FATA", "\x1b[31m", false, emptyFormat, args...) os.Exit(1) }