-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
118 lines (103 loc) · 2.38 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
// Copyright (c) 2019,CAO HONGJU. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlog
import (
"io"
"sync"
)
// Lock wraps a io.Writer in a mutex to make it safe for concurrent use.
// In particular, *os.Files must be locked before use.
func Lock(w io.Writer) io.Writer {
if _, ok := w.(*lockedWriter); ok {
// no need to layer on another lock
return w
}
return &lockedWriter{w: w, sync: getSyncFunc(w)}
}
type lockedWriter struct {
sync.Mutex
w io.Writer
sync func() error
}
func (s *lockedWriter) Write(bs []byte) (int, error) {
s.Lock()
n, err := s.w.Write(bs)
s.Unlock()
return n, err
}
func (s *lockedWriter) Sync() error {
if s.sync == nil {
return nil
}
s.Lock()
err := s.sync()
s.Unlock()
return err
}
// MultiWriter creates a writer that duplicates its writes to all the
// provided writers, similar to the Unix tee(1) command.
//
// Each write is written to each listed writer, one at a time.
// If a listed writer returns an error, that overall write operation
// stops and returns the error; it does not continue down the list.
func MultiWriter(writers ...io.Writer) io.Writer {
allWriters := make([]io.Writer, 0, len(writers))
allSyncs := make([]func() error, 0, len(writers))
for _, w := range writers {
if mw, ok := w.(*multiWriter); ok {
allWriters = append(allWriters, mw.writers...)
allSyncs = append(allSyncs, mw.syncs...)
} else {
allWriters = append(allWriters, w)
sync := getSyncFunc(w)
if sync != nil {
allSyncs = append(allSyncs, sync)
}
}
}
return &multiWriter{allWriters, allSyncs}
}
type multiWriter struct {
writers []io.Writer
syncs []func() error
}
func (mw *multiWriter) Write(p []byte) (n int, err error) {
for _, w := range mw.writers {
wn, werr := w.Write(p)
if werr != nil {
err = combineErrors(err, werr)
continue
}
if wn != len(p) {
err = combineErrors(err, io.ErrShortWrite)
}
if wn > n {
n = wn
}
}
return
}
func (mw *multiWriter) Sync() (err error) {
for _, sync := range mw.syncs {
err = combineErrors(err, sync())
}
return
}
type syncer interface {
Sync() error
}
type flusher interface {
Flush() error
}
// Get the known Sync function
func getSyncFunc(w io.Writer) func() error {
switch w := w.(type) {
case syncer:
return w.Sync
case flusher:
return w.Flush
default:
return nil
}
}