android 中在进行一些 C++ 底层库开发的时候难免需要一些 log 来辅助我们的开发调试,或是打印一些重要信息给到 Lib 库的使用者。在 android 中,通过 C++ 层的 print Or std::cout 是无法在logcat 中正常显示 log 信息的。android NDK 专门提供了相关的 Logging 工具。
__android_log_print 主要使用的函数
int __android_log_print( int prio, //优先级 constchar *tag, //标签 constchar *fmt, // format log string ... )
cmake 编译, 在 CMakeLists.txt 中配置需要依赖的 Log 系统库
#查找要依赖的系统库 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. # Links the target library to the log library # included in the NDK. ${log-lib} )
char* log_example_str = "hello logger"; LOGD("This is a log, the content is %s", log_example_str); LOGI("This is a log, the content is %s", log_example_str); LOGW("This is a log, the content is %s", log_example_str); LOGE("This is a log, the content is %s", log_example_str); LOGF("This is a log, the content is %s", log_example_str);