]> code.delx.au - pymsnt/blob - src/debug.py
Fixed path seperator issues for Windows. Fixed reactor config option.
[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, time
6
7 import config
8
9
10 def observer(eventDict):
11 try:
12 observer2(eventDict)
13 except Exception, e:
14 printf("CRITICAL: Traceback in debug.observer2 - " + str(e))
15
16
17 def observer2(eventDict):
18 edm = eventDict['message']
19 if isinstance(edm, LogEvent):
20 if edm.category == INFO and config._debugLevel < 3:
21 return
22 if (edm.category == WARN or edm.category == ERROR) and config._debugLevel < 2:
23 return
24 text = str(edm)
25 elif edm:
26 if config._debugLevel < 3: return
27 text = ' '.join(map(str, edm))
28 else:
29 if eventDict['isError'] and eventDict.has_key('failure'):
30 if config._debugLevel < 1: return
31 text = eventDict['failure'].getTraceback()
32 elif eventDict.has_key('format'):
33 if config._debugLevel < 3: return
34 text = eventDict['format'] % eventDict
35 else:
36 return
37
38 # Now log it!
39 timeStr = time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime(eventDict['time']))
40 text = text.replace("\n", "\n\t")
41 global debugFile
42 debugFile.write("%s %s\n" % (timeStr, text))
43 debugFile.flush()
44
45 def printf(text):
46 sys.__stdout__.write(text + "\n")
47 sys.__stdout__.flush()
48
49 debugFile = None
50 def reloadConfig():
51 global debugFile
52 if debugFile:
53 debugFile.close()
54
55 try:
56 config._debugLevel = int(config.debugLevel.strip())
57 except ValueError:
58 config._debugLevel = 0
59 config.debugLevel = "0"
60
61 if config._debugLevel > 0:
62 if len(config.debugFile) > 0:
63 try:
64 debugFile = open(config.debugFile, "a")
65 log.msg("Reopened log file.")
66 except IOError:
67 log.discardLogs() # Give up
68 debugFile = sys.__stdout__
69 return
70 else:
71 debugFile = sys.__stdout__
72
73 log.startLoggingWithObserver(observer)
74 else:
75 log.discardLogs()
76
77 class INFO : pass
78 class WARN : pass
79 class ERROR: pass
80
81 class LogEvent:
82 def __init__(self, category=INFO, ident="", msg="", log=True):
83 self.category, self.ident, self.msg = category, ident, msg
84 frame = sys._getframe(1)
85 # Get the class name
86 s = str(frame.f_locals.get("self", frame.f_code.co_filename))
87 self.klass = s[s.find(".")+1:s.find(" ")]
88 self.method = frame.f_code.co_name
89 self.args = frame.f_locals
90 if log:
91 self.log()
92
93 def __str__(self):
94 args = {}
95 for key in self.args.keys():
96 if key == "self":
97 args["self"] = "instance"
98 continue
99 val = self.args[key]
100 args[key] = val
101 try:
102 if len(val) > 128:
103 args[key] = "Oversize arg"
104 except:
105 # If its not an object with length, assume that it can't be too big. Hope that's a good assumption.
106 pass
107 category = str(self.category).split(".")[1]
108 return "%s :: %s :: %s :: %s :: %s :: %s" % (category, str(self.ident), self.msg, self.method, str(self.klass), str(args))
109
110 def log(self):
111 log.msg(self)
112
113
114