util/ReadWriteCloserLogger: handle unset readlog | writelog

This commit is contained in:
Christian Schwarz 2017-05-20 19:39:32 +02:00
parent 3b1cac1ea2
commit 5f84d30972

View File

@ -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 {