I’d like to automatically call a user defined function at the beginning and end of each file. This would include the script being requested as well as each include within that script.
The purpose of this is to create a debugging tool that will log the time it takes for each file to execute. The microtime at the start and end of each file will be stored to report the results at the end of script execution.
[edit]
I spent some time looking at xhprof & xdebug
Xdebug wont work since I cannot toggle it on and off at runtime (Our current debugging system allows us to pass an attribute in the URL, which turns on/off debugging as a session variable, ultimately displaying the profiling data in the bottom of the page after load – this is very useful)
Xhprof is closer, though it doesn’t allow me to break up each function call, nor pass additional debug information to be attached to the profiling element. For example, I have a mysql query user defined function that I use for all my queries – I’d like to pass the actual query to the profile element so I can identify heavier vs lighter queries in the page (I know I can log queries too, just wanted to demonstrate how I could benefit from this requirement). Another example would be attaching incoming, outgoing function data and any other debug information I would like to track to the profile element.
Currently I manually track start, end times, additional debugging info by placing lines of code in every function that send the data to a common function, hold it in a global variable and spit it out as debugging data following the script output. I extend this to scripts, templates, etc. This manual entry of the data in every function, script, template is what I am hoping to avoid. I expect it will need to be done for some of the data unique to each function, but the repeated stuff (start, end time) should not have to be entered in every stinking query lol.
I’d highly recommend checking out Xdebug instead to help you debug your code. Doing this yourself is just going to drive you nuts!
Xdebug can do function traces out of the box and much more.
For example: