So I’m writing an Android app which uses a large c++ library. I have everything working so that the java app can call the c++ delegation methods, but I’m finding myself wishing I could log messages from c++ to the Android log. This is easy from java, but I’m at a loss as to how to call a java method from c++. My searches found methods for opening a jvm from c++, which is not at all what I want to do. Ideally, I’d like to pass a log method pointer to c++, which could then be used whenever I wanted. Of course, java doesn’t support method pointers. My java method would look something like:
private void log(String s){
Log.i(Tag, s); // Android log
}
I just don’t know how to allow c++ to access this method.
C++ calls to
coutandprintfwill not show up in the LogCat output. There are two solutions.Use the Logging macros provided by the NDK that allow you to log messages to LogCat. This is good for new code and wrapper code you are writing, but not so good when you have a library full of existing debugging statements. I define macros as followed:
and then within the sourcecode I can call
LOG_INFO("Library is called!");Capture the standard out/ standard error of the program and funnel it into LogCat. From the Android Debug Bridge page: