Using log in the C + + code of Android

Source: Internet
Author: User

In Android, the Java code outputs log information through ANDROID.UTIL.LOG, and the same native C + + code also provides the corresponding and more interfaces. Android directly defines some log output macros in the header file (system/core/include/cutils/log.h), which provides more log output interfaces than Android.util.Log. Therefore, using these macros, you can make the same log output as in Java code. Macro Logd (), LOGE (), Logi (), LOGV (), LOGW (), LOGD () correspond to Android.util.Log (), LOG.D (), LOG.E (), LOG.I (), LOG.V () in LOG.W ().

Note: This is all about outputting the log to the main buffer. In addition, it is better to compare the text of theAndroid log mechanism flowchart"To see together. Log.h also provides some finer macros for the log output, such as LOGD (), logd_if (), If_logd (), Slogd (), slogd_if (). where Logd_if () represents the conditional output, IF_LOGD () is used to test whether the output is required, SLOGD () indicates that the log output to the system log buffer, slogd_if () indicates that the condition is true, the log output to the system log buffer. Note: IF_LOGD () now always returns 1. Because, log is divided into categories such as Verbose/debug/info/warn/error/assert, for simplicity, the implementation of DEBUG as an example to illustrate. #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 if_logd  #define &NBSP;IF_LOGD ()  if_log (Log_debug, Log_tag) #endif   #ifndef slogd  #define &NBSP;SLOGD (...)   (void) __android_log_buf_print (Log_id_system, Android_log_debug, Log_tag, __va_args__)) #endif #ifndef SLOGD_ if  #define &NBSP;SLOGD_IF (Cond, ...) \      ((CONDITION (cond)) \     ? (void) __android_log_buf_print (Log_id_system, Android_log_debug, Log_tag, __va_args__) \     : (Void) 0)   #endif   Macro logd (), LOGE (), Logi (), LOGV (), LOGW (), LOGD () in fact, it is best to use the following macro. #ifndef Log#define LOG (priority, tag, ...) \    log_pri (android_# #priority, Tag, __va_args__) #endif # Ifndef LOG_PRI#DEFINE&NBSP;LOG_PRI (priority, tag, ...)                                  &NBS P   \    {                                                     ,         &N Bsp  \       if ((priority = = Android_log_verbose) && (Log_ndebug = = 0)) | |  \  &N Bsp         ((priority = = Android_log_debug) && (Log_nddebug = = 0))  | |  \            ((priority = = Android_log_info) && (Log_nidebug = = 0))   | |  \      &NBSp     (priority = = Android_log_warn)                          | |  \            (priority = = Android_log_error)                         | |  \            (priority = = Android_log_fatal))           &NBSP ;                \                 (void) ) Android_printlog (priority, tag, __va_args__);     \   }) #endif   #define &NBSP;ANDROID_PRINTLOG (prio, tag, fmt ...) \__android_log_print (prio , tag, FMT) and this series of macros finally calls the __android_log_print () Int __android_log_print in system/core/liblog/logd_write.c (int Prio, const char *tag, const char *FMT, ...) {    va_list ap;    Char buf[log_buf_size];     Va_start (AP, FMT);    VSNPRI ntf(BUF, Log_buf_size, FMT, AP);    Va_end (AP);     return __android_log_write (Prio, tag, buf);} This is still tuned to function __android_log_write (). __android_log_write () organizes the parameters and calls the Write_to_log function pointer. For more information on the invocation of this function pointer, please refer to the Write_to_logAn explanation of the log mechanism in Android"It's easy to use log in C + +. It is common practice to define your own Tag_log macro, include the header file Log.h, and then use Logv/logd/logi/logw/loge, etc. where log is required. For example, the file lights.c in the beginning of this write, #define LOG_TAG "Lights" #include <cutils/log.h> and then in the subsequent part of the file, directly with the logv/loge and so on to output the log can be.

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.