I need to track the logic of my code.

I'm currently adding a diagnostic variable to my script that only runs in diagnostic mode. It collects all the logic that's triggered by each run. It's very time consuming to manually add this diagnostic feature to my script. Is there an automated tool that's available to do that?