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.
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.
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.
looks good and will bookmark this. Thanks