2019-06-18 07:28:30 -07:00
|
|
|
package xmpp
|
2016-01-06 07:51:12 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Mediated Read / Write on socket
|
2018-09-26 07:25:04 -07:00
|
|
|
// Used if logFile from Config is not nil
|
2019-06-29 01:45:25 -07:00
|
|
|
type streamLogger struct {
|
2016-01-06 07:51:12 -08:00
|
|
|
socket io.ReadWriter // Actual connection
|
|
|
|
logFile *os.File
|
|
|
|
}
|
|
|
|
|
2019-06-29 01:45:25 -07:00
|
|
|
func newStreamLogger(conn io.ReadWriter, logFile *os.File) io.ReadWriter {
|
2016-01-06 07:51:12 -08:00
|
|
|
if logFile == nil {
|
|
|
|
return conn
|
|
|
|
} else {
|
2019-06-29 01:45:25 -07:00
|
|
|
return &streamLogger{conn, logFile}
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 01:45:25 -07:00
|
|
|
func (sp *streamLogger) Read(p []byte) (n int, err error) {
|
2019-05-16 08:46:36 -07:00
|
|
|
n, err = sp.socket.Read(p)
|
2016-01-06 07:51:12 -08:00
|
|
|
if n > 0 {
|
2019-05-16 08:46:36 -07:00
|
|
|
sp.logFile.Write([]byte("RECV:\n")) // Prefix
|
|
|
|
if n, err := sp.logFile.Write(p[:n]); err != nil {
|
2016-01-06 07:51:12 -08:00
|
|
|
return n, err
|
|
|
|
}
|
2019-05-16 08:46:36 -07:00
|
|
|
sp.logFile.Write([]byte("\n\n")) // Separator
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-29 01:45:25 -07:00
|
|
|
func (sp *streamLogger) Write(p []byte) (n int, err error) {
|
2019-05-16 08:46:36 -07:00
|
|
|
sp.logFile.Write([]byte("SEND:\n")) // Prefix
|
|
|
|
for _, w := range []io.Writer{sp.socket, sp.logFile} {
|
2016-01-06 07:51:12 -08:00
|
|
|
n, err = w.Write(p)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != len(p) {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 08:46:36 -07:00
|
|
|
sp.logFile.Write([]byte("\n\n")) // Separator
|
2016-01-06 07:51:12 -08:00
|
|
|
return len(p), nil
|
|
|
|
}
|
2019-06-29 01:45:25 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
TODO: Make RECV, SEND prefixes +
|
|
|
|
*/
|