]> code.delx.au - pymsnt/blobdiff - src/debug.py
Major changes. Moved everything away from twistd and .tac files. Its nicer this way...
[pymsnt] / src / debug.py
index 10c87d7695dc848099f673d1b9b1d7040aa1ab2c..284c85ca0abd1617f71d993c098046ebfb78f577 100644 (file)
@@ -4,6 +4,64 @@
 from twisted.python import log
 import sys
 
+import config
+
+
+def observer(eventDict):
+       edm = eventDict['message']
+       if isinstance(edm, LogEvent):
+               if edm.category == INFO and config.debugLevel < 3:
+                       return
+               if (edm.category == WARN or edm.category == ERROR) and config.debugLevel < 2:
+                       return
+               text = str(edm)
+       elif edm:
+               if config.debugLevel < 3: return
+               text = ' '.join(map(str, edm))
+       else:
+               if eventDict['isError'] and eventDict.has_key('failure'):
+                       if config.debugLevel < 1: return
+                       text = eventDict['failure'].getTraceback()
+               elif eventDict.has_key('format'):
+                       if config.debugLevel < 3: return
+                       text = eventDict['format'] % eventDict
+               else:
+                       return
+       
+       # Now log it!
+       timeStr = time.strftime(self.timeFormat, time.localtime(eventDict['time']))
+       text = text.replace("\n", "\n\t")
+       global debugFile
+       debugFile.write(timeStr + msgStr)
+       
+
+debugFile = None
+def reloadConfig():
+       global debugFile
+       if debugFile:
+               debugFile.close()
+       
+       try:
+               config.debugLevel = int(config.debugLevel)
+       except ValueError:
+               config.debugLevel = 0
+
+       if config.debugLevel > 0:
+               if len(config.debugFile) > 0:
+                       try:
+                               debugFile = open(config.debugFile, "w")
+                               log.msg("Rewrote log file.")
+                       except IOError:
+                               log.discardLogs() # Give up
+                               debugFile = sys.__stdout__
+                               return
+               else:
+                       debugFile = sys.__stdout__
+
+               log.startLoggingWithObserver(observer)
+       else:
+               log.discardLogs()
+
 class INFO : pass
 class WARN : pass
 class ERROR: pass
@@ -40,3 +98,5 @@ class LogEvent:
        def log(self):
                log.msg(self)
 
+
+