3 Module for logging errors
5 __copyright__
= "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 __license__
= """ GNU General Public License
8 This program is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA. """
24 from StringIO
import StringIO
29 def debug(msg
, *args
, **kwargs
):
30 logging
.debug(msg
, args
, kwargs
)
33 caller
= traceback
.extract_stack()[-2]
36 def _log(caller
= None, *args
):
38 caller
= traceback
.extract_stack()[-2]
40 cf
= caller
[0][caller
[0].rindex("/")+1:]
43 stream
.write("%s:%d:%s: " % (cf
, caller
[1], caller
[2]))
45 if type(a
) is not type(''):
51 _log(traceback
.extract_stack()[-2], *args
)
52 l
= traceback
.format_list(traceback
.extract_stack()[:-1])
56 def logparam(locals, *vars):
59 logargs
.append(v
+ ": " + str(locals[v
]))
60 _log(traceback
.extract_stack()[-2], ", ".join(logargs
))
62 def logpparam(locals, *vars):
66 pprint
.pprint(locals[v
], sio
)
67 logargs
.append(v
+ ": " + sio
.getvalue())
69 _log(traceback
.extract_stack()[-2], "".join(logargs
))
79 def get_indent(): return " " * depth
82 caller
= traceback
.extract_stack()[-2]
84 def f2(*args
, **kwargs
):
86 if isinstance(o
, str):
90 _log(caller
, "%sEntering " % indent
, f
.__name
__)
91 argstr
= ", ".join([maybe_str(a
) for a
in args
])
93 argstr
+= ", " + ", ".join(
94 ["%s: %s" % (maybe_str(k
), maybe_str(v
))
95 for k
, v
in kwargs
.items()])
96 _log(caller
, "%sArgs: (" % indent
, argstr
, ")")
100 r
= f(*args
, **kwargs
)
103 _log(caller
, "%sReturning %s from %s" % (
104 indent
, str(r
), f
.__name
__))
110 # Originally by Bryn Keller
111 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52215
112 def log_exc(message
):
114 Print the usual traceback information, followed by a listing of all the
115 local variables in each frame.
117 _log(traceback
.extract_stack()[-2], message
)
118 tb
= sys
.exc_info()[2]
129 traceback
.print_exc()
130 print >>stream
, "Locals by frame, innermost last"
133 print >>stream
, "Frame %s in %s at line %s" % (
134 frame
.f_code
.co_name
, frame
.f_code
.co_filename
,
136 for key
, value
in frame
.f_locals
.items():
137 print >>stream
, "\t%20s = " % key
,
138 #We have to be careful not to cause a new error in our error
139 #printer! Calling str() on an unknown object could cause an
140 #error we don't want.
142 print >>stream
, value
144 print >>stream
, "<ERROR WHILE PRINTING VALUE>"