Manual Debugging


Manual debugging is the process of exposing information by way of ColdFusion Markup Language (CFML) tags in order to trace errors or strange behavior. The debugging instructions or output are embedded right inside your code.

The first thing to do before placing these tags in a ColdFusion template is to critically read the error messages returned in a browser. They tell you where the problem occurred by line number and by template. Sometimes, syntax errors can cause ColdFusion to return incorrect line numbers, but usually the problem is somewhere near the line number specified.

The following represents a manual debug page:

 <cfset X="1,2,3,4,5,a,7"> <cfloop list="#X#" index="i">  <!--- manually debug for i --->  <cfif not isNumeric(i)>  <cfoutput>i equals: #i# at loop num:#ListFind(X,i)#</cfoutput>  <cfabort>  </cfif>  <cfset Y=i+3>  <!--- I keep getting an error above that 'i' is not numeric ---> </cfloop> 

This code shows how you could use a combination of <cfoutput>, functions, and <cfabort> to figure out why a problem was occurring.

Using GetTickCount()

A useful debugging technique is to use the GetTickCount() function to ascertain how long it is taking a template or code block to execute. GetTickCount() must be called twice, with the first result subtracted from the second. This way, you can calculate the total number of milliseconds that has elapsed:

 <cfset start=GetTickCount()> <cfloop from="1"         to="30000"         index="i">   <!--- I wonder how long this will take? ---> </cfloop> <cfset end=GetTickCount()> <cfoutput> <!--- here is how long this took: ---> Total milliseconds elapsed: #end-start# </cfoutput> 

Some language elements important to debugging are:

  • <cfabort>, used to stop processing

  • <cfdump>, used to dump the contents of variables

  • IsDebugMode(), which allows for code to be executed only when debugging

  • All of the Is functions used in decision checks

Using <cftimer>

<cftimer> cam also be used to perform code timing execution tests. To use <cftimer>, wrap code to be tested within <cftimer> tags, and name the timer block. ColdFusion will display the execution time in several formats:

  • In the debug output

  • In HTML comments at the code location

  • In the page output at the code location

  • Within outlines surrounding the code

Here is an example:

 <cftimer label="myloop"> <cfloop from="1"         to="30000"         index="i">   <!--- I wonder how long this will take? ---> </cfloop> </cftimer> 

NOTE

To display <cftimer> output you must enable support for the tag in the ColdFusion Administrator.




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net