]> code.delx.au - pymsnt/blobdiff - src/debug.py
Avatars work with Windows Live Messenger clients
[pymsnt] / src / debug.py
index 284c85ca0abd1617f71d993c098046ebfb78f577..0f90522e631b8e37bb2c8be0ef7eb076bdbc4ffd 100644 (file)
@@ -2,38 +2,50 @@
 # Licensed for distribution under the GPL version 2, check COPYING for details
 
 from twisted.python import log
-import sys
+
+import sys, time
 
 import config
 
 
 def observer(eventDict):
+       try:
+               observer2(eventDict)
+       except Exception, e:
+               printf("CRITICAL: Traceback in debug.observer2 - " + str(e))
+
+
+def observer2(eventDict):
        edm = eventDict['message']
        if isinstance(edm, LogEvent):
-               if edm.category == INFO and config.debugLevel < 3:
+               if edm.category == INFO and config._debugLevel < 3:
                        return
-               if (edm.category == WARN or edm.category == ERROR) and config.debugLevel < 2:
+               if (edm.category == WARN or edm.category == ERROR) and config._debugLevel < 2:
                        return
                text = str(edm)
        elif edm:
-               if config.debugLevel < 3: return
+               if config._debugLevel < 3: return
                text = ' '.join(map(str, edm))
        else:
                if eventDict['isError'] and eventDict.has_key('failure'):
-                       if config.debugLevel < 1: return
+                       if config._debugLevel < 1: return
                        text = eventDict['failure'].getTraceback()
                elif eventDict.has_key('format'):
-                       if config.debugLevel < 3: return
+                       if config._debugLevel < 3: return
                        text = eventDict['format'] % eventDict
                else:
                        return
        
        # Now log it!
-       timeStr = time.strftime(self.timeFormat, time.localtime(eventDict['time']))
+       timeStr = time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime(eventDict['time']))
        text = text.replace("\n", "\n\t")
        global debugFile
-       debugFile.write(timeStr + msgStr)
+       debugFile.write("%s %s\n" % (timeStr, text))
+       debugFile.flush()
        
+def printf(text):
+       sys.__stdout__.write(text + "\n")
+       sys.__stdout__.flush()
 
 debugFile = None
 def reloadConfig():
@@ -42,15 +54,16 @@ def reloadConfig():
                debugFile.close()
        
        try:
-               config.debugLevel = int(config.debugLevel)
+               config._debugLevel = int(config.debugLevel.strip())
        except ValueError:
-               config.debugLevel = 0
+               config._debugLevel = 0
+               config.debugLevel = "0"
 
-       if config.debugLevel > 0:
+       if config._debugLevel > 0:
                if len(config.debugFile) > 0:
                        try:
-                               debugFile = open(config.debugFile, "w")
-                               log.msg("Rewrote log file.")
+                               debugFile = open(config.debugFile, "a")
+                               log.msg("Reopened log file.")
                        except IOError:
                                log.discardLogs() # Give up
                                debugFile = sys.__stdout__