-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
writer.go
132 lines (118 loc) · 2.76 KB
/
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
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
121
122
123
124
125
126
127
128
129
130
131
132
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package writer
import (
"bytes"
"io"
"strings"
"unicode/utf8"
)
var (
dot6str = "\n" + `......` + "\n"
dot6bytes = []byte(dot6str)
//NotRecordPrefixFlag 不记录日志的前缀标识
NotRecordPrefixFlag = `--/ignore/--`
)
type OutputWriter interface {
io.Writer
String() string
Bytes() []byte
}
func New(max uint64) *cmdRec {
return &cmdRec{
buf: new(bytes.Buffer),
max: max / 2,
last: []byte{},
}
}
type cmdRec struct {
buf *bytes.Buffer
max uint64
start uint64
last []byte
ignore bool
}
func GetRuneStartIndex(end int, p []byte) int {
n := len(p)
for ; end < n; end++ {
if utf8.RuneStart(p[end]) {
break
}
}
return end
}
func (c *cmdRec) Write(p []byte) (n int, err error) {
if c.ignore {
n = len(p)
return
}
if c.start == 0 && strings.HasPrefix(string(p), NotRecordPrefixFlag) {
c.ignore = true
n = len(p)
return
}
n = len(p)
size := uint64(n)
if c.start < c.max {
remain := c.max - c.start
if remain < uint64(n) {
end := int(remain)
end = GetRuneStartIndex(end, p)
rp := p[end:]
p = p[:end]
var actualN int
actualN, err = c.buf.Write(p)
c.start += uint64(actualN)
p = rp
size = uint64(len(p))
} else {
n, err = c.buf.Write(p)
c.start += uint64(n)
return
}
}
c.last = append(c.last, p...)
size = uint64(len(c.last))
if size > c.max {
end := int(size - c.max)
end = GetRuneStartIndex(end, c.last)
c.last = c.last[end:]
}
return
}
// String returns the contents of the unread portion of the buffer
// as a string. If the Buffer is a nil pointer, it returns "<nil>".
func (c *cmdRec) String() string {
if c.buf == nil {
// Special case, useful in debugging.
return string(c.last)
}
s := c.buf.String()
if len(s) > 0 && len(c.last) > 0 {
s += dot6str + string(c.last)
}
return s
}
func (c *cmdRec) Bytes() []byte {
if c.buf == nil {
// Special case, useful in debugging.
return c.last
}
b := c.buf.Bytes()
if len(b) > 0 && len(c.last) > 0 {
b = append(b, dot6bytes...)
}
return append(b, c.last...)
}