forked from visionmedia/go-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.go
166 lines (141 loc) · 3.38 KB
/
formatter.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
package debug
import (
"fmt"
"sort"
)
const (
FieldKeyMsg = "msg"
FieldKeyNamespace = "namespace"
FieldKeyTime = "time"
FieldKeyDelta = "delta"
FieldKeyError = "debug_error"
)
// highly inspired by logrus
type Formatter interface {
Format(Debugger, interface{}) string
GetHasFieldsOnly() bool
}
type TextFormatter struct {
HasColor bool
ForceQuote bool
QuoteEmptyFields bool
DisableQuote bool
HasFieldsOnly bool
SortingFunc func(keys []string)
}
func (t *TextFormatter) Format(dbg Debugger, _msg interface{}) string {
msg, didCast := _msg.(string)
if !didCast {
msg = fmt.Sprintf("%+v", _msg)
}
mainMsg := ""
fields := ""
var keys []string
finalized := finalizeFields(dbg, msg, HAS_COLORS && t.HasColor, false, func(k string, v interface{}) interface{} {
keys = append(keys, k)
return nil
})
if t.SortingFunc == nil {
sort.Strings(keys)
} else {
t.SortingFunc(keys)
}
// build fields string in specified order
for _, k := range keys {
v := finalized.Fields[k]
fields = t.appendKeyValue(fields, k, v)
}
if !t.GetHasFieldsOnly() {
mainMsg = BasicFormat(finalized.TimeString, finalized.Delta, finalized.Namespace, msg)
if fields != "" {
fields = " " + fields
}
}
if fields != "" {
fields += "\n"
}
return mainMsg + fields
}
func (t *TextFormatter) GetHasFieldsOnly() bool {
return t.HasFieldsOnly
}
func BasicFormat(ts string, delta string, ns string, msg string) string {
time := getTime(ts, delta, HAS_TIME)
head := time + ns
if head != "" {
head += " - "
}
return head + msg + "\n"
}
func (f *TextFormatter) appendKeyValue(s string, key string, value interface{}) string {
if len(s) > 0 {
s += " "
}
return s + key + "=" + f.appendValue(key, value)
}
func (f *TextFormatter) appendValue(key string, value interface{}) string {
stringVal, ok := value.(string)
if !ok {
stringVal = fmt.Sprint(value)
}
if key == FieldKeyDelta || key == FieldKeyTime || key == FieldKeyNamespace || !f.needsQuoting(stringVal) {
return stringVal
}
return fmt.Sprintf("%q", stringVal)
}
func (f *TextFormatter) needsQuoting(text string) bool {
if f.ForceQuote {
return true
}
if f.QuoteEmptyFields && len(text) == 0 {
return true
}
if f.DisableQuote {
return false
}
for _, ch := range text {
if !((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '@' || ch == '^' || ch == '+') {
return true
}
}
return false
}
type Finalized struct {
Fields Fields
Namespace string
TimeString string
Delta string
}
func finalizeFields(
dbg Debugger, msg string, hasColor bool, flattenMsg bool, cb func(string, interface{}) interface{}) *Finalized {
ts, delta := deltas(dbg.prev)
ns := getColorStr(dbg.color, hasColor) + dbg.name + getColorOff(hasColor)
fields := Fields{}
if !flattenMsg && dbg.fields != nil {
dbg.fields["msg"] = nil
}
for k, v := range dbg.fields {
switch {
case k == FieldKeyNamespace:
fields[k] = ns
case k == FieldKeyMsg:
fields[k] = msg
case k == FieldKeyTime:
fields[k] = ts
case k == FieldKeyDelta:
fields[k] = delta
default:
fields[k] = v
}
if cb != nil {
transformed := cb(k, fields[k])
if transformed != nil {
fields[k] = transformed
}
}
}
return &Finalized{Fields: fields, Namespace: ns, TimeString: ts, Delta: delta}
}