在日常的开发过程中,android经常会使用一些开源的动态库,这些库是跨平台的,log默认是std::print,那在android平台上,我们主要使用logcat来打印日志,如果不修改的话,动态库的log会看不到,修改方法也非常简单:
1. 增加std::out到logcat接口
extern "C" JNIEXPORT jstring JNICALL
Java_com_nio_testdds_MainActivity_stdlog2Logcat(
JNIEnv* env,
jobject /* this */) {
int pipes[2];
pipe(pipes);
dup2(pipes[1], STDOUT_FILENO);
FILE *inputFile = fdopen(pipes[0], "r");
char readBuffer[256];
while (1) {
fgets(readBuffer, sizeof(readBuffer), inputFile);
__android_log_write(2, "testddsstderr", readBuffer);
}
}
2. 在JAVA合适的地方调用接口
private void stdlog_Logcat() {
new Thread() {
public void run() {
stdlog2Logcat();
}
}.start();
}
public native String stdlog2Logcat();