Log usage and simple principles in Android Development

Source: Internet
Author: User

In the process of program development, LOG is a widely used mechanism used to record program execution processes. It can be used for program debugging or product Operation Event Recording. The Android system provides a simple and convenient LOG Mechanism for developers to conveniently use. It is often necessary to deal with logs during normal development. Therefore, it is necessary to understand the log usage and simple principles.

1. log output of Linux kernel
During the development of the standard Linux kernel, printk is used, which is the same as the name of printf output printing. It also provides the formatting output function, but only has
Print the information and save it to the/proc/kmsg log. Run the cat command to view its information. [cat/proc/kmsg]Copy codeThe Code is as follows: <SPANstyle = "COLOR: #003333; FONT-SIZE: 14px" >#definekern_emerg "<0>"/* systemisunusable */
# DefineKERN_ALERT "<1>"/* actionmustbetakenimmediately */
# DefineKERN_CRIT "<2>"/* criticalconditions */
# DeinfeKERN_ERR "<3>"/* errorconditions */
# DeinfeKERN_WARNING "<4>"/* warningconditions */
# DeinfeKERN_NOTICE "<5>"/* normalbutsignificantcondition */
# DeinfeKERN_INFO "<6>"/* informational */
# DeinfeKERN_DEBUG "<7>"/* debug-levelmessages */</SPAN>

2. log output in android
The Android system provides a lightweight logger Log System in the user space. It is a device driver implemented in the kernel, with the logcat tool of the user space, you can easily track debugging programs.
The C/C ++ log interface in the Android system is used by macros. The log level is defined in system/core/include/android/log. h:
/*
* Androidlogpriorityvalues, inascendingpriorityorder.
*/
Typedefenumandroid_LogPriority {
ANDROID_LOG_UNKNOWN = 0,
ANDROID_LOG_DEFAULT,/* onlyforSetMinPriority ()*/
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
ANDROID_LOG_INFO,
ANDROID_LOG_WARN,
ANDROID_LOG_ERROR,
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT,/* onlyforSetMinPriority (); mustbelast */
} Android_LogPriority;

For ease of use, the corresponding macro is defined in system/core/include/cutils/log. h:
# DefineLOGV (...) (void) LOG (LOG_VERBOSE, LOG_TAG ,__ VA_ARGS __))
# DefineLOGD (...) (void) LOG (LOG_DEBUG, LOG_TAG ,__ VA_ARGS __))
# DefineLOGI (...) (void) LOG (LOG_INFO, LOG_TAG ,__ VA_ARGS __))
# DefineLOGW (...) (void) LOG (LOG_WARN, LOG_TAG ,__ VA_ARGS __))
# DefineLOGE (...) (void) LOG (LOG_ERROR, LOG_TAG ,__ VA_ARGS __))
If you need to use log output, include the header file: # include <cutils/log. h> and link its dynamic library: liblog. so.
# DefineLOG_TAG "XX_LOG_TAG" // The output TAG can be defined here.
# Include <cutils/log. h>
JAVA layer printing:
Importandroid. util. Log;
PrivatestaticfinalStringTAG = "XX_LOG_TAG ";
Log. e (TAG, "ThisistheerrorlogprintedbyLog. iinandroiduserspace .");

3. How to obtain logs on the box
A. Go to shell to get log
Adbkill-server
Adbconnectdest-ip
Adbshell [logon shell]
Mount-oremount/system [Change permission]
Logcat> 1.log
Enter CTRL + C in the command line to return to the cmd command line, adbpullpath/1.log
B. Obtain the log directly from the cmd command line.
After adbconnect is connected;
Adbshell
Adblogcat> path/1.log
After the operation is completed
Enter CTRL + C in the command line to return to the cmd command line, adbpullpath/1.log
C. Directly output logs on the terminal.
Press enter to switch to shell
Logcat [now you can see the print]

4. Run in Eclipse environment (Android plug-in is installed). You can view it directly in Eclipse.

5. Common skills
1. logcat prints the output level + LOG_TAG name + process word + printed information, which can be used to analyze problems.
I/SystemServer (939): ActivityManager
I/ActivityManager (939): Memoryclass: 96
E/AndroidRuntime (939): ErrorreportingWTF
The first column is determined by Log. I (e/w...) or LOGI/LOGE/LOGW...
The second column is determined by LOG_TAG/TAG (JAVA). You can name the same prefix [xxx] funtion before the same group of modules.
The third column is the system process number getpid (), which prints the thread value pthread_slef ()

The last step is to add additional printed information.
2. Efficiency adjustment or execution time
1. We recommend that you add a prefix for key printing to facilitate search. Use [######]
2. Use logcat output time directly, debug execution speed, and analysis efficiency
Logcat-vtime [Displaythedate, invocationtime, priority/tag, andPIDoftheoriginatingprocess .]

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.