Fix calc gormSourceDir, close #4456

This commit is contained in:
Jinzhu 2021-06-13 10:32:03 +08:00
parent 25b9f2e26a
commit 3226937f68
1 changed files with 5 additions and 5 deletions

View File

@ -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)