38 CHAPTER 30 Extending ColdFusion with CFX
Generating Debug Output
If your CFX tag encounters any problems during its execution, or if you need help figuring out why
your tag isn’t doing what you expect it to, you can have your tag include debug messages in the call-
ing template. Debug messages can include whatever text you want; usually you use them to display
some kind of diagnostic information that indicates whether the tag was able to connect to some kind
of data source, was denied access to some kind of file, or encountered some other type of unexpected
condition.
In the
<CFX_ComputeStandardDeviation> example, the tag is expecting to find only numbers in the
query column passed to the tag. It knows to skip over null values, and generally does so silently. But
since it’s avoiding a potential problem, you might want to be able to see which values are being
skipped over. The code in Listing 30.6 uses the following lines to output debug messages:
if ( pRequest->Debug() ) {
char buffer[50];
sprintf(buffer, “Skipping row %i because value is empty/NULL.”, Row);
pRequest->WriteDebug(buffer);
}
The pRequest->Debug() method always returns FALSE unless the tag is being called in debug mode,
so these lines are usually skipped and thus add almost no overhead to normal tag execution. To call
a CFX tag in debug mode, add a
debug flag (attribute) to the CFX tag when you use it, like so:
<!--- Compute the standard deviation of the merchandise prices --->
<CFX_ComputeStandardDeviation query=”GetMerch”
column=”MerchPrice”
variable=”MerchStdDev”
debug>
Whenever a tag is called with the debug flag, the pRequest->Debug() method (or the request.debug()
method if you’re using Java) always returns TRUE, and the WriteDebug() method includes the speci-
fied text along with the tag name when the page is visited with a browser (Figure 30.11). You can
use this simple but effective debugging facility whenever you want to include special messages for
developers but not for the rest of the world.
NOTE
debug behaves like a flag rather than a Boolean (Yes or No) attribute, so the only thing that matters is whether it is present. The
value after the
= sign is ignored, so debug=”Yes” and debug=”No” both result in debug messages being shown. You must
remove the
debug flag altogether to stop the messages.
NOTE
The debug functionality is unfortunately not integrated with the Debugging Settings page of the ColdFusion Administrator, so the
settings on that page have no bearing on whether debug messages from CFX tags are shown. In particular, debug messages from
CFX tags are not restricted by the Debugging IP Addresses list in the Administrator.
NOTE
The CFX API for Java also includes an additional set of classes for debugging Java CFX tags. You can use the classes to test
CFX tags without actually supplying them with runtime attributes in ColdFusion pages. If you find this idea interesting, consult the
“ColdFusion Java CFX Reference” section of the CFML Reference that ships with ColdFusion.
Kommentare zu diesen Handbüchern