Skip to content

Commit

Permalink
feat: reduce allocs while getting caller frame count
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-karan committed Jul 7, 2022
1 parent 5e36465 commit edd3c10
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
stdlog "log"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -174,6 +173,8 @@ func (l Logger) handleLog(msg string, lvl Level, fields ...any) {
writeToBuf(buf, "level", lvl, lvl, l.Opts.EnableColor, true)
writeStringToBuf(buf, "message", msg, lvl, l.Opts.EnableColor, true)

if l.Opts.EnableCaller {
writeCallerToBuf(buf, "caller", l.Opts.CallerSkipFrameCount, lvl, l.EnableColor, true)
}

// Format the line as logfmt.
Expand Down Expand Up @@ -245,6 +246,26 @@ func writeStringToBuf(buf *byteBuffer, key string, val string, lvl Level, color,
}
}

func writeCallerToBuf(buf *byteBuffer, key string, depth int, lvl Level, color, space bool) {
_, file, line, ok := runtime.Caller(depth)
if !ok {
file = "???"
line = 0
}
if color {
buf.AppendString(getColoredKey(key, lvl))
} else {
buf.AppendString(key)
}
buf.AppendByte('=')
escapeAndWriteString(buf, file)
buf.AppendByte(':')
buf.AppendInt(int64(line))
if space {
buf.AppendByte(' ')
}
}

// writeToBuf takes key, value and additional options to write to the buffer in logfmt.
func writeToBuf(buf *byteBuffer, key string, val any, lvl Level, color, space bool) {
if color {
Expand Down Expand Up @@ -295,16 +316,6 @@ func getColoredKey(k string, lvl Level) string {
return colorLvlMap[lvl] + k + reset
}

// caller returns the file:line of the caller.
func caller(depth int) string {
_, file, line, ok := runtime.Caller(depth)
if !ok {
file = "???"
line = 0
}
return file + ":" + strconv.Itoa(line)
}

// checkEscapingRune returns true if the rune is to be escaped.
func checkEscapingRune(r rune) bool {
return r == '=' || r == ' ' || r == '"' || r == utf8.RuneError
Expand Down

0 comments on commit edd3c10

Please sign in to comment.