Debugging Tricks
FlexBuilder 3 Beta 2 no longer makes both release and debug versions on every build, in order to speed up build times. This means that the debug version is no longer suffixed with -debug and it appears more than one of you used that to turn on debugging functionality w/o changing your code.
Also, a few folks have asked for a single line of code that could be pasted verbatim into function bodies that would report the name of the function, and a few others have wondered if there is a way to get the name of the calling function.
Assuming you are using a debugger player, the source code at this link demonstrates how to do each of these things. See setDebugFlag(), getFunctionName() and getCallingFunctionName().
private function setDebugFlag():void
{
var e:Error = new Error();
var s:String = e.getStackTrace();
// trace(s);
var i:int = s.indexOf("setDebugFlag");
if (s.charAt(i + 14) == '[')
debugMode = true;
}
[Bindable]
public var debugMode:Boolean = false;
private function getFunctionName(e:Error):String
{
var s:String = e.getStackTrace();
var i:int = s.indexOf("at ");
var j:int = s.indexOf("()");
return s.substring(i + 3, j);
}
private function getCallingFunctionName(e:Error):String
{
var s:String = e.getStackTrace();
// trace(s);
var i:int = s.indexOf("at ");
i = s.indexOf("at ", i + 3);
if (i == -1)
return "caller unknown";
var j:int = s.indexOf("()", i + 3);
return s.substring(i + 3, j);
}
where the latter two functions are can be used like this:
private function doSomething():void
{
trace(getFunctionName(new Error()));
doit();
}
private function doSomethingElse():void
{
trace(getFunctionName(new Error()));
doit();
}
private function doit():void
{
trace(getFunctionName(new Error()));
trace(" called by", getCallingFunctionName(new Error()));
}
Usual caveats apply, and the numbers and strings might have to be adjusted for different languages.
Comments
I just wonder why you look at stacktrace instead of using flash.system.Capabilities.isDebugger, in order to detect user's player edition.. Any good reason?
-------------
isDebugger just tells you if the player has the ability to stop at breakpoints, report unhandled exception, etc. It doesn't tell you whether the swf has debug information like line numbers compiled in, which seemed to be what folks wanted to know
Posted by: tksh | October 6, 2007 05:20 AM
Are there any smarter way?
Can I know which debug session is active or not?
-------------------------
Not sure I understand the question. What do you mean by debug session? When would you have more than one?
Posted by: Shigeru | October 8, 2007 02:19 AM
I meant that debug-SWF is connecting to FlexBuilder debugger.
---------------
I can't think of a general way to know you've been connected to the debugger. The debugger tries to have a s little impact as possible. If you hit a breakpoint, the debugger will call all of the getters on the 'this' at the breakpoint, so I suppose you could always hit a particular breakpoint early in the app which would call the getter which could set the flag for everyone else to use.
Posted by: Shigeru | October 15, 2007 03:55 PM
It may be worth noting that in order to get this to work in production... you need to turn on the -verbose-stacktraces=true compiler option.
--------------------
True, but that'll fatten your swf in a significant way.
Posted by: Clint Modien | January 15, 2008 08:09 PM
Many thanks for the usefully Informations.
Posted by: Webdesign | February 12, 2008 12:50 PM