From 76841cf929c4525f81908afade12bdd6e10acde5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=C3=A1n=20C=20McCord?= Date: Mon, 6 Mar 2017 12:53:49 -0500 Subject: [PATCH 1/3] Test `Sync()` call when calling AddSync If call to `Sync()` fails, wrap the WriteSyncer with a fake `Sync()`. Fixes #328 --- zapcore/write_syncer.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/zapcore/write_syncer.go b/zapcore/write_syncer.go index 2a9237640..86a47465a 100644 --- a/zapcore/write_syncer.go +++ b/zapcore/write_syncer.go @@ -40,12 +40,26 @@ type WriteSyncer interface { func AddSync(w io.Writer) WriteSyncer { switch w := w.(type) { case WriteSyncer: + if !checkSync(w.(WriteSyncer)) { + return writerWrapper{w} + } return w default: return writerWrapper{w} } } +// checkSync executes a Sync() on the WriteSyncer to check whether it operates +// cleanly. In the particular case of the WriteSyncer being a special file, +// calling Sync will return an `os.ErrInvalid` error. If, for whatever reason, +// the call of `Sync()` returns an error, this will return a `false`. +func checkSync(w WriteSyncer) bool { + if err := w.Sync(); err != nil { + return false + } + return true +} + type lockedWriteSyncer struct { sync.Mutex ws WriteSyncer From 405aaf36d1ecc106fafae59fdebab9de754446b1 Mon Sep 17 00:00:00 2001 From: Akshay Shah Date: Mon, 13 Mar 2017 09:01:43 -0700 Subject: [PATCH 2/3] Revert "Test `Sync()` call when calling AddSync" This reverts commit 9da32f380d44e2acb975631192b974dc0781691d. --- zapcore/write_syncer.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/zapcore/write_syncer.go b/zapcore/write_syncer.go index 86a47465a..2a9237640 100644 --- a/zapcore/write_syncer.go +++ b/zapcore/write_syncer.go @@ -40,26 +40,12 @@ type WriteSyncer interface { func AddSync(w io.Writer) WriteSyncer { switch w := w.(type) { case WriteSyncer: - if !checkSync(w.(WriteSyncer)) { - return writerWrapper{w} - } return w default: return writerWrapper{w} } } -// checkSync executes a Sync() on the WriteSyncer to check whether it operates -// cleanly. In the particular case of the WriteSyncer being a special file, -// calling Sync will return an `os.ErrInvalid` error. If, for whatever reason, -// the call of `Sync()` returns an error, this will return a `false`. -func checkSync(w WriteSyncer) bool { - if err := w.Sync(); err != nil { - return false - } - return true -} - type lockedWriteSyncer struct { sync.Mutex ws WriteSyncer From f7842f1fb9ab4ca2ac6066d583f95bb5b15e56d8 Mon Sep 17 00:00:00 2001 From: Akshay Shah Date: Mon, 13 Mar 2017 09:52:50 -0700 Subject: [PATCH 3/3] Ignore sync errors --- zapcore/core.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/zapcore/core.go b/zapcore/core.go index bf33b1af3..5e87f8876 100644 --- a/zapcore/core.go +++ b/zapcore/core.go @@ -95,8 +95,9 @@ func (c *ioCore) Write(ent Entry, fields []Field) error { return err } if ent.Level > ErrorLevel { - // Since we may be crashing the program, sync the output. - return c.Sync() + // Since we may be crashing the program, sync the output. Ignore Sync + // errors, pending a clean solution to issue #370. + c.Sync() } return nil }