source file: /home/buildslave/tahoe/edgy/build/src/allmydata/util/log.py
file stats: 42 lines, 38 executed: 90.5% covered
coverage versus previous test: 0 lines added, 0 lines removed
    1. import nummedobj
    2. 
    3. from foolscap.logging import log
    4. from twisted.python import log as tw_log
    5. 
    6. NOISY = log.NOISY # 10
    7. OPERATIONAL = log.OPERATIONAL # 20
    8. UNUSUAL = log.UNUSUAL # 23
    9. INFREQUENT = log.INFREQUENT # 25
   10. CURIOUS = log.CURIOUS # 28
   11. WEIRD = log.WEIRD # 30
   12. SCARY = log.SCARY # 35
   13. BAD = log.BAD # 40
   14. 
   15. 
   16. msg = log.msg
   17. 
   18. # If log.err() happens during a unit test, the unit test should fail. We
   19. # accomplish this by sending it to twisted.log too. When a WEIRD/SCARY/BAD
   20. # thing happens that is nevertheless handled, use log.msg(failure=f,
   21. # level=WEIRD) instead.
   22. 
   23. def err(*args, **kwargs):
   24.     tw_log.err(*args, **kwargs)
   25.     if 'level' not in kwargs:
   26.         kwargs['level'] = log.UNUSUAL
   27.     return log.err(*args, **kwargs)
   28. 
   29. class LogMixin(object):
   30.     """ I remember a msg id and a facility and pass them to log.msg() """
   31.     def __init__(self, facility=None, grandparentmsgid=None):
   32.         self._facility = facility
   33.         self._grandparentmsgid = grandparentmsgid
   34.         self._parentmsgid = None
   35. 
   36.     def log(self, msg, facility=None, parent=None, *args, **kwargs):
   37.         if facility is None:
   38.             facility = self._facility
   39.         if parent is None:
   40.             pmsgid = self._parentmsgid
   41.         if pmsgid is None:
   42.             pmsgid = self._grandparentmsgid
   43.         msgid = log.msg(msg, facility=facility, parent=pmsgid, *args, **kwargs)
   44.         if self._parentmsgid is None:
   45.             self._parentmsgid = msgid
   46.         return msgid
   47. 
   48. class PrefixingLogMixin(nummedobj.NummedObj, LogMixin):
   49.     """ I prepend a prefix to each msg, which includes my class and instance number as well as
   50.     a prefix supplied by my subclass. """
   51.     def __init__(self, facility=None, grandparentmsgid=None, prefix=''):
   52.         nummedobj.NummedObj.__init__(self)
   53.         LogMixin.__init__(self, facility, grandparentmsgid)
   54. 
   55.         if prefix:
   56.             self._prefix = "%s(%s): " % (self.__repr__(), prefix)
   57.         else:
   58.             self._prefix = "%s: " % (self.__repr__(),)
   59. 
   60.     def log(self, msg="", facility=None, parent=None, *args, **kwargs):
   61.         return LogMixin.log(self, self._prefix + msg, facility, parent, *args, **kwargs)