From 9934207c42df1d2e587f0523b8cefefe17212b30 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 3 Jun 2020 14:39:36 +0800 Subject: [PATCH] Fix logger panic on windows --- utils/utils.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/utils/utils.go b/utils/utils.go index e177999e..ce42b218 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -5,25 +5,24 @@ import ( "fmt" "path/filepath" "reflect" - "regexp" "runtime" "strconv" "strings" "unicode" ) -var goSrcRegexp, goTestRegexp *regexp.Regexp +var gormSourceDir string func init() { _, file, _, _ := runtime.Caller(0) - goSrcRegexp = regexp.MustCompile(filepath.Join(filepath.Dir(filepath.Dir(file)), ".*.go")) - goTestRegexp = regexp.MustCompile(filepath.Join(filepath.Dir(filepath.Dir(file)), ".*test.go")) + gormSourceDir = filepath.Dir(filepath.Dir(file)) } func FileWithLineNum() string { for i := 2; i < 15; i++ { _, file, line, ok := runtime.Caller(i) - if ok && (!goSrcRegexp.MatchString(file) || goTestRegexp.MatchString(file)) { + + if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) { return fmt.Sprintf("%v:%v", file, line) } }