cubicweb/debug.py
author Laurent Peuch <cortex@worlddomination.be>
Thu, 12 Sep 2019 05:59:10 +0200
changeset 12768 7d2c61d40fe9
parent 12766 682d0790997f
child 12802 f6662c603730
permissions -rw-r--r--
[debug-toolbar] add registry decisions debug panel Closes #17219866
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12755
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     1
# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     3
#
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     4
# This file is part of CubicWeb.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     5
#
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     9
# any later version.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    10
#
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    14
# details.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    15
#
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    18
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    19
from logging import getLogger
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    20
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    21
logger = getLogger('cubicweb')
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    22
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    23
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    24
SUBSCRIBERS = {
12764
fb97669efcaa [debug-toolbar] add cw general panel with controller
Laurent Peuch <cortex@worlddomination.be>
parents: 12761
diff changeset
    25
    "controller": [],
12755
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    26
    "rql": [],
12761
10b8352b0208 [debug-toolbar/rql] display sql queries generated by rql ones
Laurent Peuch <cortex@worlddomination.be>
parents: 12755
diff changeset
    27
    "sql": [],
12766
682d0790997f [debug-toolbar] add registry panel
Laurent Peuch <cortex@worlddomination.be>
parents: 12764
diff changeset
    28
    "vreg": [],
12768
7d2c61d40fe9 [debug-toolbar] add registry decisions debug panel
Laurent Peuch <cortex@worlddomination.be>
parents: 12766
diff changeset
    29
    "registry_decisions": [],
12755
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    30
}
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    31
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    32
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    33
def subscribe_to_debug_channel(channel, subscriber):
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    34
    if channel not in SUBSCRIBERS.keys():
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    35
        raise Exception("debug channel '%s' doesn't exist" % channel)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    36
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    37
    SUBSCRIBERS[channel].append(subscriber)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    38
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    39
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    40
def unsubscribe_to_debug_channel(channel, subscriber):
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    41
    if channel not in SUBSCRIBERS.keys():
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    42
        raise Exception("debug channel '%s' doesn't exist" % channel)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    43
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    44
    if subscriber not in SUBSCRIBERS[channel]:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    45
        raise Exception("subscriber '%s' is not in debug channel '%s'" % (subscriber, channel))
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    46
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    47
    SUBSCRIBERS[channel].remove(subscriber)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    48
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    49
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    50
def emit_to_debug_channel(channel, message):
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    51
    if channel not in SUBSCRIBERS.keys():
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    52
        raise Exception("debug channel '%s' doesn't exist" % channel)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    53
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    54
    for subscriber in SUBSCRIBERS[channel]:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    55
        try:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    56
            subscriber(message)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    57
        except Exception:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    58
            logger.error("Failed to send debug message '%s' to subscriber '%s'", message, subscriber, exc_info=True)