54

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

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?

Brian S
  • 3,096
  • 37
  • 55
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • 1
    `printf(3)` will write to whatever is _standard output_ -- which may or may not exist in your environment. Better would be to find a supported logging mechanism. – sarnold Apr 23 '12 at 04:52

3 Answers3

117

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

Shaiful
  • 5,643
  • 5
  • 38
  • 41
  • How do I remove the logging? – powder366 Nov 29 '13 at 18:00
  • 5
    Just in case someone (e.g. me) using Android Studio (current mine is 0.8.11), you put ldLibs "log" inside 'ndk' tag. This solves undefined reference to '__android_log_print' issue. – Yang Oct 05 '14 at 07:49
  • 2
    As of Android Studio 1.4 Gradle 2.5 the LOCAL_LDLIBS := -llog has moved from Android.mk to build.gradle - use 'android.ndk { moduleName = "Your-jni" ldLibs += "log" }' NOTE that .gradle syntax has changed in 2.5 (you need the '+=') – Lorne K Oct 02 '15 at 23:44
  • That doesn't help for Visual Studio Android debugging – Michael IV Jun 19 '17 at 13:09
  • This helped so long as I had this lib included in my build. This doc helped with that https://developer.android.com/studio/projects/configure-cmake#add-ndk-api – caitcoo0odes Jan 24 '23 at 18:14
17

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

Mārtiņš Možeiko
  • 12,733
  • 2
  • 45
  • 45
  • when i use option 2 then after applying that 3 command when i run my application emulator just hang up in showing android bootanimation.. – Jeegar Patel Apr 23 '12 at 06:11
8

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!

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
user2513099
  • 81
  • 1
  • 2
  • 3
    This method only works on android versions running dalvik (4.4 or earlier), ART versions (5.0 or later) dont support log.redirect-stdio. Source: https://code.google.com/p/android/issues/detail?id=165602 – Deemoe Mar 22 '16 at 21:01