Skip to content

Commit

Permalink
Merge c6b96b6 into 1d90273
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Mar 7, 2017
2 parents 1d90273 + c6b96b6 commit 3fa7f9a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ func (cfg Config) buildOptions(errSink zapcore.WriteSyncer) []Option {
}

func (cfg Config) openSinks() (zapcore.WriteSyncer, zapcore.WriteSyncer, error) {
sink, closeOut, err := Open(cfg.OutputPaths...)
sink, closeOut, err := openOrGetStd(cfg.OutputPaths...)
if err != nil {
closeOut()
return nil, nil, err
}
errSink, closeErr, err := Open(cfg.ErrorOutputPaths...)
errSink, closeErr, err := openOrGetStd(cfg.ErrorOutputPaths...)
if err != nil {
closeOut()
closeErr()
Expand Down
50 changes: 36 additions & 14 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ import (
// Open is a high-level wrapper that takes a variadic number of paths, opens or
// creates each of the specified files, and combines them into a locked
// WriteSyncer. It also returns any error encountered and a function to close
// any opened files.
//
// Passing no paths returns a no-op WriteSyncer. The special paths "stdout" and
// "stderr" are interpreted as os.Stdout and os.Stderr, respectively.
// any opened files. Passing no paths returns a no-op WriteSyncer.
func Open(paths ...string) (zapcore.WriteSyncer, func(), error) {
if len(paths) == 0 {
return zapcore.AddSync(ioutil.Discard), func() {}, nil
Expand All @@ -52,16 +49,6 @@ func open(paths []string) ([]zapcore.WriteSyncer, func(), error) {
writers := make([]zapcore.WriteSyncer, 0, len(paths))
files := make([]*os.File, 0, len(paths))
for _, path := range paths {
switch path {
case "stdout":
writers = append(writers, os.Stdout)
// Don't close standard out.
continue
case "stderr":
writers = append(writers, os.Stderr)
// Don't close standard error.
continue
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
errs = errs.Append(err)
if err == nil {
Expand All @@ -76,3 +63,38 @@ func open(paths []string) ([]zapcore.WriteSyncer, func(), error) {
}
return writers, close, errs.AsError()
}

// openOrGetStd calls open, but handles the special paths "stdout" and
// "stderr" to be interpreted as os.Stdout and os.Stderr, respectively.
func openOrGetStd(paths ...string) (zapcore.WriteSyncer, func(), error) {
if len(paths) == 0 {
return zapcore.AddSync(ioutil.Discard), func() {}, nil
}

var truncatedPaths []string
var writers []zapcore.WriteSyncer
close := func() {}
var err error

for _, path := range paths {
switch path {
case "stdout":
writers = append(writers, os.Stdout)
case "stderr":
writers = append(writers, os.Stderr)
default:
truncatedPaths = append(truncatedPaths, path)
}
}

if len(truncatedPaths) > 0 {
var extraWriters []zapcore.WriteSyncer
extraWriters, close, err = open(paths)
writers = append(writers, extraWriters...)
}

if len(writers) == 1 {
return zapcore.Lock(writers[0]), close, err
}
return zapcore.Lock(zapcore.NewMultiWriteSyncer(writers...)), close, err
}
6 changes: 2 additions & 4 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ func TestOpen(t *testing.T) {
filenames []string
error string
}{
{[]string{"stdout"}, []string{os.Stdout.Name()}, ""},
{[]string{"stderr"}, []string{os.Stderr.Name()}, ""},
{[]string{temp.Name()}, []string{temp.Name()}, ""},
{[]string{"/foo/bar/baz"}, []string{}, "open /foo/bar/baz: no such file or directory"},
{
paths: []string{"stdout", "/foo/bar/baz", temp.Name(), "/baz/quux"},
filenames: []string{os.Stdout.Name(), temp.Name()},
paths: []string{"/foo/bar/baz", temp.Name(), "/baz/quux"},
filenames: []string{temp.Name()},
error: "open /foo/bar/baz: no such file or directory; open /baz/quux: no such file or directory",
},
}
Expand Down

0 comments on commit 3fa7f9a

Please sign in to comment.