Include filename with linenum when show error message for easy debugging

This commit is contained in:
Jinzhu 2013-12-23 17:19:15 +08:00
parent 2d52d6d51b
commit b3d2f62f40
1 changed files with 15 additions and 1 deletions

View File

@ -2,6 +2,10 @@ package gorm
import ( import (
"fmt" "fmt"
"os"
"regexp"
"runtime"
"strings"
"time" "time"
) )
@ -31,12 +35,22 @@ func (s *DB) do(data interface{}) *Do {
return &do return &do
} }
func (s *DB) fileWithLineNum() string {
for i := 5; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
if ok && (!regexp.MustCompile(`jinzhu/gorm/.*.go`).MatchString(file) || regexp.MustCompile(`jinzhu/gorm/.*test.go`).MatchString(file)) {
return fmt.Sprintf("%v:%v", strings.TrimPrefix(file, os.Getenv("GOPATH")+"src/"), line)
}
}
return ""
}
func (s *DB) err(err error) error { func (s *DB) err(err error) error {
if err != nil { if err != nil {
s.Error = err s.Error = err
if s.logMode == 0 { if s.logMode == 0 {
if err != RecordNotFound { if err != RecordNotFound {
go fmt.Println(err) go fmt.Println(s.fileWithLineNum(), err)
} }
} else { } else {
s.warn(err) s.warn(err)