]>
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()
57 config
._debugLevel
= int(config
.debugLevel
.strip())
59 config
._debugLevel
= 0
60 config
.debugLevel
= "0"
62 if config
._debugLevel
> 0:
63 if len(config
.debugFile
) > 0:
65 debugFile
= open(config
.debugFile
, "a")
66 log
.msg("Reopened log file.")
68 log
.discardLogs() # Give up
69 debugFile
= sys
.__stdout
__
72 debugFile
= sys
.__stdout
__
74 log
.startLoggingWithObserver(observer
)
83 def __init__(self
, category
=INFO
, ident
="", msg
="", log
=True):
84 self
.category
, self
.ident
, self
.msg
= category
, ident
, msg
85 frame
= sys
._getframe
(1)
87 s
= str(frame
.f_locals
.get("self", frame
.f_code
.co_filename
))
88 self
.klass
= s
[s
.find(".")+1:s
.find(" ")]
89 self
.method
= frame
.f_code
.co_name
90 self
.args
= frame
.f_locals
96 for key
in self
.args
.keys():
98 args
["self"] = "instance"
104 args
[key
] = "Oversize arg"
106 # If its not an object with length, assume that it can't be too big. Hope that's a good assumption.
108 category
= str(self
.category
).split(".")[1]
109 return "%s :: %s :: %s :: %s :: %s :: %s" % (category
, str(self
.ident
), self
.msg
, self
.method
, str(self
.klass
), str(args
))