开发者

Adobe Air, packaged install fails with my trace routine... how come?

开发者 https://www.devze.com 2022-12-25 20:00 出处:网络
I cobbled together some code from here and there for a trace I like... it generates an error to get a stack trace and picks out the traced routine name, I like that detail in the trace log.

I cobbled together some code from here and there for a trace I like... it generates an error to get a stack trace and picks out the traced routine name, I like that detail in the trace log.

Problem: it fails in an installed AIR file. I wonder why? I don't expect it to do anything as is... just, I'd prefer it not cause the program to fail!

tx artie

enter code here    

static public function XTRACE( ... traceArgs ):void {
    try {
        throw new Error();  // make a stack
    } catch (e:Error) {
        var stack:String = e.getStackTrace();
        var frames:Array = stack.split("\n");
        var myFrame:String = String(frames[2]);
        myFrame = myFrame.replace("\t", "");

        // "at " can be followed by some part of the package
        // you don't want to see. E.g., if your code is all in
        // com.foo.bar, you can put "at com.foo.bar." so as not
        // to crowd the display
        myFrame = myFrame.substr("at ".length);
        myFrame = myFrame.substring(0, myFrame.indexOf("["));
        var now:Date = new Date();
        trace(new Date().toLocaleT开发者_开发百科imeString() + ":" + myFrame + ": " + traceArgs.join(" "));
    }
}


In what way is your app failing?

1) Trace routines are for debugging, so your trace won't do anything in an installed app.

2) I'm not sure when you call this routine, but it seems weird that you have a routine that only throws an error. I think in this code the 'catch' is only going to get entered if there's an error throwing the error. Normally you would try to perform some useful action, and catch errors when something goes wrong.


Within the trace function your attempting to invoke the Date().toLocaleTimeString() statically before it becomes instantiated by the new keyword. Try the following instead:

trace((new Date()).toLocaleTimeString() + ":" + myFrame + ": " + traceArgs.join(" "));


thanks for your input Fergal. The XTRACE function works fine running with the debug player, and fails only when running with the release player. So I assume the code line I use must associate values in the right order... I settled on using a function I didn't know about before:

enter code here
static public function XTRACE( ... traceArgs ):void {
    if ( Capabilities.isDebugger ) {     

With that, XTRACE does nothing unless it is executing in a debug environment. So it works around the issue. I'll still use your brackets though, I like to make order of association obvious too ;-)


I realize you've probably grown old and forgot what flash is since you asked this question. But, you're getting an NPE because e.getStackTrace() returns null in the release player.

A couple other things:

  1. You don't need to throw the error to get a stack trace; new Error().getStackTrace() works fine.
  2. Since this is debug code, and the calling code probably isn't expecting errors, you should wrap the whole thing in a try catch.
  3. The compiler won't resolve 'at '.length, so it will be called every time at runtime. Unless you're really paranoid, you can just hard code it to 3.
  4. Both the substrs can be combined into 1
  5. The now variable isn't used.
0

精彩评论

暂无评论...
验证码 换一张
取 消