Package cssutils :: Module errorhandler |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.errorhandler

      1  #!/usr/bin/env python
      2  """cssutils ErrorHandler
      3
      4  ErrorHandler
      5      used as log with usual levels (debug, info, warn, error)
      6
      7      if instanciated with ``raiseExceptions=True`` raises exeptions instead
      8      of logging
      9
     10  log
     11      defaults to instance of ErrorHandler for any kind of log message from
     12      lexerm, parser etc.
     13
     14      - raiseExceptions = [False, True]
     15      - setloglevel(loglevel)
     16  """
     17  __all__ = ['ErrorHandler']
     18  __docformat__ = 'restructuredtext'
     19  __author__ = '$LastChangedBy: doerwalter $'
     20  __date__ = '$LastChangedDate: 2007-08-02 22:58:23 +0200 (Do, 02 Aug 2007) $'
     21  __version__ = '0.9.2a2 $LastChangedRevision: 160 $'
     22
     23  import logging
     24  import xml.dom
     25
     26
     27 -class _ErrorHandler(object):
     28      """
     29      handles all errors and log messages
     30      """
     31
     32 -    def __init__(self, log,
     33                   defaultloglevel=logging.DEBUG, raiseExceptions=False):
     34          """
     35          inits log if none given
     36
     37          log
     38              for parse messages, default logs to sys.stderr
     39          defaultloglevel
     40              if none give this is logging.DEBUG
     41          raiseExceptions
     42              - True: Errors will be reported to the calling app,
     43                e.g. during building
     44              - False: Errors will be written to the log, this is the
     45                default behaviour when parsing
     46          """
     47          if log:
     48              self._log = log
     49          else:
     50              import sys
     51              self._log = logging.getLogger('CSSUTILS')
     52              hdlr = logging.StreamHandler(sys.stderr)
     53              formatter = logging.Formatter('%(levelname)s\t%(message)s')
     54              hdlr.setFormatter(formatter)
     55              self._log.addHandler(hdlr)
     56              self._log.setLevel(defaultloglevel)
     57
     58          self.raiseExceptions = raiseExceptions
     59
     60
     61 -    def __getattr__(self, name):
     62          # here if new log has been set
     63          _logcalls = {
     64              u'debug': self._log.debug,
     65              u'info': self._log.info,
     66              u'warn': self._log.warn,
     67              u'critical': self._log.critical,
     68              u'fatal': self._log.fatal,
     69              u'error': self._log.error
     70              }
     71
     72          if name in _logcalls.keys():
     73              self._logcall = _logcalls[name]
     74              return self.__handle
     75          else:
     76              raise AttributeError(
     77                  '(errorhandler) No Attribute "%s" found' % name)
     78
     79
     80 -    def setlog(self, log):
     81          """set log of errorhandler's log"""
     82          self._log = log
     83
     84
     85 -    def setloglevel(self, level):
     86          """set level of errorhandler's log"""
     87          self._log.setLevel(level)
     88
     89
     90 -    def __handle(self, msg=u'', token=None, error=xml.dom.SyntaxErr,
     91                   neverraise=False):
     92          """
     93          handles all calls
     94          logs or raises exception
     95          """
     96          if token:
     97              msg = u'%s [%s:%s: %s]' % (
     98                  msg, token.line, token.col, token.value)
     99
    100          if error and self.raiseExceptions and not neverraise:
    101              raise error(msg)
    102          else:
    103              self._logcall(msg)
    104
    105
    106 -class ErrorHandler(_ErrorHandler):
    107      "Singleton, see _ErrorHandler"
    108
    109      instance = None
    110
    111 -    def __init__(self,
    112              log=None, defaultloglevel=logging.INFO, raiseExceptions=True):
    113
    114          if ErrorHandler.instance is None:
    115              ErrorHandler.instance = _ErrorHandler(log=log,
    116                                          defaultloglevel=defaultloglevel,
    117                                          raiseExceptions=raiseExceptions)
    118          self.__dict__ = ErrorHandler.instance.__dict__
    119