mirror of https://github.com/go-gorm/gorm.git
Faster utils.FileWithLineNum (#6981)
* faster FileWithLineNum * tweak caller skip count
This commit is contained in:
parent
d0b4ceb726
commit
bc49365de2
|
@ -32,12 +32,16 @@ func sourceDir(file string) string {
|
||||||
|
|
||||||
// FileWithLineNum return the file name and line number of the current file
|
// FileWithLineNum return the file name and line number of the current file
|
||||||
func FileWithLineNum() string {
|
func FileWithLineNum() string {
|
||||||
// the second caller usually from gorm internal, so set i start from 2
|
pcs := [13]uintptr{}
|
||||||
for i := 2; i < 15; i++ {
|
// the third caller usually from gorm internal
|
||||||
_, file, line, ok := runtime.Caller(i)
|
len := runtime.Callers(3, pcs[:])
|
||||||
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) &&
|
frames := runtime.CallersFrames(pcs[:len])
|
||||||
!strings.HasSuffix(file, ".gen.go") {
|
for i := 0; i < len; i++ {
|
||||||
return file + ":" + strconv.FormatInt(int64(line), 10)
|
// second return value is "more", not "ok"
|
||||||
|
frame, _ := frames.Next()
|
||||||
|
if (!strings.HasPrefix(frame.File, gormSourceDir) ||
|
||||||
|
strings.HasSuffix(frame.File, "_test.go")) && !strings.HasSuffix(frame.File, ".gen.go") {
|
||||||
|
return string(strconv.AppendInt(append([]byte(frame.File), ':'), int64(frame.Line), 10))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue