Finding Template Execution Stack in ColdFusion

Posted July 5, 2006 at 2:39 PM

Tags: ColdFusion

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

Comments (2)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

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

Posted by Nolan Erck on Oct 30, 2008 at 2:36 PM


@Nolan,

I am sure I learned this trick somewhere else (not sure I deserve credit).

Posted by Ben Nadel on Oct 30, 2008 at 4:22 PM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting