cubicweb/debug.py
author Laurent Peuch <cortex@worlddomination.be>
Thu, 01 Aug 2019 02:51:52 +0200
changeset 12755 7df6c6048bc8
child 12761 10b8352b0208
permissions -rw-r--r--
[debug] add a new channel events mechanism for debugging
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 = {
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    25
    "rql": [],
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    26
}
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    27
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    28
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    29
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
    30
    if channel not in SUBSCRIBERS.keys():
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    31
        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
    32
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    33
    SUBSCRIBERS[channel].append(subscriber)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    34
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    35
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    36
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
    37
    if channel not in SUBSCRIBERS.keys():
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    38
        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
    39
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    40
    if subscriber not in SUBSCRIBERS[channel]:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    41
        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
    42
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    43
    SUBSCRIBERS[channel].remove(subscriber)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    44
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    45
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    46
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
    47
    if channel not in SUBSCRIBERS.keys():
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    48
        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
    49
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    50
    for subscriber in SUBSCRIBERS[channel]:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    51
        try:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    52
            subscriber(message)
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    53
        except Exception:
7df6c6048bc8 [debug] add a new channel events mechanism for debugging
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    54
            logger.error("Failed to send debug message '%s' to subscriber '%s'", message, subscriber, exc_info=True)