Artemix
Artemix

Reputation: 8655

Making a Debug class

I saw in some projects that ppl has their own "Debug class".

So, instead of typing: trace("look at this!") you type Debug.trace("look at this!").

The only advantaje I saw was that you can disable every single trace call with a single parameter in the Debug class.. but, thats all.

My question is, what advantages can I obtain if I use a Debug class in AS3?

Upvotes: 3

Views: 219

Answers (2)

cwallenpoole
cwallenpoole

Reputation: 82028

I actually don't use my debug class that way. I have a logging framework for that, that's what logging is for. My Debug class has things like

// I usually use these for debugging and it avoids the need of an 
// additional import.
function whatIs( obj:* ):String{ return getQualifiedClassName( obj )}
function describe( obj:* ):XML{ return describeType( obj ); }

I also have a getLines method -- it returns long Strings so that I can easily look at the logging traces and see specific points.

But the most important one:

function getStack():String {
    try
    {
        throw new Error( "Someone set us up the bomb!" );
    }
    catch( e:Error )
    {
         return e.getStackTrace();
    }
}

I even have a wrapper around getStack which returns the class and method which was most recently called before the method which called getStack().

Upvotes: 3

mpdonadio
mpdonadio

Reputation: 2941

A debug class lets you do different levels (notice, warning, error, critical, apocalypse, etc). As you mentioned, you can disable in a single place. You can timestamp messages. You can send output to different places, which can be super handy for debugging a live application or debugging something that happens infrequently. You can also have some logic to detect the argument types and do different things with the debug info.

Upvotes: 1

Related Questions