From ca8ca518a5648d06abf81c26b1b31465b7b09dc6 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Thu, 24 May 2018 16:31:03 +0800 Subject: [PATCH] handle create/open file error --- writers/always_new_file_writer.go | 4 +++- writers/daily_file_writer.go | 8 ++++++-- writers/fixed_size_file_writer.go | 12 +++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/writers/always_new_file_writer.go b/writers/always_new_file_writer.go index 8402538..64900f6 100644 --- a/writers/always_new_file_writer.go +++ b/writers/always_new_file_writer.go @@ -22,7 +22,9 @@ type AlwaysNewFileWriter struct { // Write implements io.Writer func (w *AlwaysNewFileWriter) Write(p []byte) (n int, err error) { if w.file == nil { - w.openFile() + if err = w.openFile(); err != nil { + return + } } return w.file.Write(p) diff --git a/writers/daily_file_writer.go b/writers/daily_file_writer.go index 72d21ca..5dd52d0 100644 --- a/writers/daily_file_writer.go +++ b/writers/daily_file_writer.go @@ -25,10 +25,14 @@ func (w *DailyFileWriter) Write(p []byte) (n int, err error) { now := time.Now() if w.file == nil { - w.openFile(&now) + if err = w.openFile(&now); err != nil { + return + } } else if now.Unix() >= w.nextDayTime { w.file.Close() - w.openFile(&now) + if err = w.openFile(&now); err != nil { + return + } } return w.file.Write(p) diff --git a/writers/fixed_size_file_writer.go b/writers/fixed_size_file_writer.go index b53fdbf..d96a7a2 100644 --- a/writers/fixed_size_file_writer.go +++ b/writers/fixed_size_file_writer.go @@ -20,10 +20,16 @@ type FixedSizeFileWriter struct { // Write implements io.Writer func (w *FixedSizeFileWriter) Write(p []byte) (n int, err error) { if w.file == nil { - w.openCurrentFile() - } else if w.currentSize > w.MaxSize { + if err = w.openCurrentFile(); err != nil { + return + } + } + + if w.currentSize > w.MaxSize { w.file.Close() - w.openNextFile() + if err = w.openNextFile(); err != nil { + return + } } w.currentSize += int64(len(p))