From ce4aa56b4caae712ad3ab05447dbae20fec10e14 Mon Sep 17 00:00:00 2001 From: "zhengyang.zhu" Date: Mon, 4 Mar 2019 14:58:41 +0800 Subject: [PATCH] fix caller for https://golang.org/doc/go1.12\#compiler --- common_context.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/common_context.go b/common_context.go index 230a76c..a3ea037 100644 --- a/common_context.go +++ b/common_context.go @@ -81,7 +81,9 @@ func extractCallerInfo(skip int) (*logContext, error) { if runtime.Callers(skip+1, stack[:]) != 1 { return nil, errors.New("error during runtime.Callers") } - pc := stack[0] + frames := runtime.CallersFrames(stack[:1]) + frame, _ := frames.Next() + pc := frame.PC // do we have a cache entry? stackCacheLock.RLock() @@ -91,20 +93,14 @@ func extractCallerInfo(skip int) (*logContext, error) { return ctx, nil } - // look up the details of the given caller - funcInfo := runtime.FuncForPC(pc) - if funcInfo == nil { - return nil, errors.New("error during runtime.FuncForPC") - } - var shortPath string - fullPath, line := funcInfo.FileLine(pc) + fullPath, line := frame.File, frame.Line if strings.HasPrefix(fullPath, workingDir) { shortPath = fullPath[len(workingDir):] } else { shortPath = fullPath } - funcName := funcInfo.Name() + funcName := frame.Function if strings.HasPrefix(funcName, workingDir) { funcName = funcName[len(workingDir):] }