]>
code.delx.au - pymsnt/blob - 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
4 from twisted
.python
import log
11 def observer(eventDict
):
15 printf("CRITICAL: Traceback in debug.observer2 - " + str(e
))
18 def observer2(eventDict
):
19 edm
= eventDict
['message']
20 if isinstance(edm
, LogEvent
):
21 if edm
.category
== INFO
and config
._debugLevel
< 3:
23 if (edm
.category
== WARN
or edm
.category
== ERROR
) and config
._debugLevel
< 2:
27 if config
._debugLevel
< 3: return
28 text
= ' '.join(map(str, edm
))
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
40 timeStr
= time
.strftime("[%Y-%m-%d %H:%M:%S]", time
.localtime(eventDict
['time']))
41 text
= text
.replace("\n", "\n\t")
43 debugFile
.write("%s %s\n" % (timeStr
, text
))
47 sys
.__stdout
__.write(text
+ "\n")
48 sys
.__stdout
__.flush()
58 config
._debugLevel
= int(config
.debugLevel
.strip())
60 config
._debugLevel
= 0
61 config
.debugLevel
= "0"
63 if config
._debugLevel
> 0:
64 if len(config
.debugFile
) > 0:
66 debugFile
= open(config
.debugFile
, "a")
67 log
.msg("Reopened log file.")
69 log
.discardLogs() # Give up
70 debugFile
= sys
.__stdout
__
73 debugFile
= sys
.__stdout
__
76 log
.removeObserver(observer
)
79 log
.startLoggingWithObserver(observer
)
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)
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
101 for key
in self
.args
.keys():
103 args
["self"] = "instance"
109 args
[key
] = "Oversize arg"
111 # If its not an object with length, assume that it can't be too big. Hope that's a good assumption.
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
))