Skip to content

Commit 1898607

Browse files
committed
Move EINVAL check into syncwriter
Closes #44
1 parent 25e1d94 commit 1898607

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

Diff for: internal/syncwriter/syncwriter.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
package syncwriter
33

44
import (
5+
"errors"
56
"io"
67
"os"
78
"sync"
9+
"syscall"
810
)
911

1012
// Writer implements a concurrency safe io.Writer wrapper.
@@ -39,19 +41,20 @@ func (w *Writer) Sync() error {
3941
w.mu.Lock()
4042
defer w.mu.Unlock()
4143

42-
if f, ok := w.w.(*os.File); ok {
43-
// We do not want to sync if the writer is os.Stdout or os.Stderr
44-
// as that is unsupported on both Linux and MacOS and will return
45-
// an error about the fd being an invalid argument.
44+
s, ok := w.w.(syncer)
45+
if !ok {
46+
return nil
47+
}
48+
err := s.Sync()
49+
if _, ok := w.w.(*os.File); ok {
50+
// Opened files do not necessarily support syncing.
51+
// E.g. stdout and stderr both do not so we need
52+
// to ignore these errors.
4653
// See https://github.com/uber-go/zap/issues/370
47-
if f == os.Stdout || f == os.Stderr {
54+
// See https://github.com/cdr/slog/pull/43
55+
if errors.Is(err, syscall.EINVAL) {
4856
return nil
4957
}
50-
return f.Sync()
51-
}
52-
53-
if s, ok := w.w.(syncer); ok {
54-
return s.Sync()
5558
}
56-
return nil
59+
return err
5760
}

Diff for: slog.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package slog
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"os"
87
"runtime"
98
"sync"
109
"sync/atomic"
11-
"syscall"
1210
"time"
1311

1412
"go.opencensus.io/trace"
@@ -320,8 +318,7 @@ func (l Logger) log(ctx context.Context, level Level, msg string, fields []Field
320318
func (l Logger) Sync() {
321319
for _, s := range l.sinks {
322320
err := s.sink.Sync()
323-
// Certain devices do not support sync, maybe because they are synchronous by default.
324-
if err != nil && !errors.Is(err, syscall.EINVAL) {
321+
if err != nil {
325322
fmt.Fprintf(os.Stderr, "slog: sink with name %v and type %T failed to sync: %+v\n", s.name, s.sink, err)
326323
continue
327324
}

0 commit comments

Comments
 (0)