branch: elpa/jabber
commit 6ad4d6e509cbaa6873063c459a1209e80eb2ddac
Author: Thanos Apollo <[email protected]>
Commit: Thanos Apollo <[email protected]>

    stanza: Decouple XML log sink
---
 lisp/jabber-console.el  | 11 +++--------
 lisp/jabber-stanza.el   | 17 +++++++++++++----
 tests/jabber-test-sm.el | 19 +++++++++++++++++++
 3 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/lisp/jabber-console.el b/lisp/jabber-console.el
index 2d3806a618..0b9d900b18 100644
--- a/lisp/jabber-console.el
+++ b/lisp/jabber-console.el
@@ -27,20 +27,13 @@
 
 (require 'jabber-keymap)
 (require 'jabber-input)
+(require 'jabber-stanza)
 (require 'jabber-util)
 (require 'jabber-truncate)
 (require 'xml)
 (require 'ewoc)
 (require 'sgml-mode) ;we base on this mode to hightlight XML
 
-(defcustom jabber-debug-log-xml nil
-  "Set to non-nil to log all XML i/o in *-jabber-console-JID-* buffer.
-Set to string to also dump XML i/o in specified file."
-  :type '(choice (const :tag "Do not dump XML i/o" nil)
-                 (const :tag "Dump XML i/o in console" t)
-                 (string :tag "Dump XML i/o in console and this file"))
-  :group 'jabber-debug)
-
 (defcustom jabber-console-name-format "*-jabber-console-%s-*"
   "Format for console buffer name.  %s mean connection jid."
   :type 'string
@@ -145,5 +138,7 @@ DIRECTION is a marker string (typically \"send\", \"recv\", 
or \"raw\")."
          (let ((_jabber-log-lines-to-keep jabber-console-truncate-lines))
            (jabber-truncate-top buffer jabber-console-ewoc)))))))
 
+(setq jabber-stanza-log-function #'jabber-process-console)
+
 (provide 'jabber-console)
 ;;; jabber-console.el ends here
diff --git a/lisp/jabber-stanza.el b/lisp/jabber-stanza.el
index d36f1f0654..e026b06b41 100644
--- a/lisp/jabber-stanza.el
+++ b/lisp/jabber-stanza.el
@@ -33,13 +33,22 @@
 (defconst jabber-streams-xmlns "http://etherx.jabber.org/streams";
   "RFC 6120 XMPP streams namespace.")
 
-(defvar jabber-debug-log-xml)           ; jabber-console.el
-(declare-function jabber-process-console "jabber-console" (jc direction 
xml-data))
+(defcustom jabber-debug-log-xml nil
+  "Set to non-nil to log XML input and output in a console buffer.
+Set to a string to also append XML input and output to that file."
+  :type '(choice (const :tag "Do not dump XML input or output" nil)
+                 (const :tag "Dump XML in console" t)
+                 (string :tag "Dump XML in console and this file"))
+  :group 'jabber-debug)
+
+(defvar jabber-stanza-log-function nil
+  "Function called with connection, direction, and stanza log data.")
 
 (defun jabber-log-xml (jc direction data)
   "Log DATA for JC in DIRECTION when XML debugging is enabled."
-  (when jabber-debug-log-xml
-    (jabber-process-console jc direction data)))
+  (when (and jabber-debug-log-xml
+             (functionp jabber-stanza-log-function))
+    (funcall jabber-stanza-log-function jc direction data)))
 
 (defun jabber-stanza--connection-jid (state-data)
   "Return the full JID represented by STATE-DATA."
diff --git a/tests/jabber-test-sm.el b/tests/jabber-test-sm.el
index 5d3d1daec7..ccc666d707 100644
--- a/tests/jabber-test-sm.el
+++ b/tests/jabber-test-sm.el
@@ -644,6 +644,25 @@
     (should (= (length (plist-get sd :sm-pending-queue)) 1))
     (should (equal (cdar (plist-get sd :sm-pending-queue)) pres2))))
 
+;;; Transport logging
+
+(ert-deftest jabber-test-stanza-log-calls-configured-sink ()
+  "Forward enabled XML logs to the configured sink."
+  (let ((jabber-debug-log-xml t)
+        (jabber-stanza-log-function
+         (lambda (jc direction data)
+           (list jc direction data))))
+    (should (equal (jabber-log-xml 'jc "sending" '(message))
+                   '(jc "sending" (message))))))
+
+(ert-deftest jabber-test-stanza-log-skips-disabled-sink ()
+  "Do not call the stanza log sink when XML logging is disabled."
+  (let* ((jabber-debug-log-xml nil)
+         (called nil)
+         (jabber-stanza-log-function (lambda (&rest _) (setq called t))))
+    (jabber-log-xml 'jc "sending" '(message))
+    (should-not called)))
+
 (provide 'jabber-test-sm)
 
 ;;; jabber-test-sm.el ends here

Reply via email to