-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture-writer.go
54 lines (44 loc) · 1.11 KB
/
capture-writer.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
package jst
import (
"bytes"
"io"
)
type CapturedFunc func(name string, value string)
//
// CaptureWriter
//
type CaptureWriter struct {
originalWriter io.Writer
name string
onCaptured CapturedFunc
buffer *bytes.Buffer
}
func NewCaptureWriter(originalWriter io.Writer, name string, onCaptured CapturedFunc) *CaptureWriter {
return &CaptureWriter{
originalWriter: originalWriter,
name: name,
onCaptured: onCaptured,
buffer: bytes.NewBuffer(nil),
}
}
// ([io.Writer] interface)
func (self *CaptureWriter) Write(b []byte) (int, error) {
return self.buffer.Write(b)
}
// [io.StringWriter] interface
func (self *CaptureWriter) WriteString(s string) (int, error) {
return self.buffer.WriteString(s)
}
// [io.ByteWriter] interface
func (self *CaptureWriter) WriteByte(c byte) error {
return self.buffer.WriteByte(c)
}
// ([io.Closer] interface)
func (self *CaptureWriter) Close() error {
self.onCaptured(self.name, self.buffer.String())
return nil
}
// ([WrappingWriter] interface)
func (self *CaptureWriter) GetWrappedWriter() io.Writer {
return self.originalWriter
}