Updated Arabic Translation by Djihed Afifi.
[straw.git] / src / lib / error.py
blobe52b74ed386a32343010fc7a9ab89af278272401
1 """ error.py
3 Module for logging errors
4 """
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
11 version.
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. """
21 import sys
22 import traceback
23 import pprint
24 from StringIO import StringIO
26 stream = sys.stderr
28 def log(*args):
29 caller = traceback.extract_stack()[-2]
30 _log(caller, *args)
32 def _log(caller = None, *args):
33 if caller is None:
34 caller = traceback.extract_stack()[-2]
35 try:
36 cf = caller[0][caller[0].rindex("/")+1:]
37 except:
38 cf = caller[0]
39 stream.write("%s:%d:%s: " % (cf, caller[1], caller[2]))
40 for a in args:
41 if type(a) is not type(''):
42 a = repr(a)
43 stream.write(a)
44 stream.write("\n")
46 def logtb(*args):
47 _log(traceback.extract_stack()[-2], *args)
48 l = traceback.format_list(traceback.extract_stack()[:-1])
49 for e in l:
50 stream.write(e)
52 def logparam(locals, *vars):
53 logargs = []
54 for v in vars:
55 logargs.append(v + ": " + str(locals[v]))
56 _log(traceback.extract_stack()[-2], ", ".join(logargs))
58 def logpparam(locals, *vars):
59 logargs = []
60 for v in vars:
61 sio = StringIO()
62 pprint.pprint(locals[v], sio)
63 logargs.append(v + ": " + sio.getvalue())
64 sio.close()
65 _log(traceback.extract_stack()[-2], "".join(logargs))
68 depth = 0
69 def incr_depth():
70 global depth
71 depth += 1
72 def decr_depth():
73 global depth
74 depth -= 1
75 def get_indent(): return " " * depth
77 def debug_around(f):
78 caller = traceback.extract_stack()[-2]
80 def f2(*args, **kwargs):
81 def maybe_str(o):
82 if isinstance(o, str):
83 return repr(o)
84 return str(o)
85 indent = get_indent()
86 _log(caller, "%sEntering " % indent, f.__name__)
87 argstr = ", ".join([maybe_str(a) for a in args])
88 if len(kwargs) > 0:
89 argstr += ", " + ", ".join(
90 ["%s: %s" % (maybe_str(k), maybe_str(v))
91 for k, v in kwargs.items()])
92 _log(caller, "%sArgs: (" % indent, argstr, ")")
93 incr_depth()
94 r = None
95 try:
96 r = f(*args, **kwargs)
97 finally:
98 decr_depth()
99 _log(caller, "%sReturning %s from %s" % (
100 indent, str(r), f.__name__))
101 return r
104 return f2
106 # Originally by Bryn Keller
107 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52215
108 def log_exc(message):
110 Print the usual traceback information, followed by a listing of all the
111 local variables in each frame.
113 _log(traceback.extract_stack()[-2], message)
114 tb = sys.exc_info()[2]
115 while 1:
116 if not tb.tb_next:
117 break
118 tb = tb.tb_next
119 stack = []
120 f = tb.tb_frame
121 while f:
122 stack.append(f)
123 f = f.f_back
124 stack.reverse()
125 traceback.print_exc()
126 print >>stream, "Locals by frame, innermost last"
127 for frame in stack:
128 print >>stream
129 print >>stream, "Frame %s in %s at line %s" % (
130 frame.f_code.co_name, frame.f_code.co_filename,
131 frame.f_lineno)
132 for key, value in frame.f_locals.items():
133 print >>stream, "\t%20s = " % key,
134 #We have to be careful not to cause a new error in our error
135 #printer! Calling str() on an unknown object could cause an
136 #error we don't want.
137 try:
138 print >>stream, value
139 except:
140 print >>stream, "<ERROR WHILE PRINTING VALUE>"