-# Copyright 2004 James Bunton <james@delx.cjb.net>
+# Copyright 2004-2005 James Bunton <james@delx.cjb.net>
# Licensed for distribution under the GPL version 2, check COPYING for details
-import os
+from twisted.python import log
import sys
-import config
-import utils
-import time
-
-""" A simple logging module. Use as follows.
-
-> import debug
-> debug.log("text string")
-
-If debugging is enabled then the data will be dumped to a file
-or the screen (whichever the user chose)
-"""
-
-
-file = None
-rollingStack = None
-if(config.debugSmart):
- rollingStack = utils.RollingStack(100)
-
-
-def reopenFile(first=False):
- global file
- if(file or first):
- if(file): file.close()
-
- try:
- file = open(utils.doPath(config.debugLog), 'a')
- except:
- print "Error opening debug log file. Exiting..."
- os.abort()
-
-
-def flushDebugSmart():
- global rollingStack
- if(config.debugSmart):
- file.write(rollingStack.grabAll())
- rollingStack.flush()
- file.flush()
-
-
-if(config.debugOn):
- if(len(config.debugLog) > 0):
- reopenFile(True)
- def log(data, wtime=True):
- text = ""
- if(wtime):
- text += time.strftime("%D - %H:%M:%S - ")
- text += utils.latin1(data) + "\n"
- if(config.debugSmart):
- rollingStack.push(text)
- else:
- file.write(text)
- file.flush()
- else:
- def log(data, wtime=True):
- if(wtime):
- print time.strftime("%D - %H:%M:%S - "),
- print utils.latin1(data)
- sys.stdout.flush()
- log("Debug logging enabled.")
-else:
- def log(data):
- pass
-
-
-def write(data):
- # So that I can pass this module to twisted.python.failure.Failure.printDetailedTraceback() as a file
- data = data.rstrip()
- log(data)
+class INFO : pass
+class WARN : pass
+class ERROR: pass
+
+class LogEvent:
+ def __init__(self, category=INFO, ident="", msg="", log=True):
+ self.category, self.ident, self.msg = category, ident, msg
+ frame = sys._getframe(1)
+ self.klass = frame.f_locals.get("self", frame.f_code.co_filename)
+ self.method = frame.f_code.co_name
+ self.args = frame.f_locals
+ if log:
+ self.log()
+
+ def __str__(self):
+ args = {}
+ for key in self.args.keys():
+ val = self.args[key]
+ args[key] = val
+ try:
+ if len(val) > 128:
+ args[key] = "Oversize arg"
+ except:
+ # If its not an object with length, assume that it can't be too big. Hope that's a good assumption.
+ pass
+ category = str(self.category).split(".")[1]
+ return "%s :: %s :: %s :: %s :: %s :: %s" % (category, str(self.ident), self.msg, self.method, str(self.klass), str(args))
+
+ def log(self):
+ log.msg(self)