Patrick the Cat
Patrick the Cat

Reputation: 2158

Python: Internal Information of Function calls and Automated Logging

I want to write a class that will show me the tree of currently working functions, that means:

Let's say I have function A call function B first and then function C, and function C calls function D. At time t = 1, B is running, so I want the stdout to be:

Root: function A -> running properly, called B
2-nd: function B -> running properly

At time t = 2, B has returned to A, so the stdout becomes:

Root: function A -> running properly, B returned

At time t = 3, A has called C. At time t = 4, C has called D. The stdout should be:

Root: function A -> running properly, B returned, called C
2-nd: function C -> running properly, called D
3-rd: function D -> running properly

At time t = 5, D returned. At time t = 6, C returned. The stdout should be:

Root: function A -> running properly, B returned, C returned

At time t = 7, function A returned:

Root: function A -> Done

Besides creating a logger object and passing it around, is there a more elegant way to automate this? What if A or B is a multithreaded process?

Any suggestions is much appreciated. Thank you!

Upvotes: 0

Views: 203

Answers (1)

Prahalad Deshpande
Prahalad Deshpande

Reputation: 4767

Take a look at the trace module within Python. You can generate annotated trace reports using this module

The definition of the trace module in the Python docs :

The trace module allows you to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed during a program run. It can be used in another program or from the command line.

Upvotes: 1

Related Questions