I grew tired of scrolling down through the very long tracbacks to get
to the actual error, so I wrote a function that lets me skip
everything up to my handler function. While I was at it I figured I
may as well highlight the things I want to see to make the tracebacks
easier to read. When I was finished I liked it so much I had to share,
it would be nice if this found it's way into mod_python. To adapt it
for your purposes, change the condition func == 'handler' to something
else (you probably don't want to cut out the call to handler like me.)
Please excuse my lousy html, I wrote it to be small so it didn't
clutter up my source, I hate html in my python code. Feel free to
change whatever you like.
-Dan
import sys, os, re, traceback
re_traceback = re.compile(r'File "(?P<file>[^"]+?)", line
(?P<line>\d+?), in (?P<func>.+?)\s+(?P<context>.+?)$',
re.DOTALL | re.MULTILINE)
def get_formatted_traceback(req):
''' Returns an html formatted traceback for the exception. '''
req.content_type = 'text/html'
buf = ['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"',
'"http://www.w3.org/TR/html4/loose.dtd">',
'<html><head>',
'<style type="text/css">',
'b { color: navy }',
'strong { color: green }',
'.context { font-style: italic; margin-left: 30px }',
'.trace { margin: 10px }'
'span { font-weight: bold; color: blue }',
'</style>',
'</head><body>']
begin_trace = False
for e in traceback.format_exception(*sys.exc_info()):
m = re_traceback.search(e)
if m is not None:
path, line, func, context = m.group('file', 'line', 'func', 'context')
path, filename = os.path.split(path)
if begin_trace:
buf.append('<div>File "%s\<b>%s</b>", line %s, in
<strong>%s</strong>\r\n<div class="context">%s</div></div>' \
% (path, filename, line, func, context))
else:
# Ignore the stuff up to and including the entry point of
the application at the call to handler
if func == 'handler': begin_trace = True
else:
if begin_trace: buf.append('</div><span>%s</span>' % e) #
Last Line (the error)
else: buf.append('%s<div class="trace">' % e) # First line (Traceback:)
buf.append('</body></html>')
return '\r\n'.join(buf)