How to get "printf" messages written in an NDK application?

if I define such a function in a java file

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

so i need to encode 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 where does this printf print this message? In logcate I do not understand?

How can I debug any NDK application by posting log messages?

+40
c android logging android-ndk jni java-native-interface
Apr 23 2018-12-12T00:
source share
3 answers

use __android_log_print() . You must include the header <android/log.h>

An example of an example. __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n this is log messge \n");

You can also use a 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 to the logging library in your Android.mk file:

  LOCAL_LDLIBS := -llog 

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

Easy approach

Add the following lines to the general 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) as printf in c .

Remember to include a shared header file.

Delete log

If you define LOGD(...) empty, all logs will disappear. Just a comment after LOGD(...) .

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

+94
Apr 23 '12 at 5:25
source share

There are two options:

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

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

Of course, this will require changing the entire source code that printf has.

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

+16
Apr 23 2018-12-12T00:
source share

no root device needed, acroding to http://developer.android.com/guide/developing/debugging/debugging-log.html#viewingStd , below may work prefect.

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

done!

+5
Jun 20 '14 at 2:35
source share



All Articles