Skip to content

Commit

Permalink
fix sync.Once usage instead of adding a mutex lock
Browse files Browse the repository at this point in the history
  • Loading branch information
dgsb committed Mar 6, 2019
1 parent b9d4514 commit cf1b9fd
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,23 @@ func getPackageName(f string) string {

// getCaller retrieves the name of the first non-logrus calling function
func getCaller() *runtime.Frame {
// Restrict the lookback frames to avoid runaway lookups
pcs := make([]uintptr, maximumCallerDepth)
depth := runtime.Callers(minimumCallerDepth, pcs)
frames := runtime.CallersFrames(pcs[:depth])

// cache this package's fully-qualified name
callerInitOnce.Do(func() {
logrusPackage = getPackageName(runtime.FuncForPC(pcs[0]).Name())
pcs := make([]uintptr, 2)
_ = runtime.Callers(0, pcs)
logrusPackage = getPackageName(runtime.FuncForPC(pcs[1]).Name())

// now that we have the cache, we can skip a minimum count of known-logrus functions
// XXX this is dubious, the number of frames may vary store an entry in a logger interface
// XXX this is dubious, the number of frames may vary
minimumCallerDepth = knownLogrusFrames
})

// Restrict the lookback frames to avoid runaway lookups
pcs := make([]uintptr, maximumCallerDepth)
depth := runtime.Callers(minimumCallerDepth, pcs)
frames := runtime.CallersFrames(pcs[:depth])

for f, again := frames.Next(); again; f, again = frames.Next() {
pkg := getPackageName(f.Function)

Expand Down Expand Up @@ -206,9 +209,7 @@ func (entry Entry) log(level Level, msg string) {
entry.Level = level
entry.Message = msg
if entry.Logger.ReportCaller {
entry.Logger.mu.Lock()
entry.Caller = getCaller()
entry.Logger.mu.Unlock()
}

entry.fireHooks()
Expand Down

0 comments on commit cf1b9fd

Please sign in to comment.