-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuffer.go
120 lines (93 loc) · 2.5 KB
/
buffer.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
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package logs
import (
"fmt"
"strconv"
"sync"
"time"
)
var buffersPool = &sync.Pool{New: func() any {
return &Buffer{data: make([]byte, 0, 256)}
}}
// Buffer []byte 复用对象池
//
// 同时实现了 [xerrors.Printer] 接口。
// [Handler] 接口的实现中可能需要用到此对象。
type Buffer struct {
data []byte
detail bool
}
type AppendFunc = func(*Buffer)
// NewBuffer 声明 [Buffer] 对象
//
// detail 是否打印错误信息的调用堆栈;
func NewBuffer(detail bool) *Buffer { return buffersPool.Get().(*Buffer).Reset(detail) }
func (w *Buffer) Reset(detail bool) *Buffer {
w.data = w.data[:0]
w.detail = detail
return w
}
func (w *Buffer) Write(b []byte) (int, error) {
w.AppendBytes(b...)
return len(b), nil
}
func (w *Buffer) AppendFunc(f AppendFunc) *Buffer {
f(w)
return w
}
func (w *Buffer) AppendString(s string) *Buffer {
w.data = append(w.data, s...)
return w
}
func (w *Buffer) AppendBytes(b ...byte) *Buffer {
w.data = append(w.data, b...)
return w
}
func (w *Buffer) AppendFloat(n float64, fmt byte, prec, bitSize int) *Buffer {
w.data = strconv.AppendFloat(w.data, n, fmt, prec, bitSize)
return w
}
func (w *Buffer) AppendInt(n int64, base int) *Buffer {
w.data = strconv.AppendInt(w.data, n, 10)
return w
}
func (w *Buffer) AppendUint(n uint64, base int) *Buffer {
w.data = strconv.AppendUint(w.data, n, 10)
return w
}
func (w *Buffer) AppendTime(t time.Time, layout string) *Buffer {
w.data = t.AppendFormat(w.data, layout)
return w
}
func (w *Buffer) Append(v ...any) *Buffer {
w.data = fmt.Append(w.data, v...)
return w
}
func (w *Buffer) Appendf(format string, v ...any) *Buffer {
w.data = fmt.Appendf(w.data, format, v...)
return w
}
func (w *Buffer) Appendln(v ...any) *Buffer {
w.data = fmt.Appendln(w.data, v...)
return w
}
func (w *Buffer) AppendBuffer(f func(b *Buffer)) *Buffer {
bb := NewBuffer(w.detail)
defer bb.Free()
f(bb)
return w.AppendBytes(bb.data...)
}
func (w *Buffer) Print(v ...any) { w.data = fmt.Append(w.data, v...) }
func (w *Buffer) Printf(f string, v ...any) { w.data = fmt.Appendf(w.data, f, v...) }
func (w *Buffer) Println(v ...any) { w.data = fmt.Appendln(w.data, v...) }
func (w *Buffer) Detail() bool { return w.detail }
func (w *Buffer) Bytes() []byte { return w.data }
func (w *Buffer) Len() int { return len(w.data) }
func (w *Buffer) Free() {
const buffersPoolMaxSize = 1 << 10
if cap(w.data) < buffersPoolMaxSize {
buffersPool.Put(w)
}
}