From 5f84d309721a40e0572a773af47c3f7aee6ccaf2 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sat, 20 May 2017 19:39:32 +0200 Subject: [PATCH] util/ReadWriteCloserLogger: handle unset readlog | writelog --- util/io.go | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/util/io.go b/util/io.go index 5010115..68ae286 100644 --- a/util/io.go +++ b/util/io.go @@ -16,32 +16,54 @@ func NewReadWriteCloserLogger(rwc io.ReadWriteCloser, readlog, writelog string) RWC: rwc, } flags := os.O_CREATE | os.O_WRONLY - if l.ReadFile, err = os.OpenFile(readlog, flags, 0600); err != nil { - return + if readlog != "" { + if l.ReadFile, err = os.OpenFile(readlog, flags, 0600); err != nil { + return + } } - if l.WriteFile, err = os.OpenFile(writelog, flags, 0600); err != nil { - return + if writelog != "" { + if l.WriteFile, err = os.OpenFile(writelog, flags, 0600); err != nil { + return + } } return } func (c *ReadWriteCloserLogger) Read(buf []byte) (n int, err error) { n, err = c.RWC.Read(buf) - if _, writeErr := c.ReadFile.Write(buf[0:n]); writeErr != nil { - panic(writeErr) + if c.WriteFile != nil { + if _, writeErr := c.ReadFile.Write(buf[0:n]); writeErr != nil { + panic(writeErr) + } } return } func (c *ReadWriteCloserLogger) Write(buf []byte) (n int, err error) { n, err = c.RWC.Write(buf) - if _, writeErr := c.WriteFile.Write(buf[0:n]); writeErr != nil { - panic(writeErr) + if c.ReadFile != nil { + if _, writeErr := c.WriteFile.Write(buf[0:n]); writeErr != nil { + panic(writeErr) + } } return } -func (c *ReadWriteCloserLogger) Close() error { - return c.RWC.Close() +func (c *ReadWriteCloserLogger) Close() (err error) { + err = c.RWC.Close() + if err != nil { + return + } + if c.ReadFile != nil { + if err := c.ReadFile.Close(); err != nil { + panic(err) + } + } + if c.WriteFile != nil { + if err := c.WriteFile.Close(); err != nil { + panic(err) + } + } + return } type ChainedReader struct {