-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
39 lines (32 loc) · 907 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package log
import (
"fmt"
"regexp"
"runtime"
"strconv"
"strings"
)
var sourceDir string
func init() {
_, file, _, _ := runtime.Caller(0)
// compatible solution to get gorm source directory with various operating systems
sourceDir = regexp.MustCompile(`log.utils\.go`).ReplaceAllString(file, "")
}
// FileWithLineNum return the file name and line number of the current file
func FileWithLineNum() string {
// 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, sourceDir) || strings.HasSuffix(file, "_test.go")) {
return file + ":" + strconv.FormatInt(int64(line), 10)
}
}
return ""
}
func trimLineBreak(path string) string {
return path[:len(path)-1]
}
func getMessage(fmtArgs []interface{}) string {
msg := fmt.Sprintln(fmtArgs...)
return msg[:len(msg)-1]
}