1 // Copyright (c) 2016 Uber Technologies, Inc.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 "go.uber.org/multierr"
30 // A WriteSyncer is an io.Writer that can also flush any buffered data. Note
31 // that *os.File (and thus, os.Stderr and os.Stdout) implement WriteSyncer.
32 type WriteSyncer interface {
37 // AddSync converts an io.Writer to a WriteSyncer. It attempts to be
38 // intelligent: if the concrete type of the io.Writer implements WriteSyncer,
39 // we'll use the existing Sync method. If it doesn't, we'll add a no-op Sync.
40 func AddSync(w io.Writer) WriteSyncer {
41 switch w := w.(type) {
45 return writerWrapper{w}
49 type lockedWriteSyncer struct {
54 // Lock wraps a WriteSyncer in a mutex to make it safe for concurrent use. In
55 // particular, *os.Files must be locked before use.
56 func Lock(ws WriteSyncer) WriteSyncer {
57 if _, ok := ws.(*lockedWriteSyncer); ok {
58 // no need to layer on another lock
61 return &lockedWriteSyncer{ws: ws}
64 func (s *lockedWriteSyncer) Write(bs []byte) (int, error) {
66 n, err := s.ws.Write(bs)
71 func (s *lockedWriteSyncer) Sync() error {
78 type writerWrapper struct {
82 func (w writerWrapper) Sync() error {
86 type multiWriteSyncer []WriteSyncer
88 // NewMultiWriteSyncer creates a WriteSyncer that duplicates its writes
89 // and sync calls, much like io.MultiWriter.
90 func NewMultiWriteSyncer(ws ...WriteSyncer) WriteSyncer {
94 // Copy to protect against https://github.com/golang/go/issues/7809
95 return multiWriteSyncer(append([]WriteSyncer(nil), ws...))
98 // See https://golang.org/src/io/multi.go
99 // When not all underlying syncers write the same number of bytes,
100 // the smallest number is returned even though Write() is called on
102 func (ws multiWriteSyncer) Write(p []byte) (int, error) {
105 for _, w := range ws {
107 writeErr = multierr.Append(writeErr, err)
108 if nWritten == 0 && n != 0 {
110 } else if n < nWritten {
114 return nWritten, writeErr
117 func (ws multiWriteSyncer) Sync() error {
119 for _, w := range ws {
120 err = multierr.Append(err, w.Sync())