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