Use log in Android C/C ++ code

Source: Internet
Author: User

In Android, Java code outputs log information through Android. util. log. The same local C/C ++ code also provides more interfaces. Android directly goes to the header file (System/CORE/include/cutils/log. h) defines some log Output Macros, which are better than Android. util. log provides more log output interfaces. Therefore, you can use these macros to output the same logs as those in Java code. Macro logd (), LogE (), Logi (), logv (), logw (), and logd () correspond to Android respectively. util. log. D (), log. E (), log. I (), log. V (), log. W ().

Note: logs are output to the main buffer. In addition, it is best to compare 《 Android Log Mechanism Flowchart. In log. H, more detailed macros are provided for log output. For example, for logd (), logd_if (), if_logd (), slogd (), and slogd_if () are also provided (). Logd_if () indicates conditional output, if_logd () indicates whether output is required, slogd () indicates that logs are output to the System Log buffer, and slogd_if () indicates that conditions are true, output logs to the System Log buffer. Note: if_logd () always returns 1. 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 if_logd # define if_logd () if_log (log_debug, log_tag) # endif
# Ifndef slogd # define slogd (...) (void) _ android_log_buf_print (log_id_system, android_log_debug, log_tag, _ va_args _) # endif # ifndef slogd_if # define 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 (), and logd () are actually the best to use the following macros. # 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) and these macros finally call _ android_log_print () int _ android_log_print (INT Prio, FMT) in system/CORE/liblog/logd_write.c, const char * tag, const char * FMT ,...) {va_list AP; char Buf [log_buf_size]; va_start (AP, FMT); vsnprintf (Bu F, log_buf_size, FMT, AP); va_end (AP); Return _ android_log_write (PRIO, Tag, Buf);} the function _ android_log_write () is still called here (). _ Android_log_write () organizes parameters and calls the write_to_log function pointer. For more information about calling the write_to_log function pointer, see 《 Detailed explanation of Log Mechanism in Android"
It is easy to use Log in C/C ++. The common practice is to define your own tag_log macro, including the header file log. h, and then directly use logv/logd/Logi/logw/loge where you need to record the log. For example, the lights file. write in C at the beginning, # define log_tag "Lights" # include <cutils/log. h> in the subsequent part of the file, you can directly use logv/loge to output logs.

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.