-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdump.go
More file actions
63 lines (55 loc) · 1.17 KB
/
dump.go
File metadata and controls
63 lines (55 loc) · 1.17 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
package slogchi
import (
"bytes"
"io"
)
type bodyWriter struct {
body *bytes.Buffer
maxSize int
}
// implements io.Writer
func (w *bodyWriter) Write(b []byte) (int, error) {
length := len(b)
if w.body.Len()+length > w.maxSize {
w.body.Truncate(min(w.maxSize, length, w.body.Len()))
return w.body.Write(b[:min(w.maxSize-w.body.Len(), length)])
}
return w.body.Write(b)
}
func newBodyWriter(maxSize int) *bodyWriter {
return &bodyWriter{
body: bytes.NewBufferString(""),
maxSize: maxSize,
}
}
type bodyReader struct {
io.ReadCloser
body *bytes.Buffer
maxSize int
bytes int
}
// implements io.Reader
func (r *bodyReader) Read(b []byte) (int, error) {
n, err := r.ReadCloser.Read(b)
if r.body != nil && r.body.Len() < r.maxSize {
if r.body.Len()+n > r.maxSize {
r.body.Write(b[:min(r.maxSize-r.body.Len(), n)])
} else {
r.body.Write(b[:n])
}
}
r.bytes += n
return n, err
}
func newBodyReader(reader io.ReadCloser, maxSize int, recordBody bool) *bodyReader {
var body *bytes.Buffer
if recordBody {
body = bytes.NewBufferString("")
}
return &bodyReader{
ReadCloser: reader,
body: body,
maxSize: maxSize,
bytes: 0,
}
}