]> code.delx.au - pymsnt/blob - src/debug.py
Debug logging now cleaner.
[pymsnt] / src / debug.py
1 # Copyright 2004-2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
3
4 from twisted.python import log
5 import sys
6
7 class INFO : pass
8 class WARN : pass
9 class ERROR: pass
10
11 class LogEvent:
12 def __init__(self, category=INFO, ident="", msg="", log=True):
13 self.category, self.ident, self.msg = category, ident, msg
14 frame = sys._getframe(1)
15 # Get the class name
16 s = str(frame.f_locals.get("self", frame.f_code.co_filename))
17 self.klass = s[s.find(".")+1:s.find(" ")]
18 self.method = frame.f_code.co_name
19 self.args = frame.f_locals
20 if log:
21 self.log()
22
23 def __str__(self):
24 args = {}
25 for key in self.args.keys():
26 if key == "self":
27 args["self"] = "instance"
28 continue
29 val = self.args[key]
30 args[key] = val
31 try:
32 if len(val) > 128:
33 args[key] = "Oversize arg"
34 except:
35 # If its not an object with length, assume that it can't be too big. Hope that's a good assumption.
36 pass
37 category = str(self.category).split(".")[1]
38 return "%s :: %s :: %s :: %s :: %s :: %s" % (category, str(self.ident), self.msg, self.method, str(self.klass), str(args))
39
40 def log(self):
41 log.msg(self)
42