In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

trace current method

note that throwing error costs a lot when compared to nomal logging.
Get Adobe Flash player
by 9re 07 Mar 2011
  • Related works: 1
  • Talk

    makc3d at 11 Mar 2011 15:06
    1st, you dont need to throw error to call getStackTrace, e.g.: (new Error).getStackTrace (); 2nd, it only works in debug player afaik
    9re at 13 Mar 2011 10:13
    for 1st, i didn't know 2nd absolutely! does anyone want function name anytime but when debugging?! this kind of log tool for debugging should be omit by the compiler options. or practically, private function traceNow (...msg:Array):void { CONFIG::debug { var at:String = ""; if (Capabilities.isDebugger) at = (new Error).getStackTrace ().split ('\n') [2]; traceField.appendText((msg ? msg.join(' ') : '') + at + '\n'); } } you can omit CONFIG::debug{~} part from the binary when set the flag false. <flex-config> <compiler> <define> <name>CONFIG::debug</name> <value>false</value> </define> </compiler> </flex-config> whatever, removing try{} catch{} part is excellent!!
    makc3d at 13 Mar 2011 15:59
    CONFIG::debug will not do the trick, because it depends on player build, not swf build.
    9re at 14 Mar 2011 08:32
    no. conditional compilation means the CONFIG::debug{} part is not included if set false. this is determined at compilation time not runtime. just as in C #ifdef DEBUG printf("debug trace"); #endif the whole #ifdef ~ #endif part is omit by the compiler at compile time when the constant DEBUG is not set http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html
    makc3d at 14 Mar 2011 15:16
    so what's the point? swf with CONFIG::debug set to true would still fail in non-debug player without Capabilities.isDebugger check
    9re at 15 Mar 2011 06:34
    just set false. it's ok. why you want to set it true? this kind of flag must be set false when you ship release build. i'd knew Capabilities.isDebugger~~ before you pointed out and i omitted the check because i thought it was pointless. method name itself is useless for release build this code is only for debug player.
    makc3d at 15 Mar 2011 14:14
    ah. but then you still have traceNow() calls everywhere; if you want clean release build, isn't it better to use trace() that are simply omitted everywhere in release build? like: CONFIG::debug private function trace (...msg:Array):void { ... }
    9re at 15 Mar 2011 14:34
    yeah, its of course better and a nice trick!! to tell the truth, i wrote this code as a reply for a man who was asking how to get the current method in as3. http://twitter.com/#!/bouze/status/44622388230496256 (japanese) so there are lot to be improved, to be more useful for a practical use. thanks anyway, for any kinds of recommendation!
    makc3d at 15 Mar 2011 16:32
    "to tell the truth" I would have never thought of overriding "trace" function if not for this chat. now I am going to add trace.as with global debug-only trace function everywhere :)
    Embed
/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lZ3B
 */

package {
    import flash.text.TextField;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        private var traceField:TextField;
        public function FlashTest() {
            traceField = new TextField;
            traceField.width = traceField.height = 465;
            addChild(traceField);
            methodA();
        }
        
        private function methodA():void {
            traceNow('1', 2);
            methodB();
        }
        
        private function methodB():void {
            traceNow(new Date);
        }
        
        private function traceNow(...msg:Array):void {
            try {
                throw new Error;
            } catch (e:Error) {
                traceField.appendText((msg ? msg.join(' ') : '') + e.getStackTrace().split('\n')[2] + '\n');
            }
        }
        
    }
}