如何在安卓(NDK)的本地代码中编写日志?有哪些可用选项?例如,能否从NDK内部使用logcat编写日志?还是因为它更高级在安卓中,无法从NDK访问它?
目前我只知道使用以下C代码编写时间:
millis = System.currentTimeMillis();
并使用函数将此时间以及任何消息写入自定义日志文件。
如何在安卓(NDK)的本地代码中编写日志?有哪些可用选项?例如,能否从NDK内部使用logcat编写日志?还是因为它更高级在安卓中,无法从NDK访问它?
目前我只知道使用以下C代码编写时间:
millis = System.currentTimeMillis();
并使用函数将此时间以及任何消息写入自定义日志文件。
你可以使用Android日志记录
#include <android/log.h>
#define APPNAME "MyApp"
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "My Log");
LOCAL_LDLIBS := -llog
这个问题已经在有没有在Android NDK代码中打log的简单方法?中讨论过。
如果您使用较新的 Android Studio 版本 (2.2+),它使用 CMake,则在使用 C++ 支持生成新项目时将自动将以下内容添加到您的 CMakeLists.txt 文件中:
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
和
target_link_libraries( # Specifies the target library.
your-lib1
your-lib2
...
# Links the target library to the log library
# included in the NDK.
${log-lib} )