go-xmpp/stream_logger.go

53 lines
1.0 KiB
Go
Raw Permalink Normal View History

package xmpp
import (
"io"
)
// Mediated Read / Write on socket
2018-09-26 07:25:04 -07:00
// Used if logFile from Config is not nil
type streamLogger struct {
socket io.ReadWriter // Actual connection
logFile io.Writer
}
func newStreamLogger(conn io.ReadWriter, logFile io.Writer) io.ReadWriter {
if logFile == nil {
return conn
} else {
return &streamLogger{conn, logFile}
}
}
2019-11-04 03:58:10 -08:00
func (sl *streamLogger) Read(p []byte) (n int, err error) {
n, err = sl.socket.Read(p)
if n > 0 {
2019-11-04 03:58:10 -08:00
sl.logFile.Write([]byte("RECV:\n")) // Prefix
if n, err := sl.logFile.Write(p[:n]); err != nil {
return n, err
}
2019-11-04 03:58:10 -08:00
sl.logFile.Write([]byte("\n\n")) // Separator
}
return
}
2019-11-04 03:58:10 -08:00
func (sl *streamLogger) Write(p []byte) (n int, err error) {
sl.logFile.Write([]byte("SEND:\n")) // Prefix
for _, w := range []io.Writer{sl.socket, sl.logFile} {
n, err = w.Write(p)
if err != nil {
return
}
if n != len(p) {
err = io.ErrShortWrite
return
}
}
2019-11-04 03:58:10 -08:00
sl.logFile.Write([]byte("\n\n")) // Separator
return len(p), nil
}
/*
TODO: Make RECV, SEND prefixes +
*/