Logging from C/C++ on Android Studio

The easiest form of logging in C/C++ you can think of is using printf().

But as you’re using C/C++ for NDK, this won’t be useful in that.

 

Anyway, solution is like this:

  1. adding the logging library to Android.mk file:
    include $(CLEAR_VARS)
    ...
    LOCAL_LDLIBS  := -llog
    ...
    include $(BUILD_SHARED_LIBRARY)
  2. Add log ld library to the build.gradle

    android {

    compileSdkVersion 23

    buildToolsVersion “23.0.1”

    defaultConfig {

    applicationId “com.test.testproj”

    minSdkVersion 15

    targetSdkVersion 23

    versionCode 1

    versionName “1.0.0”

    ndk {

    moduleName “testmodule”

    ldLibs “log”

    }

    }

    }

    (undefined reference to `__android_log_print’ error will be occured when you didn’t do this step… 😦  )

  3. Add header file to the source file that you need to show the log:
    #include <android/log.h>
    
    #define  LOG_TAG    "CKDLEE"
    
    #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
    #define  LOGW(...)  __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
    #define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
    #define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
  4. Write log in the terminal:
    LOGD("rSegment.pathId: %d\n", rSegment.pathId);

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s