Android Log System

Source: Internet
Author: User

I recently read a little Android code about log.

For developers, the main calling tool for log reading is logcat, which is followed by some parameters,

If written, Java or C/C ++ has corresponding interfaces.

Code Location:

Frameworks/base/CORE/Java/Android/util/log. Java

System/CORE/liblog

System/CORE/logcat

Frameworks/base/CORE/JNI/android_util_log.cpp

Write process:

In the Java layer, frameworks/base/CORE/Java/Android/util/log. Java drops the local method through JNI _ android_log_buf_write,

int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg){    struct iovec vec[3];    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 */        !strcmp(tag, "AT") ||        !strcmp(tag, "GSM") ||        !strcmp(tag, "STK") ||        !strcmp(tag, "CDMA") ||        !strcmp(tag, "PHONE") ||        !strcmp(tag, "SMS"))            bufID = LOG_ID_RADIO;    vec[0].iov_base   = (unsigned char *) &prio;    vec[0].iov_len    = 1;    vec[1].iov_base   = (void *) tag;    vec[1].iov_len    = strlen(tag) + 1;    vec[2].iov_base   = (void *) msg;    vec[2].iov_len    = strlen(msg) + 1;    return write_to_log(bufID, vec, 3);}

Write_to_log is the function pointer and is initialized to _ write_to_log_init.

static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;

_ Write_to_log_init open the Log Device files, which are defined in logger. h.

#define LOGGER_LOG_MAIN     "log/main"#define LOGGER_LOG_RADIO    "log/radio"#define LOGGER_LOG_EVENTS   "log/events"#define LOGGER_LOG_SYSTEM   "log/system"

After the device is initialized, write_to_lo points to _ write_to_log_kernel.

static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr){    ssize_t ret;    int log_fd;    if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {        log_fd = log_fds[(int)log_id];    } else {        return EBADF;    }    do {        ret = log_writev(log_fd, vec, nr);    } while (ret < 0 && errno == EINTR);    return ret;}

The log_id points to the opened Log Device, obtains the file descriptor, and writes the log information to the file. The ID is defined in log. java.

    /** @hide */ public static final int LOG_ID_MAIN = 0;    /** @hide */ public static final int LOG_ID_RADIO = 1;    /** @hide */ public static final int LOG_ID_EVENTS = 2;    /** @hide */ public static final int LOG_ID_SYSTEM = 3;

For C/C ++, you only need to reference the header file <utils/log. h> and define the log_tag macro. The following macro definition is available in log. h.

#ifndef LOG_TAG                                                               #define LOG_TAG NULL                                                          #endif 

Therefore, avoid conflicting log_tag definitions.

The macro definition of log is as follows:

/* * Simplified macro to send a debug log message using the current LOG_TAG. */#ifndef LOGD#define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))#endif/* * Basic log message macro. * * Example: *  LOG(LOG_WARN, NULL, "Failed with error %d", errno); * * The second argument may be NULL or "" to indicate the "global" tag. */#ifndef LOG#define LOG(priority, tag, ...) \    LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)#endif/* * Log macro that allows you to specify a number for the priority. */#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)

_ Android_log_print will call the _ android_log_write function to complete the write process.

Read process:

As you can imagine, the reading process is to open the corresponding device file and read the log information in it.

Generally, logcat commands are executed in minicom or ADB shell.

The main function in logcat. cpp is the entry of this command.

Here, the-B parameter selects the corresponding buffer, that is, the corresponding device file.-C clears the buffer,-G gets the buffer size, and-F sets the log output device.

After obtaining the device list in the command line, open the device and call readloglines to read log information from the device to the queued_entry_t queue.

Static void readloglines (log_device_t * devices)

            for (dev=devices; dev; dev = dev->next) {                FD_SET(dev->fd, &readset);            }            result = select(max + 1, &readset, NULL, NULL, sleep ? NULL : &timeout);

Android_log_printlogline is responsible for printing the logs in the queued_entry_t queue to the g_outfd descriptor device. If-F is not set, it is printed to the standard output.

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.