是“std::cout";可用于 Android-ndk

2022-01-10 00:00:00 console android-ndk c++

在 Android-ndk 中,我们可以使用__android_log_write"、__android_log_print"等来将消息输出到LogCat"窗口.如果我使用std::cout"输出一些字符串怎么样?例如

In Android-ndk, we could use "__android_log_write", "__android_log_print", ... etc to output messages to the "LogCat" window. How about if I use "std::cout" to output some strings ? E.g.

std::cout << "some strings" << std::endl;

字符串将被发送到哪里.

Where would the strings be sent.

Android好像没有Console Applications,上面的字符串可能发不出去.我可以将stdout"重定向到一个文件,以便将字符串发送到std::cout"相当于记录消息吗?

It seems that Android does not have Console Applications and the above strings may not be sent. Could I redirect the "stdout" to a file so that sending strings to "std::cout" is equivalent to logging messages ?

推荐答案

根据Android文档,stdout &stderr 输出到 /dev/null.您可以使用 Android 调试桥实现你想要的.

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

默认情况下,Android 系统将 stdout 和 stderr(System.out 和 System.err)输出发送到/dev/null.在运行 Dalvik VM 的进程中,您可以让系统将输出的副本写入日志文件.在这种情况下,系统使用日志标签 stdout 和 stderr 将消息写入日志,两者的优先级均为 I.要以这种方式路由输出,请停止正在运行的模拟器/设备实例,然后使用 shell 命令 setprop 启用输出重定向.以下是你的做法:

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

系统会保留此设置,直到您终止模拟器/设备实例.要将设置用作模拟器/设备实例的默认设置,您可以在设备上的/data/local.prop 中添加一个条目.

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.

相关文章