Any way to redirect stdout (in c/cpp) to logcat without android/log.h?

2,971 views
Skip to first unread message

Edward Chow

unread,
Mar 1, 2019, 11:33:59 AM3/1/19
to android-ndk
hi, i'm currently making changes in a c++ library(like v8 with complicated code structure) and run on android over jni. My trouble is how to see the outputs of stdout comes from std::cout or printf. because i don't want to use android/log in such a huge library.

Chris Browet

unread,
Mar 2, 2019, 10:48:38 AM3/2/19
to android-ndk

Andrew Esh

unread,
Mar 4, 2019, 8:48:20 AM3/4/19
to android-ndk
This will allow any pre-existing stdout output to reach logcat:
adb root
adb shell stop
adb shell setprop log.redirect-stdio true
adb shell start

https://stackoverflow.com/questions/10531050/redirect-stdout-to-logcat-in-android-ndk

Edward Chow

unread,
Mar 4, 2019, 11:01:59 AM3/4/19
to android-ndk
it simply redirect stdout to logcat using android/log.h (see the row __android_log_write(ANDROID_LOG_DEBUG, tag, buf);) which is not my purpose.

在 2019年3月2日星期六 UTC+8下午11:48:38,Chris Browet写道:

Amit Agrawal

unread,
Mar 5, 2019, 10:54:07 AM3/5/19
to android-ndk
This doesn't work for me. Doing this disconnects from the adb device. Correct me if I'm missing something. Someone mentioned that "This method only works on android versions running Dalvik (4.4 or earlier), ART versions (5.0 or later) don't support log.redirect-stdio." check this -> https://issuetracker.google.com/issues/37038424

Chris Browet

unread,
Mar 5, 2019, 10:54:07 AM3/5/19
to android-ndk
Sure, but you only have to put this code in the initialization of your lib, not all over the place.
If you use a NativeActivity, you don't even have to touch the lib. The code is meant to be executed once for the process.

But yeah, if you don't want any log.h anywhere, then I guess Andrew's solution is the way to go.

Alex Cohn

unread,
Mar 6, 2019, 5:19:20 AM3/6/19
to android-ndk
What's wrong with this approach? It does not touch the library you are porting, the change is on the level of the Android-specific wrapper that you need anyway. And depending on configuration, you can choose to hide either cout or cerr, or write to file.

BR,
Alex

Edward Chow

unread,
Mar 14, 2019, 11:20:02 AM3/14/19
to android-ndk
according to https://developer.android.com/studio/command-line/logcat ,  this way only redirect stdio like System.out / System.err, or maybe i run this in a wrong way. however, i managed to find out the link configuration and add -llog into link commands. this way __android_log_write api came to usable.

在 2019年3月4日星期一 UTC+8下午9:48:20,Andrew Esh写道:

Chris Browet

unread,
Mar 15, 2019, 10:24:33 AM3/15/19
to android-ndk
So, at the end of the day, you *did* want want to use log.h, but had issues linking?
On the importance of stating requirements properly... ;)

Andrea Fiorito

unread,
Jan 12, 2024, 7:39:53 PMJan 12
to android-ndk
Hi,
I'm trying to use streambuf to solve the same problem, in a slightly different and more elegant way of the one proposed in here. It works if I use it on a function called from Java, but the weird thing is that using cout in JNI_OnLoad crashes.

jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/) {
      std::cout << "Test" << std::endl;   // It crashes 
      std::cout.rdbuf(new StreamBuf2Log(StreamBuf2Log::OUT)); // It crashes
      redirector = new StdStreamRedirector(); // doing the above in a constructor, it crashes
}

using the StdStreamRedirector with the streambuf reassigned in a constructor, crashes if it is a global variable.

I wonder if anyone knows why and from what point on it is safe a call to std::cout


Andrea Fiorito

unread,
Jan 12, 2024, 8:11:30 PMJan 12
to android-ndk
Actually my bad, I forgot to put the return clause on the JNI_OnLoad function so that was the reason it crashed, while using cout is perfectly fine.

Still if I call the rdbuf for cout on the constructor of a global variable it crashes. 
the source code used is here https://stackoverflow.com/a/77809945/1756939
Reply all
Reply to author
Forward
0 new messages