7

I know using proguard you can remove java Log.d debug statements from release versions https://stackoverflow.com/a/13327603/1527440

But is there way to remove log debug statements from NDK C/C++ code.

I am using a define statement to call them in NDK

#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) 
Community
  • 1
  • 1
pt123
  • 2,146
  • 1
  • 32
  • 57

1 Answers1

11

Use the NDEBUG macro. Or you could really #ifdef on anything.

#ifdef NDEBUG

#define LOGD(...)

#else

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

#endif

Then for the code:

void f()
{
    LOGD("About to do some stuff");
    doSomeStuff();
}

The compiler will see (roughly - ANDROID_LOG_DEBUG and LOG_TAG will also be replaced with their values if they are macros):

void f()
{
    __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,"About to do some stuff");
    doSomeStuff();
}

when NDEBUG is undefined and will see:

void f()
{
    ;
    doSomeStuff();
}

when NDEBUG is defined.

Joel
  • 1,135
  • 6
  • 9
  • But with NDEBUG the call to the LOGD would be still in the binary, leaving the message string of the call to be extracted by potential hackers. Is there a more cleaner way of using #ifdef than surrounding every call to log D? thanks – pt123 Jul 30 '13 at 04:41
  • `LOGD` is a macro, not a function. The preprocessor will remove it before the compiler ever sees it. It will be as if the call didn't happen at all. Also, you should not surround every call to `LOGD`. That's the *definition* of `LOGD`, so you only have to do it once for any given definition of `LOGD`. – Joel Jul 30 '13 at 05:21
  • 1
    More to the point, the preprocessor is the *only* way of eliminating things from the final binary. Anything based in the language will store your string literals in static storage. The method I've shown removes the strings altogether from your source code. The compiler won't include in the binary what it never sees. And `NDEBUG` is the basis of `assert`. Assertions are removed from release builds. – Joel Jul 30 '13 at 13:17
  • 1
    And where do you define NDEBUG true false? – powder366 Nov 29 '13 at 18:12
  • 1
    NDK-Build defines `NDEBUG` to 1 unless you compile with `APP_OPTIM` being `debug`. – Triang3l Jan 31 '15 at 20:06