Android Log Architecture (i)

Source: Internet
Author: User
Tags data structures

We know that in the Android system, we provide a lightweight log system that is implemented in kernel space in the form of a driver, while the user space provides both a Java interface and a C + + interface to use this log system. Depending on whether you're writing an Android application or a system component. In the previous article on the use of log in the development of Android system, we have briefly introduced the use of log in the development of Android applications, in this article, we will further analyze the source code of the logger driver. Makes us have a deep understanding of the Android log system.

"Android system source code scenario analysis," a book is in the aggressive programmer Network (http://0xcc0xcd.com) serialized, click to enter.

Since the Android log system is implemented as a driver in the kernel space, we need to get the Android kernel source code to analyze it, please refer to the previous download, compile and install the latest source code for Android in Ubuntu and download it on Ubuntu, Compile and install the latest Android kernel source code (Linux Kernel) with two articles to download the Android source code project. The logger driver is composed of two files, respectively:

Kernel/common/drivers/staging/android/logger.h

Kernel/common/drivers/staging/android/logger.c

Next, we will introduce the related data structure of the logger driver separately, then carry on the scenario analysis to the logger driver source code, respectively log system initialization story, log read story and log write scene.

A. Logger the relevant data structure of the driver.

       We first look at the contents of the Logger.h header file:

[cpp]  View plain  copy #ifndef  _LINUX_LOGGER_H   #define  _LINUX_LOGGER_H      #include  <linux/types.h>   #include  <linux/ioctl.h>      struct logger_entry {       __u16        len;    /* length of the payload */       __u16       __pad;  /* no  matter what, we get 2 bytes of padding */        __s32       pid;    /* generating  process ' s pid */       __s32        tid;    /* generating process ' s tid */        __s32&Nbsp;      sec;    /* seconds since epoch  */       __s32       nsec;    /* nanoseconds */       char         msg[0]; /* the entry ' s payload */  };      define logger_log_radio     "Log_radio"  /* radio-related messages  */   #define  LOGGER_LOG_EVENTS    "Log_events"     /*  system/hardware events */   #define  LOGGER_LOG_MAIN      "Log_ Main "  /* everything else */      #define  logger_entry_max_len          (4*1024)    #define  logger_entry_max_payload     \        (logger_entry_max_len - sizeof (struct logger_entry) )       #define  __LOGGERIO  0xAE      #define  logger_ Get_log_buf_size     _io (__loggerio, 1)  /* size of log  */   #define &NBSP;LOGGER_GET_LOG_LEN&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;_IO (__LOGGERIO, 2)   /* used log len */   #define &NBSP;LOGGER_GET_NEXT_ENTRY_LEN&NBSP;&NBSP;&NBSP;_IO ( __loggerio, 3)  /* next entry len */   #define  logger_flush_log         _io (__loggerio, 4)  /* flush log */       #endif  /* _LINUX_LOGGER_H */            struct Logger_entry is a structure used to describe a log record. The Len member variable records the length of the payload for this record, the length of the log record itself specified by the payload, but does not include the struct logger_entry structure used to describe the record. BackRemember when we call the Android.util.Log interface to use the log system, we specify the priority level of the log Priority, the tag string, and the MSG string, the length of the Priority + tag + msg combination is the recorded payload length. The __PAD member variable is used to align the struct body. The PID and TID member variables are used to record which process wrote the record. SEC and NSEC member variables record when the log was written. The MSG member variable records the contents of the payload, and its size is determined by the Len member variable.

Then define two macros:

#define Logger_entry_max_len (4*1024)

#define LOGGER_ENTRY_MAX_PAYLOAD \

(logger_entry_max_len-sizeof (struct logger_entry))

As you can see from these two macros, the payload length of each log record plus the length of the struct logger_entry cannot exceed 4K bytes.

Other macros are also defined in the Logger.h file, and readers can analyze them themselves, and in the following analysis, we will explain them in detail.

Then look at the definition of other related data structures in the Logger.c file:

[CPP] View plain

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.