I want to see the stack trace in any function of my code, so i made somthing like this to call it and print the stack trace:
public function PrintStackTrace(
I've put together this little function:
public static function getStackTrace() : String
{
var aStackTrace : Array = new Error().getStackTrace().split("\n");
aStackTrace.shift();
aStackTrace.shift();
return "Stack trace: \n" + aStackTrace.join("\n");
}
I have this function in a custom "Debug" class I use with my apps when developing. The two shift() calls remove the first two lines: The first one is just the string "Error" and the second line refers to this function itself, so it's not useful. You can even remove the third line if you wish (it refers to the line where you place the call to the getStackTrace() function) by adding another shift() call, but I left it to serve as a starting point of the "stack trace".
var tempError:Error = new Error();
var stackTrace:String = tempError.getStackTrace();
write this stackTrace
string into any file so that you can see the logs of your program at run mode also. So you need not to run it in debugger mode only. Write it into uncaughtexception
event of application, so it will execute lastly.
From Flash Player 11.5 the stack traces are also available in the non-debugger versions of the players as well.
As of Flash 11.5, stack traces work in the release version of Flash.
However, that doesn't mean this is no longer an issue. If your application is set to use a compiler older than 11.5 in Flash Builder --> Project properties --> ActionScript Compiler
, you won't have stack traces.
Additionally, on that same page you can see your AIR SDK version. If you're using v3.4 or older, you won't see stack traces. If this is your issue, all your developers should update their AIR SDK by following the instructions here.
Use the Flex DeBugger (FDB) that comes with the Flex SDK. It's a command-line debugger that allows you to debug .swf, even ones online (if it's a debug version). It allows you to set break-points, print/change variables, and dump the stack, and does not require you to add any extra code. A very useful tool that you shouldn't be without!
The fdb options you will need are 'break' and to specify the class and line where you want execution to halt, and 'bt' or 'info stack' to give you a backtrace of the stack. You can also display almost everything about the application while it runs.
@hasseg is right. You can also preserve the stacktrace information in release version (not debug) by providing the -compiler.verbose-stacktraces=true when compiling your SWF.