Recipe 1.4. How to
Trace a Message
Problem
You need to
trace out a message or the value of some data at runtime.
Solution
Use the trace function, pass the data to
it, run your application, and look for a message in the Console in
Eclipse.
Discussion
You can trace out a message, the value of a
variable, or just about any other data using trace, just as
you would in earlier versions of ActionScript. Some examples:
trace("Hello, world");
trace(userName);
trace("My name is " + userName + ".");
Since the .swf is now launched in an
external browser, it might seem that there is no way to capture the
output of these trace statements. Fortunately, it is possible, and
this functionality has been built in to Flex Builder 2 via the
Console view. The Console view is the equivalent of the Output
panel in the Flash IDE. Although it is not open when you first
start Eclipse, it appears when needed.
The only requirement to using trace and
the Console view is that you
use Debug to test your application. Doing so includes extra
features in the .swf that allows it to communicate back to
the Console behind the scenes and pass any messages you trace. The
following class creates a variable, assigns a value to it, and then
traces it, along with some other string data:
package {
import flash.display.Sprite;
public class ExampleApplication extends Sprite {
public function ExampleApplication( ) {
var userName:String = "Bill Smith";
trace("My name is " + userName + ".");
}
}
}
Now when you debug your application, it launches
as usual in your default browser. Close the browser and switch back
to Eclipse. You will see that
the Console view is now open and has displayed the data you traced
out.
When you launch the debug version of an
application, you must have the debug version of Flash Player
installed. If you don't have the debug version of Flash Player,
you'll see an error message notifying you, and you'll have to
download and install it from http://www.adobe.com/support/flashplayer/downloads.html.
Additionally, the debug version of Flash Player
can write trace content to a file. The file that Flash Player uses
is determined by mm.cfg, a file that is stored
in the following locations:
Operating system |
Location |
Windows XP |
C:\Documents and Settings\[user name]\mm.cfg |
Windows 2000 |
C:\mm.cfg |
Mac
OS X |
MacHD:Library:Application
Support:macromedia:mm.cfg |
The mm.cfg file
allows you to set the following variables:
TraceOutputFileEnable
-
The value can be 0 (don't write trace content to
a file) or 1 (write to a file).
TraceOutputFileName
-
The path to the file to which to write. If a
value isn't specified, then the content is written to flashlog.txt in the same directory as
mm.cfg.
ErrorReportingEnable
-
The value can be 0 (don't write errors to the
logfile) or 1 (write errors to the logfile). The default value is
0.
MaxWarnings
-
The maximum number of errors to write to the
logfile. If this value is set to 0, there is no limit. If a larger
value is specified, that limit is imposed and any errors beyond the
limit are not written to the log.
At a minimum mm.cfg must contain the following enable
writing to a file.
TraceOutputFileEnable=1
If you want to specify more than one variable, you should place
each on a new line, as
follows
TraceOutputFileEnable=1
TraceOutputFileName=C:\flex.log
|