-
Notifications
You must be signed in to change notification settings - Fork 2
/
trace.py
51 lines (40 loc) · 1.3 KB
/
trace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
indent = 0
from sys import stdout
import sys
def trprint(*args):
global indent
sys.stdout.write(indent * " ")
sys.stdout.write(*args)
sys.stdout.write("\n")
def trenter(name):
global indent
trprint("ENTERING %s:" % name)
indent += 1
def trleave(name):
global indent
indent -= 1
trprint("LEAVING %s." % name)
def trace(func, printArgs):
def inner(*args):
global indent
displayArgsString = '...'
if printArgs:
if type(printArgs).__name__ == 'list':
displayArgs = []
for i in range(len(args)):
if i in printArgs:
displayArgs += [str(args[i])]
else:
displayArgs += ['...']
displayArgsString = ', '.join(displayArgs)
else:
displayArgsString = ', '.join( [ str(arg) for arg in args[:printArgs]] )
trenter( "%s(%s)" % (func.__name__, displayArgsString) )
rt = func(*args)
trleave( "%s(%s) = %s" % (func.__name__, displayArgsString, rt) )
return rt
return inner
def funtrace(func, printArgs):
"""FUNTrace is one of the niftiest little utilities we wrote"""
func.im_class.__dict__[func.__name__] = trace(func, printArgs)
return func