Fri Apr 30 00:57:05 2010
This is a topic I have long since intended to write about, but never got round to it. Well, anyway... trace statements in flex/flash are something that I would imagine we all use all the time, mostly for debugging purposes. There is a great little extension for the Firefox plugin FireBug called FlashBug, which allows trace statement output directly from within the browser. It is also possible to view this output via Terminal on OSX/Linux and command prompt on Windows. However, there are a few things that need to be set up.
First create a file call "mm.cfg" and place it in the following location:
On OSX: /Library/Application Support/Macromedia/mm.cfg
On Win: C:Documents and Settingsusernamemm.cfg
On Linux: home/username/mm.cfg
Next open the mm.cfg file and type the following inside.
ErrorReportingEnable=1
TraceOutputFileEnable=1
It may be obvious, but ErrorReportingEnable controls the ability to view error output and TraceOutputFileEnable controls the ability to view trace output. Setting each of these values to 1 turns these properties on. If, for example, you didn't want to view error output, simply turn the 1 to a 0, or delete the line ErrorReportingEnable.
Anyway, moving on. Create an ActionScript file that uses trace statements. I've pasted an example below for convenience. For test purposes, just save it as Main.as.
package{
import flash.display.*;
public class Main extends MovieClip{
public function Main(){
trace("Trace output is working");
}
}
}
Next we need to compile it. If you aren't sure how to do this, you can take a look at my other post: Compile AS files with Flex SDK, which gives a more detailed "how to", or simply use Terminal/Command Prompt to open up fsch found inside the bin folder of the flex sdk (which you should already have), and type the following, obviously swapping in the location and file names where necessary.
mxmlc -o=output_location/your_filename.swf -file-specs=your_file_location/Main.as
This should compile the Main.as file and you should be left with a .swf file saved at the location you entered. Before you open this file, you will need to install the flash debug player from the Adobe website. You will find it under Debugger Versions. Once you have that installed run the swf file. A file called flashlog.txt containing any errors and/or trace output will be created in the following location:
On OSX: /Users/username/Library/Preferences/Macromedia/Flash Player/Logs/flashlog.txt
On Win: C:Documents and SettingsusernameApplication DataMacromediaFlash PlayerLogsflashlog.txt
On Linux: home/username/Macromedia/Flash_Player/Logs/flashlog.txt
To view this information in real time simply use FlashBug with Firefox or type the follow into Terminal
tail -f /Users/{username}/Library/Preferences/Macromedia/"Flash Player"/Logs/flashlog.txt
For added convenience to anyone using TextMate, go to Bundles > Bundles Editor > Show Bundle Editor and click on the little plus sign on the bottom left hand side of the bundle editor window. Choose New Command and name it whatever you want. I named it Tail Output. Make sure Save:Nothing, Input:None and Output:None are selected. Next enter the following content into the Command(s) window changing {username} to the name of your home folder on OSX (This is just a little bash program I wrote to tell Terminal to tail the FlashLog.txt file).
Note* tail -f and /User/{usersname}... should be on the same line.
#!/bin/bash
aplscr="tell application "System Events" to set terminalRunning to (exists process "Terminal")
tell application "Terminal" to activate
tell application "Terminal" to do script "tail -f
/Users/{username}/Library/Preferences/Macromedia/'Flash Player'/Logs/flashlog.txt" in front window"
osascript -e "$aplscr" &>/dev/null &
Next set Activation: Key Equivalent, and enter whatever shortcut you want to activate the command in Terminal. I chose ⌘T (T for tail). For the Scope Selector, enter source.actionscript.3.
After all that's done, it's probably best to quit and restart TextMate.
Now all you have to do to start viewing the errors and trace out is press your Key Trigger in TextMate while editing an ActionScript file and Terminal should start up and read out all the trace statements.
And that's it! By this point, if you've done it right, you should be getting readout on Errors and Trace statements and all should be well.