1 #===----------------------------------------------------------------------===##
3 # The LLVM Compiler Infrastructure
5 # This file is dual licensed under the MIT and the University of Illinois Open
6 # Source Licenses. See LICENSE.TXT for details.
8 #===----------------------------------------------------------------------===##
14 def trace_function(function, log_calls, log_results, label=''):
15 def wrapper(*args, **kwargs):
16 kwarg_strs = ['{}={}'.format(k, v) for (k, v) in kwargs]
17 arg_str = ', '.join([str(a) for a in args] + kwarg_strs)
18 call_str = '{}({})'.format(function.func_name, arg_str)
20 # Perform the call itself, logging before, after, and anything thrown.
23 print('{}: Calling {}'.format(label, call_str))
24 res = function(*args, **kwargs)
26 print('{}: {} -> {}'.format(label, call_str, res))
28 except Exception as ex:
30 print('{}: {} raised {}'.format(label, call_str, type(ex)))
36 def trace_object(obj, log_calls, log_results, label=''):
37 for name, member in inspect.getmembers(obj):
38 if inspect.ismethod(member):
39 # Skip meta-functions, decorate everything else
40 if not member.func_name.startswith('__'):
41 setattr(obj, name, trace_function(member, log_calls,