-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
logs.go
101 lines (84 loc) · 1.88 KB
/
logs.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
// Package logs implements functions to manipulate logs.
//
// Logs created are taggable.
//
// logWithTags := logs.New("a log with tags").
// Tag("a", 42).
// Tag("b", 21)
package logs
import (
"bytes"
"fmt"
"sort"
"strings"
"unsafe"
)
// New returns a log with the given description that can be tagged.
func New(v string) Log {
return Log{
description: v,
}
}
// Newf returns a log with the given formatted description that can be tagged.
func Newf(format string, v ...interface{}) Log {
return New(fmt.Sprintf(format, v...))
}
// Log is a implementation that supports tagging.
type Log struct {
description string
tags []tag
maxKeyLen int
}
// Tag sets the named tag with the given value.
func (l Log) Tag(k string, v interface{}) Log {
if l.tags == nil {
l.tags = make([]tag, 0, 8)
}
if length := len(k); length > l.maxKeyLen {
l.maxKeyLen = length
}
switch v := v.(type) {
case string:
l.tags = append(l.tags, tag{key: k, value: v})
default:
l.tags = append(l.tags, tag{key: k, value: fmt.Sprintf("%+v", v)})
}
return l
}
func (l Log) String() string {
w := bytes.NewBuffer(make([]byte, 0, len(l.description)+len(l.tags)*(l.maxKeyLen+11)))
l.format(w, 0)
return bytesToString(w.Bytes())
}
func (l Log) format(w *bytes.Buffer, indent int) {
w.WriteString(l.description)
if len(l.tags) != 0 {
w.WriteByte(':')
}
tags := l.tags
sort.Slice(tags, func(a, b int) bool {
return strings.Compare(tags[a].key, tags[b].key) < 0
})
for _, t := range l.tags {
k := t.key
v := t.value
w.WriteByte('\n')
l.indent(w, indent+4)
w.WriteString(k)
w.WriteByte(':')
l.indent(w, l.maxKeyLen-len(k)+1)
w.WriteString(v)
}
}
func (l Log) indent(w *bytes.Buffer, n int) {
for i := 0; i < n; i++ {
w.WriteByte(' ')
}
}
type tag struct {
key string
value string
}
func bytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}