1、c++添加log
__android_log_print(prio, tag, __VA_ARGS)函数的实现:
int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
ErrnoRestorer errno_restorer;
if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
return -EPERM;
}
va_list ap;
char buf[LOG_BUF_SIZE];
va_start(ap, fmt);
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
va_end(ap);
__android_log_message log_message = {
sizeof(__android_log_message), LOG_ID_MAIN, prio, tag, nullptr, 0, buf};
__android_log_write_log_message(&log_message);
return 1;
}
__android_log_print函数的具体实现不用太过关系,观察下常用的打印log如ALOGE等。
#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) --->
#define ALOG(priority, tag, ...) LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) - -->
#define LOG_PRI(priority, tag, ...) android_printLog(priority, tag, __VA_ARGS__) --->
#define android_printLog(prio, tag, ...) __android_log_print(prio, tag, __VA_ARGS__)
上层调用来说,我们可以直接建立ALOGE与__android_log_print之间的关系,并且如下的使用:
#include LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "lethe_DEBUG"
#include <android/log.h> // 在此处等价于 #include<utils/Log.h>
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,## __VA_ARGS__)
#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG, ## __VA_ARGS__)
#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG, ## __VA_ARGS__)
fun()
{
ALOGE("debug log");
}
2、头文件查找
见到一些#include <android/log.h>
这种的头文件可以在framework/system等目录下进行查找,如 android/log.h 定义是在:
system/core/include/android/log.h
见到#include<hidl/memory/1.0/IMemory.h>头文件,hidl与aidl等会生成大量的中间件,可以在out/soong/.intermediates目录下进行查找,该文件定义在:
out/soong/.intermediates/system/libhidl/transport/memory/1.0/android.hidl.memory@1.0_genc++_headers/gen/android/hidl/memory/1.0/IMemory.h