Skip to content

Commit 804e7cb

Browse files
committed
Add some debug output utility functions.
- Legacy-Id: 3262
1 parent ef9fda0 commit 804e7cb

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

ietf/utils/debug.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
import sys
3+
import inspect
4+
from pprint import pformat
5+
from django.conf import settings
6+
7+
# A debug decorator, written by Paul Butler, taken from
8+
# http://paulbutler.org/archives/python-debugging-with-decorators/
9+
10+
# Number of times to indent output
11+
# A list is used to force access by reference
12+
__report_indent = [4]
13+
increment = 2
14+
15+
def set_indent(i):
16+
__report_indent[0] = i
17+
18+
def trace(fn): # renamed from 'report' by henrik 16 Jun 2011
19+
"""Decorator to print information about a function
20+
call for use while debugging.
21+
Prints function name, arguments, and call number
22+
when the function is called. Prints this information
23+
again along with the return value when the function
24+
returns.
25+
"""
26+
def fix(s,n=32):
27+
if len(s) > n+3:
28+
s = s[:n]+"..."
29+
s = s.replace('\n',' ')
30+
return s
31+
def wrap(*params,**kwargs):
32+
call = wrap.callcount = wrap.callcount + 1
33+
34+
indent = ' ' * __report_indent[0]
35+
fc = "%s(%s)" % (fn.__name__, ', '.join(
36+
[fix(repr(a)) for a in params] +
37+
["%s = %s" % (a, fix(repr(b))) for a,b in kwargs.items()]
38+
))
39+
40+
print "%s* %s [#%s]" % (indent, fc, call)
41+
__report_indent[0] += increment
42+
ret = fn(*params,**kwargs)
43+
__report_indent[0] -= increment
44+
print "%s %s [#%s] ==> %s" % (indent, fc, call, repr(ret))
45+
46+
return ret
47+
wrap.callcount = 0
48+
if settings.DEBUG:
49+
return wrap
50+
else:
51+
return fn
52+
53+
def show(name):
54+
if settings.DEBUG:
55+
frame = inspect.stack()[1][0]
56+
value = eval(name, frame.f_globals, frame.f_locals)
57+
indent = ' ' * (__report_indent[0])
58+
print "%s%s: %s" % (indent, name, value)
59+
60+
def pprint(name):
61+
if settings.DEBUG:
62+
frame = inspect.stack()[1][0]
63+
value = eval(name, frame.f_globals, frame.f_locals)
64+
indent = ' ' * (__report_indent[0])
65+
sys.stdout.write("%s%s:\n" % (indent, name))
66+
lines = pformat(value).split('\n')
67+
for line in lines:
68+
sys.stdout.write("%s %s\n"%(indent, line))
69+
70+
def say(s):
71+
if settings.DEBUG:
72+
indent = ' ' * (__report_indent[0])
73+
print "%s%s" % (indent, s)
74+

0 commit comments

Comments
 (0)