2

For the purpose of building a specialized debugger for my code I would like to trace some of the functions in my code, and to log the arguments they received in each call.

I would like to be able to do this without adding line of code to each function, or even a decorator around all functions, but to set a trace around the whole run.

This is somewhat similar to what you can do with sys.settrace in the sys module: https://docs.python.org/2/library/sys.html

except that as far as I can tell the trace doesn't include the funcions' arguments.

So i would like to write a function that looks something like this:

def tracing_func(func_name, args):
    if func_name in ['func', 'foo']:
         log_func_args(func_name, args)

where log_func_args logs it in a file for later analysis.

Then set this function to be called whenever any function in my code is called, with the functions' name and args.

Can this be done?

Oren Matar
  • 651
  • 4
  • 11

1 Answers1

0

Ok so sys.settrace does the trick pretty well:

https://docs.python.org/2/library/sys.html#sys.settrace

and an example:

https://pymotw.com/2/sys/tracing.html

note that the function you pass to settrace has to return a reference to itself (or to another function for further tracing in that scope).

Oren Matar
  • 651
  • 4
  • 11