work on aggregating items of category feeds and minor fixes
[straw.git] / src / lib / error.py
blobd0da326af2b1bb9e2eb57f41566638408290802f
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
25 import logging
27 stream = sys.stderr
29 def debug(msg, *args, **kwargs):
30 logging.debug(msg, args, kwargs)
32 def log(*args):
33 caller = traceback.extract_stack()[-2]
34 _log(caller, *args)
36 def _log(caller = None, *args):
37 if caller is None:
38 caller = traceback.extract_stack()[-2]
39 try:
40 cf = caller[0][caller[0].rindex("/")+1:]
41 except:
42 cf = caller[0]
43 stream.write("%s:%d:%s: " % (cf, caller[1], caller[2]))
44 for a in args:
45 if type(a) is not type(''):
46 a = repr(a)
47 stream.write(a)
48 stream.write("\n")
50 def logtb(*args):
51 _log(traceback.extract_stack()[-2], *args)
52 l = traceback.format_list(traceback.extract_stack()[:-1])
53 for e in l:
54 stream.write(e)
56 def logparam(locals, *vars):
57 logargs = []
58 for v in vars:
59 logargs.append(v + ": " + str(locals[v]))
60 _log(traceback.extract_stack()[-2], ", ".join(logargs))
62 def logpparam(locals, *vars):
63 logargs = []
64 for v in vars:
65 sio = StringIO()
66 pprint.pprint(locals[v], sio)
67 logargs.append(v + ": " + sio.getvalue())
68 sio.close()
69 _log(traceback.extract_stack()[-2], "".join(logargs))
72 depth = 0
73 def incr_depth():
74 global depth
75 depth += 1
76 def decr_depth():
77 global depth
78 depth -= 1
79 def get_indent(): return " " * depth
81 def debug_around(f):
82 caller = traceback.extract_stack()[-2]
84 def f2(*args, **kwargs):
85 def maybe_str(o):
86 if isinstance(o, str):
87 return repr(o)
88 return str(o)
89 indent = get_indent()
90 _log(caller, "%sEntering " % indent, f.__name__)
91 argstr = ", ".join([maybe_str(a) for a in args])
92 if len(kwargs) > 0:
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, ")")
97 incr_depth()
98 r = None
99 try:
100 r = f(*args, **kwargs)
101 finally:
102 decr_depth()
103 _log(caller, "%sReturning %s from %s" % (
104 indent, str(r), f.__name__))
105 return r
108 return f2
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]
119 while 1:
120 if not tb.tb_next:
121 break
122 tb = tb.tb_next
123 stack = []
124 f = tb.tb_frame
125 while f:
126 stack.append(f)
127 f = f.f_back
128 stack.reverse()
129 traceback.print_exc()
130 print >>stream, "Locals by frame, innermost last"
131 for frame in stack:
132 print >>stream
133 print >>stream, "Frame %s in %s at line %s" % (
134 frame.f_code.co_name, frame.f_code.co_filename,
135 frame.f_lineno)
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.
141 try:
142 print >>stream, value
143 except:
144 print >>stream, "<ERROR WHILE PRINTING VALUE>"