From 59b89cfac8c3e0402b8e3c27cca8990674b8a5e3 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 1 Jul 2023 19:55:11 +0200 Subject: Implement byte-based logging for TX Using an io.MultiWriter it is trivially possible to write encoded tokens to the TCP channel aswell as to stdout. The disadvantage is that it is not possible to inject prefix data like the so far used `C: ` only for stdout and not for the TCP channel. Such a prefix is not wanted in the TCP channel and thus not wanted for both. The solution to get a nice log is to implement a transparent logging middleware which gets the raw TX stream and inserts a prefix at each line before sending to stdout. --- xmpp/encoder_decoder.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/xmpp/encoder_decoder.go b/xmpp/encoder_decoder.go index c340b09..6a37c42 100644 --- a/xmpp/encoder_decoder.go +++ b/xmpp/encoder_decoder.go @@ -10,8 +10,7 @@ import ( type encoderDecoder struct { session *session - ct *xml.Encoder // client transport encoder - cl *xml.Encoder // client log encoder + tx *xml.Encoder st *xml.Decoder // server transport decoder sl *xml.Encoder // server log encoder terminator chan bool @@ -22,11 +21,9 @@ func newEncoderDecoder(s *session) encoderDecoder { ed.session = s - ed.ct = xml.NewEncoder(s.transport) - ed.ct.Indent("", " ") - - ed.cl = xml.NewEncoder(os.Stdout) - ed.cl.Indent("C: ", " ") + w := io.MultiWriter(s.transport, os.Stdout) + ed.tx = xml.NewEncoder(w) + ed.tx.Indent("", " ") ed.st = xml.NewDecoder(s.transport) @@ -44,20 +41,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 } -- cgit v1.2.3-70-g09d2