TwelvestoneFlash

Useful Class : Trace


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
mystic_juju
 
2008-12-10

So I wrote a class that I find very useful and thought I'd share it. Unless I am not seeing some very obvious native functionality, I think this is something that many can find quite useful. Feel free to comment/change it.

Basically I've always been very annoyed with the limits of the silly little "trace" statement. Very little to it. So I've come up with something that just makes a bit more sense to me. Basically you now send all your trace statements to a single function, meaning that you can control the behaviour of all your trace statements in one place (for example, you may want to write all your traces to a file or server or whatever for more detailed logging purposes). But more importantly i have implemented quite a hack-ish bit of functionality too: traces never give you line numbers, and there isn't a way to get at them, unless you encounter a runtime error. So what I've done here is to force an error, and through some string shenanigans extract the line number of the trace statement. Personally i think that is quite cool.

Anyway, here is the class: (use it by writing "Tracer.out(this, 'whatever you want to trace');", and deactivate all your traces by setting DEBUG_MODE to false. i know one can remove tracing from published files, but i like being able to do it in the code)

package com.rigardkruger.utils {

/**
 * ...
 * @author Rigard Kruger
 */
public class Tracer 
{

    public static var DEBUG_MODE:Boolean = true;

    public static function out(classRef:*, ... args) : void
    {

        if (!DEBUG_MODE) return;

        var classRefString:String = String(classRef);
        var len:int = classRefString.length;
        classRefString = classRefString.substring(8, len-1);

        var arr:Array;
        try
        {
            arr.push();
        }
        catch (e:Error)
        {
            var sourceLineNumber:String = String((String((e.getStackTrace().split("\n") as Array)[2]).split(":") as Array).pop()).slice(0, -1);
        }

        classRefString = "_" + classRefString + ":" + sourceLineNumber + "_";
        trace(classRefString + String("                             ").slice(classRefString.length) + args);
    }
}

}

It's fairly straightforward and one could probably expand on it a fair bit, but I reckon it's a handy thing to have around. And really, i hate a messy output panel.

JLM
 
2008-12-11

till I read this

So what I've done here is to force an error, and through some string shenanigans extract the line number of the trace statement

I had little interest, but will take a closer look now. Worth noting that haxe has an improved trace haxe trace I have not tried it yet.

mystic_juju
 
2008-12-11

yeah it was actually haxe's trace functionality that got me thinking about a way to do it in flash. haxe's way of doing it offers so much more - so it is nice to have something like this in flash.

Storm
 
2008-12-11

looks good and will bookmark this. Thanks

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

Useful Class : Trace