-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
398 lines (351 loc) · 8.06 KB
/
write.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sync"
"unicode/utf8"
"github.com/pborman/ansi"
)
// templateWriter writes out rendered data line by line.
type templateWriter struct {
*template
io.Writer
buf bytes.Buffer
}
// Write passes p to the underlying template for use as the {text} placeholder
// during rendering. A trailing newline is expected and is discarded while
// rendering the template, but appended once rendering is done, such that the
// output consists of the rendered bytes plus a newline.
func (w *templateWriter) Write(p []byte) (n int, err error) {
n = len(p)
if _, err = w.template.render(&w.buf, p[:len(p)-1]); err != nil {
return
}
w.buf.WriteRune('\n')
_, err = w.Writer.Write(w.buf.Bytes())
w.buf.Reset()
return n, err
}
func newLinewiseWriter(w io.Writer) *linewiseWriter {
return &linewiseWriter{
Writer: w,
buf: make([]byte, 0, 1024),
}
}
// linewiseWriter accumulates data until a newline is encountered, dumping it
// all at once.
type linewiseWriter struct {
io.Writer
buf []byte
}
func (w *linewiseWriter) Write(p []byte) (n int, err error) {
for {
idx := bytes.IndexByte(p, '\n')
if idx == -1 {
break
}
w.buf = append(w.buf, p[:idx+1]...)
if _, err = w.Writer.Write(w.buf); err != nil {
return
}
w.buf = w.buf[:0]
p = p[idx+1:]
n += idx + 1
}
n += len(p)
w.buf = append(w.buf, p...)
return
}
func (w *linewiseWriter) Close() (err error) {
if len(w.buf) > 0 {
_, err = w.Writer.Write(append(w.buf, '\n'))
w.buf = w.buf[:0]
}
if c, ok := w.Writer.(io.Closer); ok {
err = c.Close()
}
return
}
func newCloseWriter(w io.Writer, close func(io.Writer) error) *closeWriter {
ic := &closeWriter{Writer: w}
nop := func(io.Writer) error { return nil }
ic.close = func(io.Writer) error {
ic.close = nop
return close(w)
}
return ic
}
// closeWriter calls its close method when Closed.
type closeWriter struct {
io.Writer
close func(io.Writer) error
}
func (w *closeWriter) Close() error {
err := w.close(w.Writer)
if c, ok := w.Writer.(io.Closer); ok {
if cerr := c.Close(); err == nil {
err = cerr
}
}
return err
}
// ansiStripper strips ANSI escape sequences of any kind.
type ansiStripper struct {
io.Writer
}
func (w *ansiStripper) Write(p []byte) (n int, err error) {
n = len(p)
if p, err = ansi.Strip(p); err != nil {
return 0, err
}
if _, err = w.Writer.Write(p); err != nil {
return 0, err
}
return
}
func (w *ansiStripper) Close() error {
if c, ok := w.Writer.(io.Closer); ok {
return c.Close()
}
return nil
}
// quoteEscaper escapes unescaped single and double quotes unconditionally.
type quoteEscaper struct {
io.Writer
buf bytes.Buffer
prev rune
}
func (w *quoteEscaper) Write(p []byte) (int, error) {
n := len(p)
for len(p) > 0 {
r, sz := utf8.DecodeRune(p)
var pr rune
pr, w.prev = w.prev, r
switch r {
case '\'', '"':
if pr != '\\' {
w.buf.WriteRune('\\')
}
}
w.buf.WriteRune(r)
p = p[sz:]
}
_, err := w.Writer.Write(w.buf.Bytes())
w.buf.Reset()
return n, err
}
// newInterlockedWriterPair creates a pair of Writers whose Write method is
// protected by the same mutex, such that neither one of them can mangle the
// output of the other.
func newInterlockedWriterPair(a, b io.Writer) (io.Writer, io.Writer) {
mu := new(sync.Mutex)
a = &interlockedWriter{Mutex: mu, Writer: a}
b = &interlockedWriter{Mutex: mu, Writer: b}
return a, b
}
type interlockedWriter struct {
*sync.Mutex
io.Writer
}
func (w *interlockedWriter) Write(p []byte) (int, error) {
w.Lock()
defer w.Unlock()
return w.Writer.Write(p)
}
// byteCounter counts how many bytes it writes.
type byteCounter struct {
io.Writer
n *uint64
}
func (w *byteCounter) Write(p []byte) (int, error) {
n, err := w.Writer.Write(p)
*w.n += uint64(n)
return n, err
}
func newFileRotator(path string, maxSize int64, maxCount int) (*fileRotator, error) {
f, err := openLogfile(path)
if err != nil {
return nil, err
}
r := &fileRotator{
file: f,
maxSize: maxSize,
maxCount: maxCount,
fileRe: regexp.MustCompile(fmt.Sprintf(`%s\.\d+`, regexp.QuoteMeta(f.Name()))),
}
// Ensure we have an ordered list of files.
if err := r.reorder(0); err != nil {
return nil, err
}
// If we're limited to a number of logfiles, drop whatever falls outside
// the range.
if maxCount > 0 {
if err := r.slice(maxCount); err != nil {
return nil, err
}
}
return r, nil
}
type fileRotator struct {
file *os.File
maxSize int64
maxCount int
fileRe *regexp.Regexp
fileCount int // current file count
}
func (w *fileRotator) spaceLeft() (n int64) {
stat, err := w.file.Stat()
if err != nil {
return
}
n = w.maxSize - stat.Size()
if n < 0 {
n = 0
}
return
}
func (w *fileRotator) Write(p []byte) (n int, err error) {
defer func() {
if err != nil {
err = w.err(err)
}
}()
if int64(len(p)) > w.spaceLeft() {
if err = w.rotate(); err != nil {
return
}
}
return w.file.Write(p)
}
func (w *fileRotator) Close() error {
return w.file.Close()
}
func (w *fileRotator) rotate() (err error) {
defer func() {
if err == nil {
err = notice(w.file, "logfile turned over")
}
}()
if w.maxCount == 0 {
return w.truncate()
}
if w.fileCount == w.maxCount {
if err := w.dropLast(); err != nil {
return err
}
}
return w.prependCurrent()
}
// prependCurrent prepends <file> to the head of the file list under <file>.0
// and recreates <file>.
func (w *fileRotator) prependCurrent() error {
// Move <file>.0 to <file>.1, etc.
if err := w.shiftRight(); err != nil {
return err
}
// Copy <file> to <file>.0 and recreate <file>.
old, new := w.file.Name(), w.fileNameAt(0)
if err := w.file.Close(); err != nil {
return err
}
if err := os.Rename(old, new); err != nil {
return err
}
// Touch the original file.
f, err := openLogfile(old)
if err != nil {
return err
}
w.file = f
if w.fileCount < w.maxCount {
w.fileCount++
}
return nil
}
func (w *fileRotator) shiftRight() error {
if err := w.slice(w.fileCount); err != nil {
return err
}
return w.reorder(1)
}
func (w *fileRotator) dropLast() error {
return w.slice(w.fileCount - 1)
}
// slice drops files that fall outside the range of [0, to).
func (w *fileRotator) slice(to int) error {
fs := w.files()
switch {
case to < 0:
to = 0
case to > len(fs):
to = len(fs)
}
// Drop whatever happens to lie outside of fs[:to].
for _, f := range fs[to:] {
if err := os.Remove(f); err != nil {
return err
}
w.fileCount--
}
return nil
}
// reorder reorders files such that the first file ends with a suffix that
// corresponds to startAt, and increments subsequent ones.
func (w *fileRotator) reorder(startAt int) error {
fs := w.files()
// Start renaming files from the end of the list, such that each file has
// a slot to its "right-side" to accommodate it.
for i := len(fs) - 1; i >= 0; i-- {
old := fs[i]
new := w.fileNameAt(startAt + i)
if old != new {
if err := os.Rename(old, new); err != nil {
return err
}
}
}
w.fileCount = len(fs)
return nil
}
// files returns the list of old logfiles (i.e., everything but w.file), with the
// newest file (<file>.0) at the start of the slice.
func (w *fileRotator) files() (res []string) {
fs, err := ioutil.ReadDir(filepath.Dir(w.file.Name()))
if err != nil {
return
}
for _, f := range fs {
if w.fileRe.MatchString(f.Name()) && !f.IsDir() {
res = append(res, f.Name())
}
}
return
}
// fileNameAt generates a filename that corresponds to <file>.<idx>.
func (w *fileRotator) fileNameAt(idx int) string {
return fmt.Sprintf("%s.%s", w.file.Name(), w.nthFileSuffix(idx))
}
func (w *fileRotator) nthFileSuffix(n int) string {
return fmt.Sprintf("%0*d", countDigits(w.maxCount-1), n)
}
func (w *fileRotator) err(err error) error {
if err == nil {
return nil
}
return w.errf("%s", err)
}
func (w *fileRotator) errf(s string, args ...interface{}) error {
return fmt.Errorf("rotate: %s", fmt.Sprintf(s, args...))
}
const (
logMode = os.O_CREATE | os.O_WRONLY | os.O_APPEND
logPerms = 0644
)
func openLogfile(path string) (*os.File, error) {
return os.OpenFile(path, logMode, logPerms)
}