Finding Template Execution Stack in ColdFusion
Posted July 5, 2006 at 2:39 PM
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:
Launch code in new window » Download code as text file »
- <!--- 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.
Download Code Snippet ZIP File
Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Newer Post
Handling NULL Values In ColdFusion
Older Post
ColdFusion Query Argument Reference Can Be Confusing
Reader Comments
Hi Ben,
Thanks for posting this -- just this morning I needed code to solve this problem (for my debugging library) and your sample works great. I've refactored it into a UDF, and made a few tweaks. Would you mind if I submitted the UDF to CFLib.org? I'll of course give you credit for the original work.
Thanks again.
Nolan
@Nolan,
I am sure I learned this trick somewhere else (not sure I deserve credit).




