-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog2.go
145 lines (120 loc) · 2.78 KB
/
log2.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"os"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
/*
func main() {
info2 = true
info2.Println("<abcdefg>")
os.Stdout.Sync()
// time.Sleep(2 * time.Second)
info2.Println("<abcdefg>")
// time.Sleep(2 * time.Second)
info2.Println("<abcdefg>")
// time.Sleep(2 * time.Second)
info2.Println("<abcdefg>c")
info2.Println("<abcdefg>")
// time.Sleep(2 * time.Second)
info2.Println("<abcdefg>")
// time.Sleep(2 * time.Second)
fmt.Println()
}
*/
// TODO 最后一条日志的重复归并,
// 即如果连续出现相同的日志,则不再输出更多相同的行,而是对重复的行进行计数修改。
// 这种功能在emacs的*Message*中看到过。
type infoLogger bool
type debugLogger bool
type errorLogger bool
type requestLogger bool
type responseLogger bool
var (
info2 infoLogger
)
func (d infoLogger) Println(args ...interface{}) {
if d {
alog := fmt.Sprint(args...)
newlog, _needbr := foldLog(alog)
if _needbr {
fmt.Print("\n")
}
fmt.Print(newlog)
}
}
// log folder
var _lastlog string
// 折叠相邻并且相同的log
// folder to format: file.go:20 xxx +123
// 相同log按照file:no log计算
func foldLog(alog string) (newlog string, _needbr bool) {
_needbr = false
fileline := getFileLine2()
alog = fileline + " " + alog
exp := `\+(\d)`
_trim := func(s string) string {
return strings.Trim(s, " \t\n")
}
same := func(lastlog, curlog string) bool {
if lastlog == curlog {
return true
}
if strings.HasPrefix(lastlog, curlog) {
suffix := lastlog[len(curlog):]
if ok, err := regexp.MatchString(exp, _trim(suffix)); ok && err == nil {
return true
}
}
return false
}
bsfix := ""
if len(alog) > 0 && len(_lastlog) > 0 && same(_lastlog, alog) {
if _lastlog == alog {
bsfix = strings.Repeat("\b", len(alog))
newlog = alog + " +1"
} else {
suffix := _lastlog[len(alog):]
ok, err := regexp.MatchString(exp, _trim(suffix))
if err != nil {
}
if ok {
cexp, err := regexp.Compile(exp)
if err != nil {
}
mats := cexp.FindAllStringSubmatch(suffix, -1)
times, err := strconv.Atoi(mats[0][1])
bsfix = strings.Repeat("\b", len(_lastlog))
newlog = alog + " +" + strconv.Itoa(times+1)
} else {
newlog = alog
}
}
} else {
if len(_lastlog) > 0 {
_needbr = true
}
newlog = alog
}
_lastlog = newlog
tfmt := "2006/01/02 15:04:05 "
now := time.Now().Format(tfmt)
level := "[DEBUG] "
newlog = strings.Repeat("\b", len(tfmt)+len(level)) +
bsfix + level + now + newlog
return
}
func getFileLine2() string {
pc, file, line, ok := runtime.Caller(2)
if !ok {
file = "unknown"
}
fn := runtime.FuncForPC(pc)
pos := strings.LastIndexByte(file, os.PathSeparator)
sfile := file[pos+1:]
return sfile + ":" + strconv.Itoa(line) + " " + fn.Name()
}