Useful Flash Tracer Class
Let’s be honest, Flash’s trace functionality is pretty substandard. It provides no useful information besides what you’ve asked it to output. Unlike something like HaXe, you cannot extend it or extract anything from it that really helps you debug your code and when you’ve got a lot of traces in your application or project, it becomes messy and incredibly hard to manage. A while back I created and we’ve since been using a utility class that makes tracing much more user friendly and provides some additional information and functionality that would seem obvious to have in the first place. Additionally, it was important to me to have something that is lightweight and easy to use, with no need to install anything extra – just a class/swc for inclusion in your project, and no additional dependencies.
Firstly, you are able to toggle all of your traces on or off in a single line of code – no need for additional compiler settings. Secondly, the traces that are output include both the name of the class that has made the trace call as well as the line number it happened. This is hugely beneficial, particularly when you are working on a big project with a lot of traces dotted throughout your code. You’ll immediately be able to distinguish your different trace statements and can head to the exact line that you made the call, and if you want to remove the trace statement, you know exactly where to find it. Third, it provides the option of adding in a timestamp to your traces. This aids in keeping track of when things happen and what amount of time has elapsed between different calls to the trace function. Lastly, the traces can be toggled on and off at runtime, which can be very useful when you need to deploy a debug version to a live environment, but don’t want your traces showing up to inspectors like Flash Tracer and the like, unless you’ve specified it to do so.
The class is very easy to use. To initialise it, simply call its static function “init” (most likely in your document or preloader class), and specify whether you want traces on/off, an optional timestamp flag, and optionally set the stage for keyboard capture:
Tracer.init(true, true, stage);
After this, you can just make calls to the static method ‘out’ to output trace statements:
Tracer.out("This is a trace");
If you have specified the third parameter, stage, in the init function, you can toggle the debug mode on and off by pressing the keys of the word “debug”.
Source code and SWC can be got here.
