From Adobe, this is the description of what the function trace does:
Displays expressions, or writes to log files, while debugging.We can trace anything anywhere in our code, however, as mentioned before, how do we know where? we could then specify this in each trace call, for example:
trace('MyClass - ' + someValueToTrace);Right, instead of writing the object's name in every call, we could have a private function to do this:
trace('MyClass - ' + someOtherValue);
private function debug(objectToTrace : Object) : voidThen, instead of calling 'trace(blah)' we use 'debug(blah)' which will include the class name. For example:
{
trace('MyClass' + objectToTrace);
}
debug(someValue);Advantages? well, if in case we don't want to trace anything any more, we just comment out the 'trace' line in our code rather than deleting every single call to debug in that class...
// outputs: 'MyClass - someValue'
Cool! now we know where are we using 'trace'.
Can we extend it? sure thing! and this is my solution:
- Create a class in your utils package, name it Debug.as.
- Create a static function to trace the name of the caller plus any parameter.Code:
package com.mysite.core.utilsIf we don't use the second function (getClassName), the output would be '[object MyClass]' so that's why we slice a string to get only the very class name.
{
import flash.utils.getQualifiedClassName;
public class Debug
{
/**
* // Writes in the flashlog the name of the object calling this function and any number of parameters
* Debug.print(this, someValue, someOtherValue);
*/
public static function write(caller : Object, ...arguments) : void
{
var objectString : String = getClassName(caller);
trace(objectString + ' - ' + arguments);
}
private static function getClassName(object : Object) : String
{
var className : String = getQualifiedClassName(object);
return className.slice(className.lastIndexOf('::'));
}
}
}
Now you can call Debug.write instead of trace in the debug function on every class:
private function debug(objectToTrace : Object) : voidSweet! :)
{
Debug.write(this, objectToTrace);
}
// use debug(objectToTrace) instead of trace(objectToTrace)