]>
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
10 def observer(eventDict
):
14 printf("CRITICAL: Traceback in debug.observer2 - " + str(e
))
17 def observer2(eventDict
):
18 edm
= eventDict
['message']
19 if isinstance(edm
, LogEvent
):
20 if edm
.category
== INFO
and config
._debugLevel
< 3:
22 if (edm
.category
== WARN
or edm
.category
== ERROR
) and config
._debugLevel
< 2:
26 if config
._debugLevel
< 3: return
27 text
= ' '.join(map(str, edm
))
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
39 timeStr
= time
.strftime("[%Y-%m-%d %H:%M:%S]", time
.localtime(eventDict
['time']))
40 text
= text
.replace("\n", "\n\t")
42 debugFile
.write("%s %s\n" % (timeStr
, text
))
46 sys
.__stdout
__.write(text
+ "\n")
47 sys
.__stdout
__.flush()
56 config
._debugLevel
= int(config
.debugLevel
.strip())
58 config
._debugLevel
= 0
59 config
.debugLevel
= "0"
61 if config
._debugLevel
> 0:
62 if len(config
.debugFile
) > 0:
64 debugFile
= open(config
.debugFile
, "a")
65 log
.msg("Reopened log file.")
67 log
.discardLogs() # Give up
68 debugFile
= sys
.__stdout
__
71 debugFile
= sys
.__stdout
__
73 log
.startLoggingWithObserver(observer
)
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)
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
95 for key
in self
.args
.keys():
97 args
["self"] = "instance"
103 args
[key
] = "Oversize arg"
105 # If its not an object with length, assume that it can't be too big. Hope that's a good assumption.
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
))