How to get console output of log lines (printf, cout, etc.) of the C ++ library used in an Android application through JNI / NDK

in my android application, I use my own C ++ library through JNI. This library creates log lines if it is built in debug mode. I want the log lines to be redirected to logcat.

So, I created lib in debug mode, NDK_DEBUG=1and is used LOCAL_LDLIBS += -llog.

My devices are not deployed, but I installed:

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

As described in http://developer.android.com/tools/debugging/debugging-log.html#viewingStd and here Is "std :: cout"; can be used in android ndk

The use __android_log_print(ANDROID_LOG_INFO, "foo", "Error: %s", foobar);works, but this is not an option for me, because C ++ lib is also used for the iOS application, so I do not want to change my own code.

I also tried to get the console output (printf) generated in the JNI shell with these settings, but besides the __android_log_print statements, the output also does not distort the logarithmic file.

Am I missing or redirecting something only for root devices?

How can I get console output generated by native code.

Thank you at Advance

+4
source share
2 answers

I use the magazine title to print cross-platform magazines.

++ LOGD ( "msg" ); LOGE ( "msg" ); , .

, :

Logs.h

#       ifdef ANDROID
            // LOGS ANDROID
#           include <android/log.h>
#           define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, 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__)
#           define LOGW(...) __android_log_print(ANDROID_LOG_WARN   , LOG_TAG,__VA_ARGS__)
#           define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , LOG_TAG,__VA_ARGS__)
#           define LOGSIMPLE(...)
#       else
            // LOGS NO ANDROID
#           include <stdio.h>
#           define LOGV(...) printf("  ");printf(__VA_ARGS__); printf("\t -  <%s> \n", LOG_TAG);
#           define LOGD(...) printf("  ");printf(__VA_ARGS__); printf("\t -  <%s> \n", LOG_TAG);
#           define LOGI(...) printf("  ");printf(__VA_ARGS__); printf("\t -  <%s> \n", LOG_TAG);
#           define LOGW(...) printf("  * Warning: "); printf(__VA_ARGS__); printf("\t -  <%s> \n", LOG_TAG);
#           define LOGE(...) printf("  *** Error:  ");printf(__VA_ARGS__); printf("\t -  <%s> \n", LOG_TAG);
#           define LOGSIMPLE(...) printf(" ");printf(__VA_ARGS__);
#       endif // ANDROID
+10

, adb shell stop/start . , zygote , log.redirect-stdio. - - , , stdout/stderr .

-, __android_log_print , iOS, (, __ANDROID__). .

+3

Source: https://habr.com/ru/post/1545355/


All Articles