How to Use logcat in Android native programming

Source: Internet
Author: User

Http://hi.baidu.com/grfillzaccbaqwq/item/b8dc91da94576e12d90e4490

Http://www.cnblogs.com/melode11/archive/2009/10/15/1584120.html

 

[11.04.02] added log debugging (Logi \ logd \ loge...) Output support on the android C/C ++ layer.

Recently, I have been studying the C/C ++ layer of Android 2.3.3 source code. I need to debug the code, but it is strange that I directly add logd ("XXXXXXXX ");, logcat does not display any output. Logi, logv, logw, and loge are ineffective. So I found the solution on the Internet. After several experiments, I finally found the solution. Now I will post the following notes:

Step 1: add the following to the corresponding MK file:Local_ldlibs: =-llog
Step 2: add the following to the CPP file to use log:
# Include <Android/log. h>
# Define logd (...) _ android_log_print (android_log_debug, "keymatch", _ va_args __)
Step 3: You can use: logd ("debug information I want to see ^_^"); in this way, the output on the logcat side is: D/keymatch (32 ): debugging information I want to see ^_^ if you want to change the content in the output, you can refer to the corresponding color labels. For example, if you want to define loge, you can change android_log_debug to android_log_error. Likewise, logi's magic horse and so on:

# Define logv (...) _ android_log_print (android_log_verbose, "projectname", _ va_args __)
# Define logd (...) _ android_log_print (android_log_debug, "projectname", _ va_args __)
# Define Logi (...) _ android_log_print (android_log_info, "projectname", _ va_args __)
# Define logw (...) _ android_log_print (android_log_warn, "projectname", _ va_args __)
# Define LogE (...) _ android_log_print (android_log_error, "projectname", _ va_args __)

Of course, you can also use the _ android_log_print function without defining logxxx. For more details, refer to this article: in addition, some articles have said that this method may cause problems when compiling a dynamic library, and a cannot find-llog error will be prompted. The library file liblog. So cannot be found. Therefore, you need to change itLocal_ldlibs: =-L $ (sysroot)/usr/lib-llogCan be compiled normally. However, when I compile a dynamic library, I don't seem to need to modify it like this. I didn't find the error "cannot find-llog" prompted during compilation. If you find this problem in practice, you can refer to this Article. After Android ndk is published, how to use logcat in Android native programming, java + C programming method has become the first choice for performance programming on Android. But it is difficult to debug in C, so it is necessary to use logcat. For the use of logcat in native code, there are many sayings on the internet, mostly lacking, and some are fundamentally wrong. To use logcat, first introduce the log header file in the code. # Include <Android/log. h>. Then, you can simply use the _ android_log_write (android_log_error, "tag", "message") method to output data to logcat. There are many log levels: warn, android_log_default, warn, android_log_debug, android_log_info, warn, android_log_error, warn, and warn. After writing this, If you compile it directly, the _ android_log_write method is undefined. what's going on? The key is to set the compilation options. In the Android. mk file, you can specify a local_ldlibs parameter. If this parameter is not specified, only the important default Lib, such as libc, will be introduced during compilation. If you want to use log, you need to introduce liblog. Local_ldlibs: =-llog is widely used on the Internet. It is okay to build static Lib. If you build shared Lib, A cannot find-llog error is returned. The library file liblog. So cannot be found. Therefore, you must change it to local_ldlibs: =-L $ (sysroot)/usr/lib-llog for normal compilation. The-l parameter specifies the path for searching Lib. The following is an android. example of MK content: local_path: = $ (call my-DIR) include $ (clear_vars) local_module: = testndknetworklocal_src_files: = httpconnection. cpplocal_ldlibs: =-L $ (sysroot)/usr/lib-lloginclude $ (build_shared_library)

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.