def __init__(self, btlevels=10, btdefault=False, maxcount=1, *args, **kwargs): """Define Warning logger. It is defined by btlevels : int how many levels of backtrack to print to give a hint on WTF btdefault : bool if to print backtrace for all warnings at all maxcount : int how many times to print each warning """ OnceLogger.__init__(self, *args, **kwargs) self.__btlevels = btlevels self.__btdefault = btdefault self.__maxcount = maxcount self.__explanation_seen = False
def __call__(self, msg, bt=None): import traceback if bt is None: bt = self.__btdefault tb = traceback.extract_stack(limit=2) msgid = repr(tb[-2]) # take parent as the source of ID fullmsg = "WARNING: %s" % msg if not self.__explanation_seen: self.__explanation_seen = True fullmsg += "\n * Please note: warnings are " + \ "printed only once, but underlying problem might " + \ "occur many times *" if bt and self.__btlevels > 0: fullmsg += "Top-most backtrace:\n" fullmsg += reduce(lambda x, y: x + "\t%s:%d in %s where '%s'\n" % \ y, traceback.extract_stack(limit=self.__btlevels), "") OnceLogger.__call__(self, msgid, fullmsg, self.__maxcount)
def setUp(self): self.msg = "Test level 2" # output stream self.sout = StringIO() self.once = OnceLogger(handlers=[self.sout]) # set verbose to 4th level self.__oldverbosehandlers = verbose.handlers verbose.handlers = [] # so debug doesn't spoil it verbose.level = 4 if __debug__: self.__olddebughandlers = debug.handlers self.__olddebugactive = debug.active debug.active = ['1', '2', 'SLC'] debug.handlers = [self.sout] debug.offsetbydepth = False verbose.handlers = [self.sout]