13.6 Trace Debugging

Trace debugging is sometimes the most useful way of debugging your applications, because it is quick to implement and offers immediate feedback. The Output window (a.k.a. trace window) in Flash's authoring environment provides instant feedback when you test a movie. By simply adding a trace( ) statement to your movie, you are effectively adding debugging code.

In its simplest form, trace debugging involves tracing a value to the Output window:


As you become more organized in your debugging and testing procedures, you will add more information so that the output is more readable and useful:

trace("myVariable: " + myVariable + " in function myFunction( )");

If your movie is complex and there are areas that demand constant debugging, you can put a debug flag in your script as a local variable and uncomment it when you want to debug:

// var myDebugFlag=true;
var myDebugFlag=false;

You can then sprinkle your code with trace( ) statements. This one simply tells you that a particular function, getSearchResult_Result( ), was reached without requiring you to set a breakpoint in the debugger to verify it:

if (myDebugFlag) trace("debugging getSearchResult_Result( )");

This technique works well for routines called so frequently that it would be impractical to use a breakpoint, which would repeatedly stop the movie in the debugger. On the other hand, if frequently called routines display too much trace information, it becomes difficult to find other information in the Output window.

You can create some pretty complex and useful debugging tools using trace( ). For example, you can set up a more sophisticated trace-debugging system using a wrapper routine that accepts a priority number as a parameter:

function traceDebug (message, priority) {
  if (priority <= 3) {

and call it like this:

traceDebug("This is a priority 1 debugging message", 1);
traceDebug("This is a low-priority message", 5);

By adjusting the if statement in the traceDebug( ) function or the priority of messages sent to it, you can display certain debugging messages while omitting others (or even suppress all messages).

Chapter 4 showed a client-side RecordSetDebug.as file that added custom methods to help debug your RecordSet objects on the client. The principles behind this class were based on trace debugging?building custom strings that contained descriptions and property values:

Recordset.prototype.showData = function ( ) {
  var fields = this.getColumnNames( );
  var i,j,tempfield="",temprow="",temprec="";
  trace("--Recordset Properties--");
  trace("Recordset length: " + this.getLength( ));
  trace("Fields: " + fields);
  trace("Begin records...");
  for (var i = 0; i<this.getLength( ); i++) {
    temprec = this.getItemAt(i);
    for (var j=0; j < fields.length; j++) {
      tempfield = fields[j];
      temprow += tempfield + ': "' + temprec[tempfield] + '"; ';
  trace("End records...");
  trace("--End Recordset Properties--");

This code displays object properties in a way that is readable by you. By concatenating the descriptions of the properties with their values, you insure that the values in the properties are clearly labeled.

The Output window shows warnings and errors by default, but you can set the debug level to Verbose, which gives more detail in some situations. To do so, click on the Options menu in the upper-right corner of the Output window and choose the Debug Level option. Here, you will find the choices of None, Errors, Warnings, and Verbose.

There are built-in trace-debugging features that you should be aware of as well. While debugging your movie (Control Debug Movie), the Debug menu contains two entries: List Variables and List Objects. These commands dump the current values of variables and objects in a hierarchical fashion to the Output window.

The trace( ) statements have no effect in the final movie; they are displayed only in the authoring environment's Output window. However, it is best to disable them when publishing your movie, by using the Omit Trace Actions option under File Publish Settings.

Trace debugging gives you a viable alternative to using a full-blown debugger, and it is often much quicker to get immediate feedback to a problem. Still, using the debugger will become more comfortable with time, and before long you'll wonder how you got along using only trace( ) statements. The interactive debugger and trace( ) statements are complementary tools, and you'd be well served to make use of them both as the situation demands.

    Part III: Advanced Flash Remoting