package { public class Debug { private var _displayDebug:Boolean = false; private var _incomingClass:String = ""; private var _logArray:Array = []; //to stop all debugging set this to false private var _turnOffDebug:Boolean = false; /** * Create the debug object * @param display * @param fileName * */ public function Debug(display:Boolean, fileName:String): void { _displayDebug = display; _incomingClass = fileName; } /** * Display a trace message * @param msg * */ public function log(msg:String) : void { //override anything a debug passes in if(_turnOffDebug) { return; } if(!_displayDebug) { return; } var logMessage:String = "["+ new Date() +"]" + _incomingClass + ": " + msg; trace(logMessage); _logArray.push(logMessage); } } }
Using the class is simple. First create an instance of the class and tell if wither you want it on and what the name of the script you're calling it from is named.
var debugObj:Debug = new Debug(true, 'main.mxml');
To turn off debugging for the script just set the first parameter to false. To turn it off for the whole project change the class variable _turnOffDebug to true;
Use the log method to display your statement in the debug area of flash or flex.
debugObj.log('debug statement');
This is a what the trace statement should look like.
[Fri Sep 25 15:03:21 GMT-0700 2009]main.mxml: debug statement
If you have any feedback please send me a message.