Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 24, 2014 23:53
Show Gist options
  • Save bennadel/9752010 to your computer and use it in GitHub Desktop.
Save bennadel/9752010 to your computer and use it in GitHub Desktop.
Determining Which Function Called This Function (Using ColdFusion)
<cffunction name="StackTrace" access="public" returntype="void" output="false">
<!--- Define local variables. --->
<cfset var Trace = StructNew() />
<cftry>
<!--- Throw custom error. --->
<cfthrow
message="This is thrown to gain access to the strack trace."
type="StackTrace"
/>
<!--- Catch custom error so that page doesn't crap out. --->
<cfcatch>
<!---
Create the trace object. The automatically
generated CFCATCH object (from CFTry/CFCatch)
should now have key elements that we can get
our hands on.
--->
<cfset Trace.StackTrace = CFCATCH.StackTrace />
<cfset Trace.TagContext = CFCATCH.TagContext />
<!--- Add stack trace to request. --->
<cfset ArrayAppend(
REQUEST.StackTraces,
Trace
) />
</cfcatch>
</cftry>
<!--- Return out. --->
<cfreturn />
</cffunction>
<cffunction name="A">
<!--- Get stack trace. --->
<cfset StackTrace() />
<!--- Return the return of B(). --->
<cfreturn B() />
</cffunction>
<cffunction name="B">
<!--- Get stack trace. --->
<cfset StackTrace() />
<!--- Return the return of C(). --->
<cfreturn C() />
</cffunction>
<cffunction name="C">
<!--- Get stack trace. --->
<cfset StackTrace() />
<cfreturn "C is the bomb!" />
</cffunction>
<!--- Create the stack trace array. --->
cfset REQUEST.StackTraces = ArrayNew( 1 ) />
<!--- Call all the methods. --->
#A()#<br />
#B()#<br />
#C()#<br />
<!--- Output the stack traces. --->
<h2>
Stack Traces
</h2>
<cfloop
index="intI"
from="1"
to="#ArrayLen( REQUEST.StackTraces )#"
step="1">
<h4>
Trace #intI#
</h4>
<cfdump var="#REQUEST.StackTraces[ intI ]#" />
</cfloop>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment