diff options
-rw-r--r-- | xmpp/encoder_decoder.go | 54 | ||||
-rw-r--r-- | xmpp/log.go | 15 |
2 files changed, 34 insertions, 35 deletions
diff --git a/xmpp/encoder_decoder.go b/xmpp/encoder_decoder.go index c340b09..b0ea77b 100644 --- a/xmpp/encoder_decoder.go +++ b/xmpp/encoder_decoder.go @@ -5,15 +5,12 @@ import ( "errors" "io" "log" - "os" ) type encoderDecoder struct { session *session - ct *xml.Encoder // client transport encoder - cl *xml.Encoder // client log encoder - st *xml.Decoder // server transport decoder - sl *xml.Encoder // server log encoder + tx *xml.Encoder + rx *xml.Decoder terminator chan bool } @@ -22,16 +19,14 @@ func newEncoderDecoder(s *session) encoderDecoder { ed.session = s - ed.ct = xml.NewEncoder(s.transport) - ed.ct.Indent("", " ") + lw := logger{"[TX] "} + w := io.MultiWriter(s.transport, lw) + ed.tx = xml.NewEncoder(w) + ed.tx.Indent("", "") - ed.cl = xml.NewEncoder(os.Stdout) - ed.cl.Indent("C: ", " ") - - ed.st = xml.NewDecoder(s.transport) - - ed.sl = xml.NewEncoder(os.Stdout) - ed.sl.Indent("S: ", " ") + lr := logger{"[RX] "} + r := io.TeeReader(s.transport, lr) + ed.rx = xml.NewDecoder(r) return ed } @@ -44,20 +39,11 @@ func (ed *encoderDecoder) encodeToken(t xml.Token) error { } }() - err = ed.ct.EncodeToken(t) - if err != nil { - return err - } - err = ed.ct.Flush() - if err != nil { - return err - } - - err = ed.cl.EncodeToken(t) + err = ed.tx.EncodeToken(t) if err != nil { return err } - err = ed.cl.Flush() + err = ed.tx.Flush() if err != nil { return err } @@ -71,18 +57,16 @@ func (ed *encoderDecoder) run() { case <-ed.terminator: return default: - t, err := ed.st.Token() + t, err := ed.rx.Token() if t != nil && err == nil { - err = ed.sl.EncodeToken(t) - if err != nil { - log.Println("Could not encode to server stream log") - } - err = ed.sl.Flush() - if err != nil { - log.Println("Could not flush server stream log") + switch t.(type) { + case xml.ProcInst: + case xml.Directive: + case xml.Comment: + default: + c := xml.CopyToken(t) + ed.session.rx <- c } - c := xml.CopyToken(t) - ed.session.rx <- c } if err != nil { if errors.Is(err, io.EOF) { diff --git a/xmpp/log.go b/xmpp/log.go new file mode 100644 index 0000000..4ac0f88 --- /dev/null +++ b/xmpp/log.go @@ -0,0 +1,15 @@ +package xmpp + +import ( + "log" +) + +type logger struct { + prefix string +} + +func (l logger) Write(p []byte) (n int, err error) { + log.Printf("%s%s", l.prefix, string(p)) + + return len(p), nil +} |