How to get “printf” messages written in NDK application?

if i am defining such function in java file

  /** 
   * Adds two integers, returning their sum
   */
  public native int add( int v1, int v2 );

so i need to code in c file

  • Fragment's onOptionsItemSelected doesn't get called
  • Get both sim numbers in a dual sim android phone
  • Create an (repetitive high pitch) Alarm on a remote trigger when App is not running (iphone/android) just like Find My iPhone
  • Android ListView selected item stay highlighted
  • Dagger example built through eclipse fails with 'Please ensure that code generation was run for this module.'
  • Sugar ORM just won't create tables
  • JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
      (JNIEnv * env, jobject obj, jint value1, jint value2) {
    
      printf("\n this is log messge \n");
    
            return (value1 + value2);
    }
    

    then from where this printf will print it message ?
    In logcate i dont get it?

    How can i debug any NDK application by putting log messages?

  • Unity3D and Android Studio Integration
  • Relative Layout alignParentLeft vs alignParentStart
  • Re-installation failed due to different application signatures
  • Android Studio Unable to run AVD
  • Using FloatMath or Math and a cast?
  • TabWidget onClick after onSaveInstanceState (java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState)
  • 3 Solutions collect form web for “How to get “printf” messages written in NDK application?”

    use __android_log_print() instead. You have to include header <android/log.h>

    Sample Example. __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n this is log messge \n");

    You can also use format specifier like printf –

    __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "Need to print : %d %s",int_var, str_var);
    

    Make sure you also link against the logging library, in your Android.mk file:

      LOCAL_LDLIBS := -llog
    

    Ohh.. forgot .. The output will be shown in Logcat with tag LOG_TAG

    Easy Approach

    Add the following lines to your common header file.

    #include <android/log.h>
    
    #define  LOG_TAG    "your-log-tag"
    
    #define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
    #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
    // If you want you can add other log definition for info, warning etc
    

    Now just call LOGD("Hello world") or LOGE("Number = %d", any_int) like printf in c.

    Don’t forget to include the common header file.

    Remove the logging

    If you define LOGD(...) empty, all logs will be gone. Just comment after LOGD(...).

    #define LOGD(...) // __android_log..... rest of the code

    There is two options:

    1) replace printf with __android_log_print. You can do this easily with define at beginning of code:

    #define printf(...) __android_log_print(ANDROID_LOG_DEBUG, "TAG", __VA_ARGS__);
    

    Of course this will require changing all source code that has printf.

    2) redirect stdout and stderr to Android logcat (not sure if this will work on non-rooted device): http://developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd

    not need to root device, acroding to http://developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd, below can work prefectly.

    $ adb shell 
    
    $ su 
    
    $ stop
    
    $ setprop log.redirect-stdio true
    
    $ start
    

    done!

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.