Is "Std::Cout" Usable in Android-Ndk

Is std::cout usable in Android-ndk

According to the Android documentation, stdout & stderr output to /dev/null. You can use the Android Debug Bridge to achieve what you want.

By default, the Android system sends stdout and stderr (System.out and System.err) output to /dev/null. In processes that run the Dalvik VM, you can have the system write a copy of the output to the log file. In this case, the system writes the messages to the log using the log tags stdout and stderr, both with priority I.
To route the output in this way, you stop a running emulator/device instance and then use the shell command setprop to enable the redirection of output. Here's how you do it:

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

The system retains this setting until you terminate the emulator/device instance. To use the setting as a default on the emulator/device instance, you can add an entry to /data/local.prop on the device.

C/C++ printfs - Where's it appears in a Android native code?

Log to logcat.

1) To invoke the logger in native code include the header and call _android_log_write(..).

#include <android/log.h>

__android_log_write(ANDROID_LOG_INFO, "tag here", "message here");

2) In your Android.mk file include the log lib like this.

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 

How to get console output of log lines (printf, cout ,etc...) of c++ library used in Android app via JNI/NDK

I use a logger header to print crossplatform logs.

In c++ code just write LOGD("msg"); or LOGE("msg"); and print messages checking the platform.

Try creating a crossplatform log header like:

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

Capturing stdout/stderr with NDK

stdout is path 1 and stderr is path 2. Knowing this, you can establish new path(s) that you want to be the output destination, then coerce them into stdout and/or stderr. There's an example showing how to do this at practical examples use dup or dup2.

Android NDK Native LIB, What to do about existing stdio?

By default stdout and stderr are sent to /dev/null (nowhere) for android apps.

You can use adb setprop to set log.redirect-stdio to true, or put "log.redirect-stdio=true" in /data/local.prop (which you may need root access to create, but it's more reliable). Doing this will send their output to the logcat log.

See "Viewing stdout and stderr": http://developer.android.com/guide/developing/tools/adb.html

Rust stdout not printing in Java application

For Android you have to use logcat instead of stdout (more details here and there) . The one possible way is to use android_logger or any other crate which provides logcat support.

You can found more information about stdout redirection there.



Related Topics



Leave a reply



Submit