views:

57

answers:

4

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().toLocaleTimeString() + ":" + myFrame + ": " + traceArgs.join(" "));
    }
}
A: 

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.

99miles
The visible symptom of the app failure is, the opening screen comes up blank. No error is shown or found (e.g. in system logs). When the calls to XTRACE are removed, the program works fine. Easier solution: When all the content of the XTRACE routine is commented out... then the ".air" installed program works fine.The error in XTRACE creates a stack trace, so the origin of the trace is known, e.g.Global.XTRACE("test for the moves data")produces:01:14:26 PM:com.muchoswing.cantoyo::ApplicationClass/handleButton(): test for the moves data
artie scie
Comment the lines out one by one and see what line breaks it
99miles
A: 

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(" "));
Fergal
A: 

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 ;-)

artie scie
add your extra info to the question, having it in an answer is weird!
grapefrukt
A: 

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.
Sean Fujiwara