迪特馬爾·庫爾的答案是非常好的,但它不從Crystax NDK與boost.log一起工作。我發現了another idea並已對其進行了一些修正。下面是代碼:
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <android/log.h>
static int pfd[2];
static pthread_t thr;
static const char *tag = "myapp";
static void *thread_func(void*)
{
ssize_t rdsz;
char buf[128];
while((rdsz = read(pfd[0], buf, sizeof buf - 1)) > 0) {
if(buf[rdsz - 1] == '\n') --rdsz;
buf[rdsz] = 0; /* add null-terminator */
__android_log_write(ANDROID_LOG_DEBUG, tag, buf);
}
return 0;
}
int start_logger(const char *app_name)
{
tag = app_name;
/* make stdout line-buffered and stderr unbuffered */
setvbuf(stdout, 0, _IOLBF, 0);
setvbuf(stderr, 0, _IONBF, 0);
/* create the pipe and redirect stdout and stderr */
pipe(pfd);
dup2(pfd[1], 1);
dup2(pfd[1], 2);
/* spawn the logging thread */
if(pthread_create(&thr, 0, thread_func, 0) == -1)
return -1;
pthread_detach(thr);
return 0;
}
而且其採用:
...
start_logger("MyApp");
...
從升壓所有輸出現在。登錄到std ::法院和std :: CERR將在logcat中:
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/logger.hpp>
...
boost::log::add_console_log(std::cout);
boost::log::add_common_attributes();
boost::log::sources::logger_mt lg;
BOOST_LOG(lg) << "Hello, World!";
...
子集:stdout重定向到的logcat:http://stackoverflow.com/questions/10531050/redirect-stdout-to-logcat-in-android-ndk的 – 2016-02-16 13:52:12
可能的複製[C/C++ printfs - 它出現在Android本地代碼中的位置?](https://stackoverflow.com/questions/6426911/cc-printfs-wheres-it-appears-in-a-android-native-code) – 2017-11-06 13:39:06