StackTrace in Flash / ActionScript 3.0

醉酒当歌 提交于 2019-12-17 06:13:01

问题


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() {
    try {
        throw new Error('StackTrace');
    } catch (e:Error) {
        trace(e.getStackTrace());
    }
}

I like to know if there are other way to do this. In some place, the Error class creates the stack trace, but maybe it didn't do it with ActionScript 3.0 so maybe it's not posible, but i want to know.

Thanks!


回答1:


As far as I know, the only way to make the stack trace available to your own code is via the getStackTrace() method in the Error class, just like you're already doing. In response to the example in your question, though, I would mention that you don't actually have to throw the Error -- you can just create it and call the method on it:

var tempError:Error = new Error();
var stackTrace:String = tempError.getStackTrace();

Also, like the documentation says, this only works in the debug version of Flash Player, so you can wrap this functionality in an if-block that checks the value of Capabilities.isDebugger if you want.




回答2:


From Flash Player 11.5 the stack traces are also available in the non-debugger versions of the players as well.




回答3:


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.




回答4:


@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.




回答5:


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".




回答6:


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.




回答7:


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.




回答8:


The getStackTrace method returns the stack trace only on the debug flash player (https://www.adobe.com/support/flashplayer/debug_downloads.html), on the release player returns null. Make sure you have the debug player installed and running.

The -compiler.verbose-stacktraces=true only adds the line number to the debug stack trace.

Sample test: https://gist.github.com/pipeno/03310d3d3cae61460ac6c590c4f355ed



来源:https://stackoverflow.com/questions/149073/stacktrace-in-flash-actionscript-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!