Hi Lists,
I was looking for a way to trace the name of the current function or the
calling function and I came up with a hackish bit of code.
Don't know if anyone already came up with something like this but anyway, I
thought it was pretty cool so I'd like to share it with you.
I'm still working on making it more general, and you should not use it in a
production enviroment (unless the compiler directive is set to false)
package nl.trimm.lang
{
/**
* @author JC Wichman
*/
public class DebugUtil
{
public static function getCallingInfo(pInfo:String = "", pFull:Boolean =
false):String {
return getInfo (pInfo, pFull, 2);
}
public static function traceCallingInfo(pInfo:String = "", pFull:Boolean =
false):void {
trace (getCallingInfo (pInfo, pFull));
}
public static function getCallerInfo(pInfo:String = "", pFull:Boolean =
false):String {
return getInfo(pInfo, pFull, 3)
}
public static function traceCallerInfo(pInfo:String = "", pFull:Boolean =
false):void {
trace (getCallerInfo (pInfo, pFull));
}
private static function getInfo (pInfo:String = "", pFull:Boolean = false,
pIndex:Number = 0):String {
CONFIG::EXTENDED_INFO_ON {
try {
throw new Error();
} catch (e:Error) {
var lTrace:String = e.getStackTrace().split("\tat ")[pIndex+1];
lTrace = pFull?lTrace:lTrace.split("[")[0];
return (lTrace + "->" + pInfo);
}
}
return pInfo;
}
}
}
Usage:
- define a compiler directive and set it to true or false (can be optimized)
Example:
public class Test extends Sprite
{
public function Test():void
{
init();
}
private function init():void
{
trace (DebugUtil.getCallerInfo("here", true));
trace (DebugUtil.getCallingInfo("here", true));
}
}
Prints:
Test()[D:\MY_DATA\checkouts\co_as3_library\\src\Test.as:21]->here
Test/init()[D:\MY_DATA\checkouts\co_as3_library\src\Test.as:42]->here
If you use DebugUtil.getCallingInfo("here", false) the source line is
omited.
In the next version that will be a global flag as well.
I'm not sure what happens in other languages, you might need to replace \tat
with \t
Have fun!
JC
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders