cubicweb/debug.py
changeset 12755 7df6c6048bc8
child 12761 10b8352b0208
equal deleted inserted replaced
12754:22ece66dcd47 12755:7df6c6048bc8
       
     1 # copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 
       
    19 from logging import getLogger
       
    20 
       
    21 logger = getLogger('cubicweb')
       
    22 
       
    23 
       
    24 SUBSCRIBERS = {
       
    25     "rql": [],
       
    26 }
       
    27 
       
    28 
       
    29 def subscribe_to_debug_channel(channel, subscriber):
       
    30     if channel not in SUBSCRIBERS.keys():
       
    31         raise Exception("debug channel '%s' doesn't exist" % channel)
       
    32 
       
    33     SUBSCRIBERS[channel].append(subscriber)
       
    34 
       
    35 
       
    36 def unsubscribe_to_debug_channel(channel, subscriber):
       
    37     if channel not in SUBSCRIBERS.keys():
       
    38         raise Exception("debug channel '%s' doesn't exist" % channel)
       
    39 
       
    40     if subscriber not in SUBSCRIBERS[channel]:
       
    41         raise Exception("subscriber '%s' is not in debug channel '%s'" % (subscriber, channel))
       
    42 
       
    43     SUBSCRIBERS[channel].remove(subscriber)
       
    44 
       
    45 
       
    46 def emit_to_debug_channel(channel, message):
       
    47     if channel not in SUBSCRIBERS.keys():
       
    48         raise Exception("debug channel '%s' doesn't exist" % channel)
       
    49 
       
    50     for subscriber in SUBSCRIBERS[channel]:
       
    51         try:
       
    52             subscriber(message)
       
    53         except Exception:
       
    54             logger.error("Failed to send debug message '%s' to subscriber '%s'", message, subscriber, exc_info=True)