In the process of program development, log is a widely used mechanism for recording program execution, which can be used for both program debugging and event recording in product operations. The Android system provides a simple, convenient log mechanism that developers can use easily. In peacetime development process often need to deal with log, so it is necessary to understand the use of log and simple principle.
1, the Linux kernel log output
In the standard Linux kernel development process, the use of PRINTK, which is the same as printf output printing function, also provides formatted output function, but it has
Print level and save the information to the/proc/kmsg log, and use the Cat command to view its information [cat/proc/kmsg]
Copy Code code 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, the Android log output
The Android system provides a lightweight logger log system in user space, a device driver that is implemented in the kernel, and is used in conjunction with the LOGCAT tools of user space to easily track the debugger.
The C + + log interface in the Android system is used by macros. The level of the log 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 macros are 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__))
Because if you need to use log output, include its header file: #include <cutils/log.h> and link its dynamic library: liblog.so
#defineLOG_TAG "Xx_log_tag"//Here you can define the TAG for its output
#include <cutils/log.h>
Java Layer Printing:
Importandroid.util.Log;
Privatestaticfinalstringtag= "Xx_log_tag";
LOG.E (TAG, "thisistheerrorlogprintedbylog.iinandroiduserspace.");
3, how to get Log on the box
A, enter the shell to get log
Adbkill-server
Adbconnectdest-ip
Adbshell "Login Shell"
Mount-oremount/system/system "Change Permissions"
Logcat>>1.log
command line input CTRL + C back to cmd command line, Adbpullpath/1.log
b, directly in the cmd command line to get log
After the adbconnect connected;
Adbshell
Adblogcat>>path/1.log
After the operation is complete
command line input CTRL + C back to cmd command line, Adbpullpath/1.log
c, direct output log at Terminal terminal
Return to Shell
Logcat[you can see the print at this time
4, in the Eclipse environment (installed with the Android plugin), you can directly in eclipse will be able to view the
5. Common Skills
1, Logcat will print "output level +log_tag name + process Word + printed information" can make full use of these information analysis problems
I/systemserver (939): Activitymanager
I/activitymanager (939): memoryclass:96
E/androidruntime (939): ERRORREPORTINGWTF
The first column consists of LOG.I (e/w). Decision) or LOGI/LOGE/LOGW ...
The second column is determined by Log_tag/tag (JAVA) and can be prefixed to the same set of modules [Xxx]funtion this name
The third column is the system process number Getpid (), the print thread value Pthread_slef ()
The final thing is to increase your printing information
2. Efficiency or execution time
1, the proposed focus of printing add prefix, easy to find. with [######]
2, direct use of logcat output time, debugging execution speed, analysis efficiency
Logcat-vtime "Displaythedate,invocationtime,priority/tag,andpidoftheoriginatingprocess."