0

I am planning to add a DEBUG flag, and adding an if statement before every Log.d call

 public static final boolean DEBUG = FALSE;
 if (DEBUG)
 {
    Log.d(TAG,"Debug"); //dead code
 }

Will the dead code going to be removed in compile time, or will the if statement is going to be called on every time ?

meh
  • 22,090
  • 8
  • 48
  • 58
  • You might consider using an advanced logging library; or using ProGuard to remove the `Log` calls from your bytecode. See similar question: http://stackoverflow.com/questions/2446248/remove-all-debug-logging-calls-before-publishing-are-there-tools-to-do-this – tony19 Feb 04 '13 at 04:15
  • @user46874: the proguard seems to be useful, I will give it a try, thank you. – meh Feb 04 '13 at 07:35

3 Answers3

1

No, it increase the verbosity of the code, simply use a log library for this purpose, like Log4J for example ...

logger.debug("Here is some DEBUG");

In Android you can use android-logging-log4j ...

aleroot
  • 71,077
  • 30
  • 176
  • 213
1

It is not removed completely but there is next no performance impact due to branch prediction of the CPU.

Ideally you should use the logging level of the logger to determine whether the logger should be called.

Note: a simple method call like this has next to no overhead whether you check or not (and the library should check) Where you get a performance advantage is if you are doing something in the log message like

LOGGER.debug("The map is " + map);

In this case, the debug message will only be displayed if the debug is enable, however the string message will be build every time meaning this is more efficient.

if(LOGGER.isDebugEnabled())
    LOGGER.debug("The map is " + map);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Your debug code will always be executed (always the if). By the way, Android already has this type of flag: BuildConfig.DEBUG

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • What about the performance issues? are they noticeable if I have around 300-400 log calls? Or should I just comment out/delete the logs? – meh Feb 03 '13 at 11:11
  • If you use the BuildConfig.DEBUg flag I may even think it will be optimized by the compiler. This is because the piece of code in the If statement will never be executed. – RvdK Feb 03 '13 at 11:42