|
1 | 1 | import sys |
2 | 2 | import time as timeutils |
3 | 3 | import inspect |
| 4 | + |
4 | 5 | try: |
5 | 6 | import syslog |
6 | 7 | logger = syslog.syslog |
|
15 | 16 | pformat = lambda x: x |
16 | 17 |
|
17 | 18 | import cProfile |
| 19 | +import traceback as tb |
| 20 | + |
18 | 21 | try: |
19 | 22 | from django.conf import settings |
20 | 23 | debug = settings.DEBUG |
21 | 24 | except ImportError: |
22 | 25 | debug = True |
| 26 | + |
23 | 27 | from decorator import decorator |
24 | 28 |
|
25 | 29 | # A debug decorator, written by Paul Butler, taken from |
|
34 | 38 | # Number of times to indent output |
35 | 39 | # A list is used to force access by reference |
36 | 40 | _report_indent = [4] |
37 | | -_mark = timeutils.clock() |
| 41 | +_mark = [ timeutils.clock() ] |
38 | 42 |
|
39 | 43 | def set_indent(i): |
40 | 44 | _report_indent[0] = i |
@@ -79,16 +83,15 @@ def wrap(fn, *params,**kwargs): |
79 | 83 | return fn |
80 | 84 |
|
81 | 85 | def mark(): |
82 | | - say("! mark") |
83 | | - _mark = timeutils.clock() |
| 86 | + _mark[0] = timeutils.clock() |
84 | 87 |
|
85 | 88 | def lap(s): |
86 | | - tau = timeutils.clock() - _mark |
| 89 | + tau = timeutils.clock() - _mark[0] |
87 | 90 | say("> %s: %.3fs since mark" % (s, tau)) |
88 | 91 |
|
89 | 92 | def clock(s): |
90 | 93 | lap(s) |
91 | | - _mark = timeutils.clock() |
| 94 | + _mark[0] = timeutils.clock() |
92 | 95 |
|
93 | 96 | def time(fn): |
94 | 97 | """Decorator to print timing information about a function call. |
@@ -170,3 +173,16 @@ def wrapper(*args, **kwargs): |
170 | 173 | else: |
171 | 174 | return fn |
172 | 175 |
|
| 176 | +def traceback(): |
| 177 | + if debug: |
| 178 | + indent = ' ' * (_report_indent[0]) |
| 179 | + for s in tb.format_stack()[:-1]: |
| 180 | + sys.stderr.write("%s%s" % (indent, s)) |
| 181 | + |
| 182 | +def info(name): |
| 183 | + if debug: |
| 184 | + frame = inspect.stack()[1][0] |
| 185 | + value = eval(name, frame.f_globals, frame.f_locals) |
| 186 | + indent = ' ' * (_report_indent[0]) |
| 187 | + sys.stderr.write("%s%s: %s %s\n" % (indent, name, value, type(value))) |
| 188 | + |
0 commit comments