-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_type.go
51 lines (44 loc) · 1.04 KB
/
file_type.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
40
41
42
43
44
45
46
47
48
49
50
51
package lager
import (
"fmt"
"runtime"
"strings"
)
// FileType represents values for including file information of logs
type FileType uint8
const (
// NoFile does not include the file name or line number of the log
NoFile FileType = iota
// ShortFile includes the file name and line number of the log
ShortFile
// PackageFile includes the file name, including package, and line number of the log
PackageFile
// FullFile includes the absolute file path and line number of the log
FullFile
)
// Caller returns the appropriate filename and line number of the file type
func (ft FileType) Caller(calldepth int) string {
if ft == NoFile {
return ""
}
_, file, line, ok := runtime.Caller(calldepth)
if !ok {
return ""
}
if ft == PackageFile {
paths := strings.Split(file, "src/")
if len(paths) == 2 {
file = paths[1]
}
} else if ft == ShortFile {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
}
return fmt.Sprintf("%s:%d", file, line)
}