diff options
author | xengineering <me@xengineering.eu> | 2023-07-01 21:05:03 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-01 21:05:03 +0200 |
commit | 322e2c5f24c830282dbd66adbb95a7acf1255c9a (patch) | |
tree | 1321563b28a7fc0c8b5748210e802986e965e595 | |
parent | 0882d7861ed764cc856f2a6b4572630ec12751bf (diff) | |
parent | 6e973cfabe17421748af51856351f83e9856d137 (diff) | |
download | limox-322e2c5f24c830282dbd66adbb95a7acf1255c9a.tar limox-322e2c5f24c830282dbd66adbb95a7acf1255c9a.tar.zst limox-322e2c5f24c830282dbd66adbb95a7acf1255c9a.zip |
Merge branch 'byte-based-logging'
Logging used to re-encode tokens for receive logging and double-encode
tokens for send logging. This had the flexibility to pretty-print the
XML. Furthermore it was trivial to prepend a prefix like `S: ` and `C: `
for server and client.
This merge moves away from logging based on re-encoding XML tokens.
Instead the received or sent bytes are multiplexed to a logger which
uses the log module of the standard library to log the XML code.
These advantages come with this merge:
- [TX] / [RX] instead of C: / S: as prefixes (direction instead of node)
- encoding issues can be debugged via the log
- everything is now logged via the log module and has timestamps
The only disadvantage is that the logged XML is not pretty-printed
anymore. This is choosen for the RX stream to not accidentally hide
encoding issues and show what is really on the wire. That argument is
true for TX aswell and consistency is appreciated anyway.
-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 +} |