views:

1631

answers:

6

I have created a simple test application with the following code

var i : int; for (i=0; i<3000000; i++){ trace(i); }

When I run the application, it's very slow to load, which means the "trace" is running. I check the flash player by right-clicking, the debugger option is not enable.

So I wonder if there is an option to put in compiler to exclude the trace. Otherwise, I have to remove manually all the trace in the program.

Are there any other options of compiler to optimize the flex application in a maximum way?

Thanks

+2  A: 

You could do a find/replace on the entire project. search for 'trace(' and replace with '//trace('. That would be quick enough and easily undone.

kenneth
That's a good idea. But why flex doesn't provide compiler option for debug and release version?
maoanz
I'm not from Adobe so I dont know :)Its not good practice though to leave trace statements in your code anyway. They should always be removed before checking anything in, but ideals and reality are different things so for some devs it would be a nice feature to have.Personally I don't like trace, prefer to use breakpoints, very rarely have to use trace.
kenneth
+2  A: 

The mxmlc argument debug allows you to add or remove debug features from SWF files. The value of the debug argument is false by default for the command line compiler, but in Flex Builder, you have to manually create a non-debug SWF. According to the documentation on compiler arguments, debug information added to the SWF includes "line numbers and filenames of all the source files". There is no mention of trace() function calls, and I don't think there's a way to remove them through a compiler argument, but you're welcome to check the linked document for the entire list of available arguments.

joshtynjala
+1  A: 

There are two compiler options that you should set: -debug=false -optimize=true. In Flex Builder or Eclipse, look under Project->Properties->Flex Compiler and fill in the box labeled "Additional compiler arguments."

Mike Ivanov
+2  A: 

There is a really sweet feature built into Flex called the logging API (you can read more about it here http://livedocs.adobe.com/flex/3/html/logging%5F09.html).

Basically, you log (trace) things in a different way, admittedly with slightly more code than a standard trace, but it allows you much greater flexibility. This is an example:

import mx.logging.Log;
Log.getLogger("com.edibleCode.logDemo").info("This is some info");
Log.getLogger("com.edibleCode.logDemo").error("This is an error");

Then all you need to do is create a trace target in your main application file, something like:

<mx:TraceTarget id="logTarget" fieldSeparator=" - " includeCategory="true" includeLevel="true" includeTime="true">

      <mx:filters>
                <mx:Array>
                 <mx:String>*</mx:String>
                </mx:Array>
            </mx:filters>

            <!--
            0 = ALL, 2 = DEBUG, 4 = INFO, 6 = WARN, 8 = ERROR, 1000 = FATAL
            -->
            <mx:level>0</mx:level>

    </mx:TraceTarget>

And register the trace with:

Log.addTarget(logTarget);

This provides several benefits over the normal trace:

  • You can filter (turn off) traces to only see what you want:
    • Either by modifying the filters array
    • Or the level to show only error or fatal messages
  • You can replace the trace target with any other type of logging interface, e.g.
    • A TextField
    • A text file
edibleCode
+2  A: 

Use conditional compilation, more here.

In your code set:

CONFIG::debugging { 
    trace(i);
}

Then go to Project->Properties->Flex Compiler and add

-define=CONFIG::debugging,false
or
-define=CONFIG::debugging,true
zdmytriv
A: 

Go to your flex code base directory (and shut down Flex Builder if its running - it gets uppity if you change things while it's running). Run this to change all your trace statements. I recommend checking the tree into git or something first and then running a diff afterwards (or cp -r the tree to do a diff -r or something). The only major case this will mess up is if you have semicolons inside trace strings:

find . -name '*.as'   -exec perl -pe 'BEGIN{ undef $/; }s/trace([^;]*);/CONFIG::debugging { trace $1 ; };/smg;' -i {} \;

find . -name '*.mxml' -exec perl -pe 'BEGIN{ undef $/; }s/trace([^;]*);/CONFIG::debugging { trace $1 ; };/smg;' -i {} \;

Then set up the following in your Project->Properties->Flex Compiler->Additional compiler arguments:

-define=CONFIG::debugging,true -define=CONFIG::release,false

And use:

CONFIG::release { /* code */ } 

for the "#else" clause. This was the solution I picked after reading this question and answer set.

Also beware this:

if( foo )
{
    /*code*/
}
else
    CONFIG::debugging { trace("whoops no braces around else-clause"); };

I.e. if you have ONLY one of these in an if or else or whatever block, and its a naked block with no braces, then regardless of whether it's compiled out, it will complain.

eruciform
@maoanz: were any answers applicable for you?
eruciform