-
Notifications
You must be signed in to change notification settings - Fork 1
/
logmap.go
149 lines (132 loc) · 2.81 KB
/
logmap.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
// Copyright (c) 2012-2016 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package mlog
import (
"fmt"
"sort"
)
// Map is a key value element used to pass
// data to the Logger functions.
type Map map[string]interface{}
// Keys returns an unsorted list of keys in the Map as a []string.
func (m Map) Keys() []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
// unsortedWriteBuf writes an unsorted string representation of
// the Map's key value pairs to w.
func (m Map) unsortedWriteBuf(w byteSliceWriter) {
// scratch buffer for intermediate writes
buf := bufPool.Get()
defer bufPool.Put(buf)
first := true
for k, v := range m {
if first {
first = false
} else {
w.WriteByte(' ')
}
w.WriteString(k)
w.WriteString(`="`)
fmt.Fprint(buf, v)
// pull out byte slice from buff
b := buf.Bytes()
blen := buf.Len()
p := 0
for i := 0; i < blen; i++ {
switch b[i] {
case '"':
w.Write(b[p:i])
w.WriteString(`\"`)
p = i + 1
case '\t':
w.Write(b[p:i])
w.WriteString(`\t`)
p = i + 1
case '\r':
w.Write(b[p:i])
w.WriteString(`\r`)
p = i + 1
case '\n':
w.Write(b[p:i])
w.WriteString(`\n`)
p = i + 1
}
}
if p < blen {
w.Write(b[p:blen])
}
w.WriteByte('"')
// truncate intermediate buf so it is clean for next loop
buf.Truncate(0)
}
}
// sortedWriteBuf writes a sorted string representation of
// the Map's key value pairs to w.
func (m Map) sortedWriteBuf(w byteSliceWriter) {
// scratch buffer for intermediate writes
buf := bufPool.Get()
defer bufPool.Put(buf)
keys := m.Keys()
sort.Strings(keys)
first := true
for _, k := range keys {
if first {
first = false
} else {
w.WriteByte(' ')
}
w.WriteString(k)
w.WriteString(`="`)
fmt.Fprint(buf, m[k])
b := buf.Bytes()
blen := buf.Len()
p := 0
for i := 0; i < blen; i++ {
switch b[i] {
case '"':
w.Write(b[p:i])
w.WriteString(`\"`)
p = i + 1
case '\t':
w.Write(b[p:i])
w.WriteString(`\t`)
p = i + 1
case '\r':
w.Write(b[p:i])
w.WriteString(`\r`)
p = i + 1
case '\n':
w.Write(b[p:i])
w.WriteString(`\n`)
p = i + 1
}
}
if p < blen {
w.Write(b[p:blen])
}
w.WriteByte('"')
// truncate intermediate buf so it is clean for next loop
buf.Truncate(0)
}
}
// String returns an unsorted string representation of
// the Map's key value pairs.
func (m Map) String() string {
buf := bufPool.Get()
defer bufPool.Put(buf)
m.unsortedWriteBuf(buf)
return buf.String()
}
// SortedString returns a sorted string representation of
// the Map's key value pairs.
func (m Map) SortedString() string {
buf := bufPool.Get()
defer bufPool.Put(buf)
m.sortedWriteBuf(buf)
return buf.String()
}