在android下面debug,最主要的方式就是用logcat抓log了,我们可能有尝试过使用printf来打印,当然结果是不行的,这里有时间就看了一下android平台下log的flow,在此做个笔记以作记录
我们一般使用ALOGD来打印log,所以这里就跟一下ALOGD的flow
system/core/include/log/log.h
这里有发现ALOGD其实是一系列宏,最终会调用到 __android_log_print(prio, tag, fmt),这个函数带三个参数:
1,优先级,这个就是我们平时说的log level,ALOGD的log level是LOG_DEBUG;
2,tag,这个就是我们在code里面指定的LOG_TAG
3,__VA_ARGS__,这个就是我们常见的格式化输入了,也就是说我们ALOGD带的参数可以使用格式化的格式
关于__android_log_print的具体实现如下:
system/core/liblog/logd_write.c
关于__android_log_write的具体实现如下:
我们一般使用ALOGD来打印log,所以这里就跟一下ALOGD的flow
system/core/include/log/log.h
system/core/include/log/log.h
#ifndef ALOGD
#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#endif
#ifndef ALOG
#define ALOG(priority, tag, ...) \
LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
#endif
#ifndef LOG_PRI
#define LOG_PRI(priority, tag, ...) \
android_printLog(priority, tag, __VA_ARGS__)
#endif
#define android_printLog(prio, tag, fmt...) \
__android_log_print(prio, tag, fmt)
这里有发现ALOGD其实是一系列宏,最终会调用到 __android_log_print(prio, tag, fmt),这个函数带三个参数:
1,优先级,这个就是我们平时说的log level,ALOGD的log level是LOG_DEBUG;
2,tag,这个就是我们在code里面指定的LOG_TAG
3,__VA_ARGS__,这个就是我们常见的格式化输入了,也就是说我们ALOGD带的参数可以使用格式化的格式
关于__android_log_print的具体实现如下:
system/core/liblog/logd_write.c
int __android_log_print(int prio, const char *tag, const char *fmt, ...)
{
va_list ap;
char buf[LOG_BUF_SIZE];
va_start(ap, fmt);
vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
va_end(ap);
return __android_log_write(prio, tag, buf);
}
这里可以看到格式化的输入被转化成buf的字符数组了,然后buf作为第三个参数调用__android_log_write
关于__android_log_write的具体实现如下:
int __android_log_write(int prio, const char *tag, const char *msg)
{
struct iovec vec[3];
log_id_t log_id = LOG_ID_MAIN;
char tmp_tag[32];
if (!tag)
tag = "";
/* XXX: This needs to go! */
if (!strcmp(tag, "HTC_RIL") ||
!strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
!strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
!strcmp(tag, "AT") ||
!strcmp(tag, "GSM") ||
!strcmp(tag, "STK") ||
!strcmp(tag, "CDMA") ||
!strcmp(tag, "PHONE") ||
!strcmp(tag, "SMS")) {
log_id = LOG_ID_RADIO;
// Inform third party apps/ril/radio.. to use Rlo