Android日志系统Logcat源代码简要分析


在前面两篇文章Android日志系统驱动程序Logger源代码分析和Android应用程序框架层和系统运行库层日志系统源代码中,介绍了Android内核空间层、系统运行库层和应用程序框架层日志系统相关的源代码,其中,后一篇文章着重介绍了日志的写入操作。为了描述完整性,这篇文章着重介绍日志的读取操作,这就是我们在开发Android应用程序时,经常要用到日志查看工具Logcat了。

Logcat工具内置在Android系统中,可以在主机上通过adb logcat命令来查看模拟机上日志信息。Logcat工具的用法很丰富,因此,源代码也比较多,本文并不打算完整地介绍整个Logcat工具的源代码,主要是介绍Logcat读取日志的主线,即从打开日志设备文件到读取日志设备文件的日志记录到输出日志记录的主要过程,希望能起到一个抛砖引玉的作用。

Logcat工具源代码位于system/core/logcat目录下,只有一个源代码文件logcat.cpp,编译后生成的可执行文件位于out/target/product/generic/system/bin目录下,在模拟机中,可以在/system/bin目录下看到logcat工具。下面我们就分段来阅读logcat.cpp源代码文件。

一.  Logcat工具的相关数据结构。

这些数据结构是用来保存从日志设备文件读出来的日志记录:

  1. struct queued_entry_t {  
  2.     union {  
  3.         unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1] __attribute__((aligned(4)));  
  4.         struct logger_entry entry __attribute__((aligned(4)));  
  5.     };  
  6.     queued_entry_t* next;  
  7.   
  8.     queued_entry_t() {  
  9.         next = NULL;  
  10.     }  
  11. };  
  12.   
  13. struct log_device_t {  
  14.     char* device;  
  15.     bool binary;  
  16.     int fd;  
  17.     bool printed;  
  18.     char label;  
  19.   
  20.     queued_entry_t* queue;  
  21.     log_device_t* next;  
  22.   
  23.     log_device_t(char* d, bool b, char l) {  
  24.         device = d;  
  25.         binary = b;  
  26.         label = l;  
  27.         queue = NULL;  
  28.         next = NULL;  
  29.         printed = false;  
  30.     }  
  31.   
  32.     void enqueue(queued_entry_t* entry) {  
  33.         if (this->queue == NULL) {  
  34.             this->queue = entry;  
  35.         } else {  
  36.             queued_entry_t** e = &this->queue;  
  37.             while (*e && cmp(entry, *e) >= 0) {  
  38.                 e = &((*e)->next);  
  39.             }  
  40.             entry->next = *e;  
  41.             *e = entry;  
  42.         }  
  43.     }  
  44. };  

其中,宏LOGGER_ENTRY_MAX_LEN和struct logger_entry定义在system/core/include/cutils/logger.h文件中,在Android应用程序框架层和系统运行库层日志系统源代码分析一文有提到,为了方便描述,这里列出这个宏和结构体的定义:

  1. struct logger_entry {  
  2.     __u16       len;    /* length of the payload */  
  3.     __u16       __pad;  /* no matter what, we get 2 bytes of padding */  
  4.     __s32       pid;    /* generating process's pid */  
  5.     __s32       tid;    /* generating process's tid */  
  6.     __s32       sec;    /* seconds since Epoch */  
  7.     __s32       nsec;   /* nanoseconds */  
  8.     char        msg[0]; /* the entry's payload */  
  9. };  
  10.   
  11. #define LOGGER_ENTRY_MAX_LEN        (4*1024)  

从结构体struct queued_entry_t和struct log_device_t的定义可以看出,每一个log_device_t都包含有一个queued_entry_t队列,queued_entry_t就是对应从日志设备文件读取出来的一条日志记录了,而log_device_t则是对应一个日志设备文件上下文。在Android日志系统驱动程序Logger源代码分析一文中,我们曾提到,Android日志系统有三个日志设备文件,分别是/dev/log/main、/dev/log/events和/dev/log/radio。

每个日志设备上下��通过其next成员指针连接起来,每个设备文件上下文的日志记录也是通过next指针连接起来。日志记录队例是按时间戳从小到大排列的,这个log_device_t::enqueue函数可以看出,当要插入一条日志记录的时候,先队列头开始查找,直到找到一个时间戳比当前要插入的日志记录的时间戳大的日志记录的位置,然后插入当前日志记录。比较函数cmp的定义如下:

  1. static int cmp(queued_entry_t* a, queued_entry_t* b) {  
  2.     int n = a->entry.sec - b->entry.sec;  
  3.     if (n != 0) {  
  4.         return n;  
  5.     }  
  6.     return a->entry.nsec - b->entry.nsec;  
  7. }  

为什么日志记录要按照时间戳从小到大排序呢?原来,Logcat在使用时,可以指定一个参数-t <count>,可以指定只显示最新count条记录,超过count的记录将被丢弃,在这里的实现中,就是要把排在队列前面的多余日记记录丢弃了,因为排在前面的日志记录是最旧的,默认是显示所有的日志记录。在下面的代码中,我们还会继续分析这个过程。

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 下一页

相关内容