diff --git a/utils/utils.go b/utils/utils.go index 3261138f..1110c7a7 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,8 +3,8 @@ package utils import ( "database/sql/driver" "fmt" - "path/filepath" "reflect" + "regexp" "runtime" "strconv" "strings" @@ -15,14 +15,14 @@ var gormSourceDir string func init() { _, file, _, _ := runtime.Caller(0) - // Here is the directory to get the gorm source code. Here, the filepath.Dir mode is enough, - // and the filepath is compatible with various operating systems - gormSourceDir = filepath.Dir(filepath.Dir(file)) + // compatible solution to get gorm source directory with various operating systems + gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "") } // FileWithLineNum return the file name and line number of the current file func FileWithLineNum() string { - for i := 1; i < 15; i++ { + // the second caller usually from gorm internal, so set i start from 2 + for i := 2; i < 15; i++ { _, file, line, ok := runtime.Caller(i) if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) { return file + ":" + strconv.FormatInt(int64(line), 10)