IamDeveloper
IamDeveloper

Reputation: 5236

How to tell if Flex is run in debug mode?

I want to declaratively turn on and off logger for my Flex application and it seems that my usual way of determining if it's debug mode or not works only sometimes. The code for it:

isDebugSWF = new Error().getStackTrace().search(/:[0-9]+]$/m) > -1;

Do you know a better way for it?

Edit: I posted answer below.

Upvotes: 1

Views: 1205

Answers (4)

IamDeveloper
IamDeveloper

Reputation: 5236

The previous approach was based on great article . One of the comments suggested that in Release the stacktrace is null, so I modified it properly:

protected function configureLogger() : void
{
    if(!isDebugPlayer()|| !isDebugBuild())
    {
        // stop logging
        Logger.hide = true;
    }
    else
    {
        // resume logging
        Logger.hide = false;                    
    }
}

private function isDebugPlayer() : Boolean
{
    return Capabilities.isDebugger;
}

/**
* Returns true if the swf is built in debug mode
**/
private function isDebugBuild() : Boolean
{
    var stackTrace:String = new Error().getStackTrace();
    if(stackTrace == null || stackTrace.length == 0)
    {
        //in Release the stackTrace is null
        return false;
    }
    //The code searches for line numbers of errors in StackTrace result. 
    //Only StackTrace result of a debug SWF file contains line numbers.
    return stackTrace.search(/:[0-9]+]$/m) > -1;
}

This way I can finally configure ThunderBoltAS3 logger depending on current build type.

Upvotes: 3

user264541
user264541

Reputation:

The static property Capabilities.isDebugger in the flash.system.Capabilities class specifies if the Flash player installed is a debug version or not. It requires Flash Player 9 or AIR 1.0 as the minimum version.

Keep in mind though that debug Flash Player installers are publicly available, and that there is nothing stopping users from installing the debug version of Flash. If have something sensitive that you want to hide, using conditional compilation would be a better option.

Upvotes: 3

grass
grass

Reputation: 176

We are using conditional compilation at our project : Adobe Documentation

Upvotes: 0

M.D.
M.D.

Reputation: 1896

Looks like you are looking for the same, as answered in another post. Look here: How can you tell programmatically if a Flex App is running in debug mode?

Upvotes: 0

Related Questions