Fix calc gormSourceDir, close #4456
This commit is contained in:
parent
25b9f2e26a
commit
3226937f68
@ -3,8 +3,8 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -15,14 +15,14 @@ var gormSourceDir string
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
_, file, _, _ := runtime.Caller(0)
|
_, file, _, _ := runtime.Caller(0)
|
||||||
// Here is the directory to get the gorm source code. Here, the filepath.Dir mode is enough,
|
// compatible solution to get gorm source directory with various operating systems
|
||||||
// and the filepath is compatible with various operating systems
|
gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "")
|
||||||
gormSourceDir = filepath.Dir(filepath.Dir(file))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
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)
|
_, file, line, ok := runtime.Caller(i)
|
||||||
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
|
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
|
||||||
return file + ":" + strconv.FormatInt(int64(line), 10)
|
return file + ":" + strconv.FormatInt(int64(line), 10)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user