Interpreting the implementation of the Android LOG Mechanism: (6) using LOG in the c/c ++ domain

Source: Internet
Author: User

 

The LOG Mechanism provided by Android runs through Java, JNI, local c/c ++ implementation, Linux kernel driver, and Other Android layers, and is simple and clear, it is a pretty good case. This series of articles explains the internal implementation mechanism of the LOG mechanism. This article is the sixth in the series. It explains how to use the LOG Mechanism to record LOG information in Android c/c ++ programs.

 

 

 

Implement LOG output in the c/c ++ local database

 

I learned from the previous article that Android Java outputs Log information through android. util. Log. Can the Android local c/c ++ program record logs through this mechanism? Looking back at the current local implementation of c/c ++ in Log, the answer is certainly yes, and it is quite simple. Android directly defines some Macros in the header file (system/core/include/cutils/log. h.

 

Because the LOG is classified into VERBOSE/DEBUG/INFO/WARN/ERROR/ASSERT and other categories. For the sake of simplicity, the implementation of DEBUG is described as an example.

 

# Ifndef LOGD

# Define LOGD (...) LOG (LOG_DEBUG, LOG_TAG, _ VA_ARGS __)

# Endif

 

# Ifndef LOGD_IF

# Define LOGD_IF (cond ,...)\

(CONDITION (cond ))\

? LOG (LOG_DEBUG, LOG_TAG, _ VA_ARGS __)\

: (Void) 0)

# Endif

 

# Ifndef LOG

# Define LOG (priority, tag ,...)\

LOG_PRI (ANDROID _ # priority, tag, _ VA_ARGS __)

# Endif

 

# Ifndef LOG_PRI

# Define LOG_PRI (priority, tag ,...)\

({\

If (priority = ANDROID_LOG_VERBOSE) & (LOG_NDEBUG = 0) | \

(Priority = ANDROID_LOG_DEBUG) & (LOG_NDDEBUG = 0) | \

(Priority = ANDROID_LOG_INFO) & (LOG_NIDEBUG = 0) | \

(Priority = ANDROID_LOG_WARN) | \

(Priority = ANDROID_LOG_ERROR) | \

(Priority = ANDROID_LOG_FATAL ))\

(Void) android_printLog (priority, tag, _ VA_ARGS __);\

})

# Endif

 

# Define android_printLog (prio, tag, fmt ...)\

_ Android_log_print (prio, tag, fmt)

 

 

In the end, this series of macros still use the function _ android_log_print ()

 

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 );

}

 

The _ android_log_write () function is called here (). You should be familiar with this function. It is exactly where the local c/c ++ function mentioned above is used to write device files.

 

 

 

How to record logs in c/c ++ programs

 

The common practice of logging in c/c ++ is:

 

Define your own TAG_LOG macro; include the header file log. h; then you can directly use LOGV/LOGD/LOGI/LOGW/LOGE to record the Log.

 

For example, the file lights. c is written in the beginning like this,

 

# Define LOG_TAG "lights"

# Include <cutils/log. h>

 

 

Then, LOGV, LOGE, and etc are used to record logs in the subsequent parts of the file.

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.