blob: 209e25fe22417c3f8a4870f6c7f7acb21d47f432 [file] [log] [blame]
Don Newton7577f072020-01-06 12:41:11 -05001// Copyright (c) 2016 Uber Technologies, Inc.
2//
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:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
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
19// THE SOFTWARE.
20
21package zapcore
22
23import (
24 "io"
25 "sync"
26
27 "go.uber.org/multierr"
28)
29
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.
32type WriteSyncer interface {
33 io.Writer
34 Sync() error
35}
36
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.
40func AddSync(w io.Writer) WriteSyncer {
41 switch w := w.(type) {
42 case WriteSyncer:
43 return w
44 default:
45 return writerWrapper{w}
46 }
47}
48
49type lockedWriteSyncer struct {
50 sync.Mutex
51 ws WriteSyncer
52}
53
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.
56func Lock(ws WriteSyncer) WriteSyncer {
57 if _, ok := ws.(*lockedWriteSyncer); ok {
58 // no need to layer on another lock
59 return ws
60 }
61 return &lockedWriteSyncer{ws: ws}
62}
63
64func (s *lockedWriteSyncer) Write(bs []byte) (int, error) {
65 s.Lock()
66 n, err := s.ws.Write(bs)
67 s.Unlock()
68 return n, err
69}
70
71func (s *lockedWriteSyncer) Sync() error {
72 s.Lock()
73 err := s.ws.Sync()
74 s.Unlock()
75 return err
76}
77
78type writerWrapper struct {
79 io.Writer
80}
81
82func (w writerWrapper) Sync() error {
83 return nil
84}
85
86type multiWriteSyncer []WriteSyncer
87
88// NewMultiWriteSyncer creates a WriteSyncer that duplicates its writes
89// and sync calls, much like io.MultiWriter.
90func NewMultiWriteSyncer(ws ...WriteSyncer) WriteSyncer {
91 if len(ws) == 1 {
92 return ws[0]
93 }
94 // Copy to protect against https://github.com/golang/go/issues/7809
95 return multiWriteSyncer(append([]WriteSyncer(nil), ws...))
96}
97
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
101// all of them.
102func (ws multiWriteSyncer) Write(p []byte) (int, error) {
103 var writeErr error
104 nWritten := 0
105 for _, w := range ws {
106 n, err := w.Write(p)
107 writeErr = multierr.Append(writeErr, err)
108 if nWritten == 0 && n != 0 {
109 nWritten = n
110 } else if n < nWritten {
111 nWritten = n
112 }
113 }
114 return nWritten, writeErr
115}
116
117func (ws multiWriteSyncer) Sync() error {
118 var err error
119 for _, w := range ws {
120 err = multierr.Append(err, w.Sync())
121 }
122 return err
123}