[PATCH 1 of 6 cubicweb] [debug] add a new channel events mechanism for debugging

Laurent Peuch cortex at worlddomination.be
Thu Oct 24 06:43:38 CEST 2019


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1564620712 -7200
#      Thu Aug 01 02:51:52 2019 +0200
# Node ID 34e258d3e5ede77b96209c919c28577d673496c3
# Parent  ff63319a1730d9970240d4684df91afc0dc56d06
# Available At https://hg.logilab.org/users/lpeuch/cubicweb
#              hg pull https://hg.logilab.org/users/lpeuch/cubicweb -r 34e258d3e5ed
[debug] add a new channel events mechanism for debugging

diff --git a/cubicweb/debug.py b/cubicweb/debug.py
new file mode 100644
--- /dev/null
+++ b/cubicweb/debug.py
@@ -0,0 +1,54 @@
+# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr/ -- mailto:contact at logilab.fr
+#
+# This file is part of CubicWeb.
+#
+# CubicWeb is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+
+from logging import getLogger
+
+logger = getLogger('cubicweb')
+
+
+SUBSCRIBERS = {
+    "rql": [],
+}
+
+
+def subscribe_to_debug_channel(channel, callable):
+    if channel not in SUBSCRIBERS.keys():
+        raise Exception("debug channel '%s' doesn't exist" % channel)
+
+    SUBSCRIBERS[channel].append(callable)
+
+
+def unsubscribe_to_debug_channel(channel, callable):
+    if channel not in SUBSCRIBERS.keys():
+        raise Exception("debug channel '%s' doesn't exist" % channel)
+
+    if callable not in SUBSCRIBERS[channel]:
+        raise Exception("callable '%s' is not in debug channel '%s'" % (callable, channel))
+
+    SUBSCRIBERS[channel].remove(callable)
+
+
+def emit_to_debug_channel(channel, message):
+    if channel not in SUBSCRIBERS.keys():
+        raise Exception("debug channel '%s' doesn't exist" % channel)
+
+    for callable in SUBSCRIBERS[channel]:
+        try:
+            callable(message)
+        except Exception:
+            logger.error("Failed to send debug message '%s' to callable '%s'", message, callable, exc_info=True)



More information about the cubicweb-devel mailing list