-
Notifications
You must be signed in to change notification settings - Fork 28
/
logger.go
222 lines (177 loc) · 5.28 KB
/
logger.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
"time"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
)
const (
typeRequest = 1 << iota
typeRequestResponse = 1 << iota
typeRequestResponseError = 1 << iota
typeEvent = 1 << iota
)
const (
levelConnection = 1 << iota
levelProtocol = 1 << iota
levelTarget = 1 << iota
)
const (
fieldLevel = "level"
fieldType = "type"
fieldTargetID = "targetID"
fieldRequest = "request"
fieldMethod = "method"
fieldInspectorID = "inspectorId"
)
const (
requestReplyFormat = "%-17s %-32s % 48s(%s) = %s\n"
requestFormat = "%-17s %-32s % 48s(%s)\n"
eventFormat = "%-17s %-32s % 48s(%s)\n"
protocolFormat = "%-17s %-32s\n"
timeFormat = "15:04:05.00000000"
deltaFormat = "Δ%8.2fms"
)
var (
responseColor = color.New(color.FgHiRed).SprintfFunc()
requestColor = color.New(color.FgHiBlue).SprintFunc()
requestReplyColor = color.New(color.FgHiWhite).SprintfFunc()
eventsColor = color.New(color.FgGreen).SprintfFunc()
eventsLabelColor = color.New(color.FgCyan).SprintfFunc()
protocolColor = color.New(color.FgYellow).SprintfFunc()
protocolError = color.New(color.FgHiYellow, color.BgRed).SprintfFunc()
targetColor = color.New(color.FgHiWhite).SprintfFunc()
methodColor = color.New(color.FgHiYellow).SprintfFunc()
errorColor = color.New(color.BgRed, color.FgWhite).SprintfFunc()
protocolTargetID = center("browser", 32)
)
type FramesFormatter struct {
lastTime int64
}
func (f *FramesFormatter) Format(e *logrus.Entry) ([]byte, error) {
message := e.Message
var timestamp string
if *flagMicroseconds {
timestamp = fmt.Sprintf("%d", e.Time.UnixNano()/int64(time.Millisecond))
} else {
timestamp = e.Time.Format(timeFormat)
}
if *flagDelta {
var delta string
if f.lastTime == 0 {
delta = fmt.Sprintf(deltaFormat, 0.00)
} else {
delta = fmt.Sprintf(deltaFormat, math.Abs(float64(e.Time.UnixNano()-f.lastTime)/float64(time.Millisecond)))
}
f.lastTime = e.Time.UnixNano()
timestamp = fmt.Sprintf("%s %s", timestamp, delta)
}
var protocolType = -1
var protocolMethod = ""
protocolLevel := e.Data[fieldLevel].(int)
if val, ok := e.Data[fieldType].(int); ok {
protocolType = val
}
if val, ok := e.Data[fieldMethod].(string); ok {
protocolMethod = val
}
if !accept(protocolMethod, message) {
return []byte{}, nil
}
switch protocolLevel {
case levelConnection:
switch e.Level {
case logrus.ErrorLevel:
return []byte(fmt.Sprintf(protocolFormat, timestamp, errorColor(message))), nil
case logrus.InfoLevel:
return []byte(fmt.Sprintf(protocolFormat, timestamp, protocolColor(message))), nil
}
case levelProtocol, levelTarget:
targetID := e.Data[fieldTargetID].(string)
switch protocolType {
case typeEvent:
return []byte(fmt.Sprintf(eventFormat, timestamp, targetColor(targetID), eventsLabelColor(protocolMethod), eventsColor(message))), nil
case typeRequest:
return []byte(fmt.Sprintf(requestFormat, timestamp, targetColor(targetID), methodColor(protocolMethod), requestColor(message))), nil
case typeRequestResponse:
return []byte(fmt.Sprintf(requestReplyFormat, timestamp, targetColor(targetID), methodColor(protocolMethod), requestReplyColor(e.Data[fieldRequest].(string)), responseColor(message))), nil
case typeRequestResponseError:
return []byte(fmt.Sprintf(requestReplyFormat, timestamp, targetColor(targetID), methodColor(protocolMethod), requestReplyColor(e.Data[fieldRequest].(string)), errorColor(message))), nil
}
}
return []byte(fmt.Sprintf("unsupported entry: %+v", e)), nil
}
type multiWriter struct {
io.Writer
writers []io.Writer
}
func newMultiWriter(writers ...io.Writer) *multiWriter {
return &multiWriter{
Writer: io.MultiWriter(writers...),
writers: writers,
}
}
func (m *multiWriter) Close() (err error) {
for _, writer := range m.writers {
if v, ok := writer.(io.Closer); ok && v != os.Stdout {
v.Close()
}
}
return nil
}
var loggers = make(map[string]*logrus.Logger)
func createLogWriter(filename string) (io.Writer, error) {
if filename == "" {
if *flagQuiet {
return ioutil.Discard, nil
}
return os.Stdout, nil
}
logFilePath := fmt.Sprintf(*flagDirLogs+"/%s.log", filename)
dir := filepath.Dir(logFilePath)
if _, err := os.Stat(dir); err != nil {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return nil, err
}
}
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
return nil, err
}
if *flagQuiet {
return newMultiWriter(logFile), nil
}
return newMultiWriter(logFile, os.Stdout), nil
}
func createLogger(name string) (*logrus.Logger, error) {
if *flagForceColor {
color.NoColor = false
}
if _, exists := loggers[name]; !exists {
writer, err := createLogWriter(name)
if err != nil {
return nil, err
}
loggers[name] = &logrus.Logger{
Out: writer,
Formatter: new(FramesFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.DebugLevel,
}
}
return loggers[name], nil
}
func destroyLogger(name string) error {
if logger, exists := loggers[name]; exists {
if closer, ok := logger.Out.(io.Closer); ok {
closer.Close()
}
delete(loggers, name)
}
return nil
}