From b3d2f62f40b82dc6d94f185a060053bcb7ae3701 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 23 Dec 2013 17:19:15 +0800 Subject: [PATCH] Include filename with linenum when show error message for easy debugging --- private.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/private.go b/private.go index f87d4cc8..58fcae90 100644 --- a/private.go +++ b/private.go @@ -2,6 +2,10 @@ package gorm import ( "fmt" + "os" + "regexp" + "runtime" + "strings" "time" ) @@ -31,12 +35,22 @@ func (s *DB) do(data interface{}) *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 { if err != nil { s.Error = err if s.logMode == 0 { if err != RecordNotFound { - go fmt.Println(err) + go fmt.Println(s.fileWithLineNum(), err) } } else { s.warn(err)