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
def log(self):
log.msg(self)
+
+