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.
This commit is contained in:
tidwall 2021-12-28 16:33:46 -07:00
parent 5f41a687bf
commit 517bc57e9c
2 changed files with 27 additions and 61 deletions

View File

@ -432,10 +432,10 @@ Developer Options:
core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, saddr, os.Getpid()) core.Version, gitsha, strconv.IntSize, runtime.GOARCH, runtime.GOOS, hostd, saddr, os.Getpid())
} else { } else {
fmt.Fprintf(logw, ` fmt.Fprintf(logw, `
_____ _ _ ___ ___ _____ _ _ ___ ___
|_ _|_| |___|_ | . | Tile38 %s%s %d bit (%s/%s) |_ _|_| |___|_ | . | Tile38 %s%s %d bit (%s/%s)
| | | | | -_|_ | . | %s%s, PID: %d | | | | | -_|_ | . | %s%s, PID: %d
|_| |_|_|___|___|___| tile38.com |_| |_|_|___|___|___| tile38.com
Please consider sponsoring Tile38 development, especially if your company Please consider sponsoring Tile38 development, especially if your company
benefits from this software. Visit tile38.com/sponsor today to learn more. benefits from this software. Visit tile38.com/sponsor today to learn more.

View File

@ -93,6 +93,27 @@ func log(level int, tag, color string, formatted bool, format string, args ...in
if Level < level { if Level < level {
return 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 := []byte(time.Now().Format("2006/01/02 15:04:05"))
s = append(s, ' ') s = append(s, ' ')
if tty { 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, "\x1b[0m"...)
} }
s = append(s, ' ') s = append(s, ' ')
if formatted { s = append(s, msg...)
s = append(s, fmt.Sprintf(format, args...)...)
} else {
s = append(s, fmt.Sprint(args...)...)
}
if s[len(s)-1] != '\n' { if s[len(s)-1] != '\n' {
s = append(s, '\n') s = append(s, '\n')
} }
@ -123,10 +140,6 @@ var emptyFormat string
// Infof ... // Infof ...
func Infof(format string, args ...interface{}) { func Infof(format string, args ...interface{}) {
if Level >= 1 { if Level >= 1 {
if LogJSON {
logger.Infof(format, args...)
return
}
log(1, "INFO", "\x1b[36m", true, format, args...) log(1, "INFO", "\x1b[36m", true, format, args...)
} }
} }
@ -134,10 +147,6 @@ func Infof(format string, args ...interface{}) {
// Info ... // Info ...
func Info(args ...interface{}) { func Info(args ...interface{}) {
if Level >= 1 { if Level >= 1 {
if LogJSON {
logger.Info(args...)
return
}
log(1, "INFO", "\x1b[36m", false, emptyFormat, args...) log(1, "INFO", "\x1b[36m", false, emptyFormat, args...)
} }
} }
@ -145,10 +154,6 @@ func Info(args ...interface{}) {
// HTTPf ... // HTTPf ...
func HTTPf(format string, args ...interface{}) { func HTTPf(format string, args ...interface{}) {
if Level >= 1 { if Level >= 1 {
if LogJSON {
logger.Infof(format, args...)
return
}
log(1, "HTTP", "\x1b[1m\x1b[30m", true, format, args...) log(1, "HTTP", "\x1b[1m\x1b[30m", true, format, args...)
} }
} }
@ -156,10 +161,6 @@ func HTTPf(format string, args ...interface{}) {
// HTTP ... // HTTP ...
func HTTP(args ...interface{}) { func HTTP(args ...interface{}) {
if Level >= 1 { if Level >= 1 {
if LogJSON {
logger.Info(args...)
return
}
log(1, "HTTP", "\x1b[1m\x1b[30m", false, emptyFormat, args...) log(1, "HTTP", "\x1b[1m\x1b[30m", false, emptyFormat, args...)
} }
} }
@ -167,10 +168,6 @@ func HTTP(args ...interface{}) {
// Errorf ... // Errorf ...
func Errorf(format string, args ...interface{}) { func Errorf(format string, args ...interface{}) {
if Level >= 1 { if Level >= 1 {
if LogJSON {
logger.Errorf(format, args...)
return
}
log(1, "ERRO", "\x1b[1m\x1b[31m", true, format, args...) log(1, "ERRO", "\x1b[1m\x1b[31m", true, format, args...)
} }
} }
@ -178,10 +175,6 @@ func Errorf(format string, args ...interface{}) {
// Error .. // Error ..
func Error(args ...interface{}) { func Error(args ...interface{}) {
if Level >= 1 { if Level >= 1 {
if LogJSON {
logger.Errorf(emptyFormat, args...)
return
}
log(1, "ERRO", "\x1b[1m\x1b[31m", false, emptyFormat, args...) log(1, "ERRO", "\x1b[1m\x1b[31m", false, emptyFormat, args...)
} }
} }
@ -189,10 +182,6 @@ func Error(args ...interface{}) {
// Warnf ... // Warnf ...
func Warnf(format string, args ...interface{}) { func Warnf(format string, args ...interface{}) {
if Level >= 1 { if Level >= 1 {
if LogJSON {
logger.Warnf(format, args...)
return
}
log(2, "WARN", "\x1b[33m", true, format, args...) log(2, "WARN", "\x1b[33m", true, format, args...)
} }
} }
@ -200,10 +189,6 @@ func Warnf(format string, args ...interface{}) {
// Warn ... // Warn ...
func Warn(args ...interface{}) { func Warn(args ...interface{}) {
if Level >= 1 { if Level >= 1 {
if LogJSON {
logger.Warnf(emptyFormat, args...)
return
}
log(2, "WARN", "\x1b[33m", false, emptyFormat, args...) log(2, "WARN", "\x1b[33m", false, emptyFormat, args...)
} }
} }
@ -211,10 +196,6 @@ func Warn(args ...interface{}) {
// Debugf ... // Debugf ...
func Debugf(format string, args ...interface{}) { func Debugf(format string, args ...interface{}) {
if Level >= 3 { if Level >= 3 {
if LogJSON {
logger.Debugf(format, args...)
return
}
log(3, "DEBU", "\x1b[35m", true, format, args...) log(3, "DEBU", "\x1b[35m", true, format, args...)
} }
} }
@ -222,10 +203,6 @@ func Debugf(format string, args ...interface{}) {
// Debug ... // Debug ...
func Debug(args ...interface{}) { func Debug(args ...interface{}) {
if Level >= 3 { if Level >= 3 {
if LogJSON {
logger.Debugf(emptyFormat, args...)
return
}
log(3, "DEBU", "\x1b[35m", false, emptyFormat, args...) log(3, "DEBU", "\x1b[35m", false, emptyFormat, args...)
} }
} }
@ -242,23 +219,12 @@ func Print(args ...interface{}) {
// Fatalf ... // Fatalf ...
func Fatalf(format string, args ...interface{}) { func Fatalf(format string, args ...interface{}) {
if LogJSON {
logger.Fatalf(format, args...)
os.Exit(1)
return
}
log(1, "FATA", "\x1b[31m", true, format, args...) log(1, "FATA", "\x1b[31m", true, format, args...)
os.Exit(1) os.Exit(1)
} }
// Fatal ... // Fatal ...
func Fatal(args ...interface{}) { func Fatal(args ...interface{}) {
if LogJSON {
logger.Fatalf(emptyFormat, args...)
os.Exit(1)
return
}
log(1, "FATA", "\x1b[31m", false, emptyFormat, args...) log(1, "FATA", "\x1b[31m", false, emptyFormat, args...)
os.Exit(1) os.Exit(1)
} }