forked from sadlil/gologger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinterHandler.go
52 lines (45 loc) · 1 KB
/
printerHandler.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
52
package gologger
import (
"path"
"runtime"
"strings"
"time"
"os"
)
func logPrinter(log LogInstance) {
info := retrieveCallInfo()
timer := time.Now()
logPrint(log, info, timer)
}
func logPrint(log LogInstance, info *callerInfo, time time.Time) {
Print(log, info.packageName, info.fileName, info.line, info.funcName, time)
if log.LogType == "CRT" {
os.Exit(1)
}
}
type callerInfo struct {
packageName string
fileName string
funcName string
line int
}
func retrieveCallInfo() *callerInfo {
pc, file, line, _ := runtime.Caller(3)
_, fileName := path.Split(file)
parts := strings.Split(runtime.FuncForPC(pc).Name(), ".")
pl := len(parts)
packageName := ""
funcName := parts[pl-1]
if parts[pl-2][0] == '(' {
funcName = parts[pl-2] + "." + funcName
packageName = strings.Join(parts[0:pl-2], ".")
} else {
packageName = strings.Join(parts[0:pl-1], ".")
}
return &callerInfo{
packageName: packageName,
fileName: fileName,
funcName: funcName,
line: line,
}
}