-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.go
More file actions
67 lines (60 loc) · 1.21 KB
/
common.go
File metadata and controls
67 lines (60 loc) · 1.21 KB
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
package log
import (
"bytes"
"fmt"
"log/slog"
"sync"
)
var bufferPool = sync.Pool{New: func() any { return &bytes.Buffer{} }}
func buildOutput(bs ...[]byte) []byte {
buf := bufferPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
bufferPool.Put(buf)
}()
for _, b := range bs {
buf.Write(b)
buf.WriteByte(' ')
}
buf.WriteByte('\n')
b := buf.Bytes()
return b
}
func putAttr(attrs Attrs, attr slog.Attr) {
var v any
switch attr.Value.Kind() {
case slog.KindString:
v = attr.Value.String()
case slog.KindInt64:
v = attr.Value.Int64()
case slog.KindUint64:
v = attr.Value.Uint64()
case slog.KindFloat64:
v = attr.Value.Float64()
case slog.KindBool:
v = attr.Value.Bool()
case slog.KindDuration:
v = attr.Value.Duration()
case slog.KindTime:
v = attr.Value.Time()
case slog.KindAny:
switch x := attr.Value.Any().(type) {
case Marshaler:
b, _ := Marshal(x)
v = RawMessage(b)
case fmt.Stringer:
v = x.String()
case error:
v = x.Error()
default:
b, err := Marshal(x)
if err != nil {
panic(fmt.Sprintf("bad kind any: %s", attr.Value.Kind()))
}
v = RawMessage(b)
}
default:
panic(fmt.Sprintf("bad kind: %s", attr.Value.Kind()))
}
attrs[attr.Key] = v
}