There have been times when I wanted to get a list of all the ColdFusion templates that were executed during a single page call. I wanted to get a list similar to that of the debugging information that shows at the bottom of the page when debugging output is turned on. To figure it out, I looked at the template that handles the current debugging. When you look in the file, getting this information is actually really simple. To get a stack trace, you just have to create a ColdFusion service factory and ask for the debugging service:
<!--- Create ColdFusion service factory instance. ---> <cfset objFactory = CreateObject( "java", "coldfusion.server.ServiceFactory" ) /> <!--- Get the debugging service from the service factory. ---> <cfset objDebugging = objFactory.GetDebuggingService() /> <!--- Get the events table. This includes all events that have taken place, not just template executions. This is returned as a query. ---> <cfset qEvents = objDebugging.GetDebugger().GetData() /> <!--- Now that we have all the events in query format, do a query of queries to get only events that were template executions event. ---> <cfquery name="qTemplates" dbtype="query"> SELECT line, parent, template, endtime, starttime FROM qEvents WHERE type = 'Template' ORDER BY template ASC </cfquery> <!--- Dump out the query of template. ---> <cfdump var="#qTemplates#" />
That's all there is to it. As far as I know, the ServiceFactory stuff was not documented in ColdFusion MX 6, and was considered "unsupported", but I think that in ColdFusion 7, this is a fully supported factory object and is available for programmers to leverage.
Want to use code from this post? Check out the license.