Android to implement its own log information _android

Source: Internet
Author: User
Tags string tags throwable

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 this article, we briefly describe how to use and view logs in the Android kernel space and user space.

The use of log during kernel development. The Android kernel is based on Linux Kerne 2.36, so the Linux kernel log mechanism is also suitable for the Android kernel, which is known as PRINTK, and the C language of printf. Like printf, PRINTK provides formatted input functionality, and it also features all log mechanisms-providing logging-level filtering. PRINTK provides 8 logging levels (<linux/kernel.h>):

#define Kern_emerg "<0>"/  * system is unusable   */ 
#define KERN_ALERT "<1>"/  * action must be Taken immediately * * * 
#define KERN_CRIT "<2>"/  * Critical conditions/ 
#deinfe kern_err "<3 > "/  * ERROR conditions * * * 
#deinfe kern_warning" <4> "/  * WARNING conditions 
* * #deinfe kern_notice "<5>"/  * Normal but significant condition * * * 
#deinfe kern_info "<6>"/  * Informational   * * 
#deinfe kern_debug "<7>"/  * Debug-level messages   

How to use PRINTK:

PRINTK (Kern_alert "This are the log printed by PRINTK into Linux kernel space.");

Kern_alert represents the log level, followed by the formatting of the string.

In the Android system, the PRINTK output log information is stored in the/proc/kmsg, to view the contents of the/proc/kmsg, to download the Android kernel source in Ubuntu, compile, install a text, run the simulator in the background:

user-name@machine-name:~/android$ Emulator &

To start the ADB shell tool:

user-name@machine-name:~/android$ adb Shell

To view/proc/kmsg files:

Root@android:/# cat/proc/kmsg

two. The use of log during the development of user space programs. 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. In the Android system, there are two different logger access interfaces for both C + + and the Java language respectively. C + + Log interfaces are typically used when writing hardware abstraction layer modules or writing JNI methods, while Java interfaces are typically used when writing apps in the application tier.

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:

 
 * * Android Log priority values, in ascending priority order. 
 * * 
typedef enum Android_logpriority { 
 android_log_unknown = 0, 
 android_log_default,/* only for Setminpriority () * * * 
 android_log_verbose, 
 android_log_debug, 
 android_log_info, 
 Android_log_warn, 
 Android_log_error, 
 Android_log_fatal, 
 android_log_silent,/* Only for setminpriority (); must is last * * 

In System/core/include/cutils/log.h, the corresponding macros are defined, such as the macro LOGV corresponding to Android_log_verbose:

* * This is the local tag used for the following simplified * logging macros. 
 You can change this preprocessor definition * before using the "other macros" to "change the" tag. * * #ifndef log_tag #define LOG_TAG NULL #endif/* Simplified macro to send a verbose LOG message using the Curre 
 NT Log_tag. * * #ifndef LOGV #if log_ndebug #define LOGV (...) ((void) 0) #else #define LOGV (...) 
 (void) log (Log_verbose, Log_tag, __va_args__) #endif #endif/* Basic LOG message macro. 
 * * Example: * LOG (Log_warn, NULL, "Failed with Error%d", errno); 
 * The second argument may is NULL or "" to indicate the "global" tag. * * #ifndef Log #define LOG (priority, tag, ...) \ Log_pri (android_# #priority, Tag, __va_args__) #endif * * Log 
 Macro that allows the Specify a number for priority. 
 * * #ifndef LOG_PRI #define LOG_PRI (priority, tag, ...) \ Android_printlog (priority, tag, __va_args__) #endif/* * ================================================================ * * The stuff in the rest of this file should is used directly.  * * #define ANDROID_PRINTLOG (prio, tag, fmt ...) \ __android_log_print (prio, tag, FMT)

Therefore, if you want to use the C + + log interface, you can simply define your own Log_tag macros and include header files System/core/include/cutils/log.h:

#define LOG_TAG "My LOG TAG"

#include <cutils/log.h>

You can, for example, use LOGV:

LOGV ("This are the log printed by LOGV on Android user space.");

Look at the Java log interface in the Android system. The Android system defines the log interface in the frameworks layer:

(Frameworks/base/core/java/android/util/log.java):

..... ....... ...... ...... ..... ...... ...... ..... ..... ... public final class Log {... ... ...... ..... ....... ....... ........ ... .../** * Priority constant for the println method; 
   Use LOG.V. 
 
 * * public static final int VERBOSE = 2; /** * Priority constant for the println method; 
   Use LOG.D. 
 
 * * public static final int DEBUG = 3; /** * Priority constant for the println method; 
   Use LOG.I. 
 
 * * public static final int INFO = 4; /** * Priority constant for the println method; 
   Use LOG.W. 
 
 * * public static final int WARN = 5; /** * Priority constant for the println method; 
   Use LOG.E. 
 
 * * public static final int ERROR = 6; 
   /** * Priority constant for the println method. 
 
* * public static final int ASSERT = 7; ..... ....... ...... ..... ..... ...... ..... ..... ..... ..... ..... ... public static int V (string tag, string msg) {return println_ 
 Native (Log_id_main, VERBOSE, Tag, msg); public static int V (string tag, string msg, THRowable tr) {return println_native (Log_id_main, VERBOSE, tag, msg + ' \ n ' + getstacktracestring (tr)); 
 public static int D (string tags, string msg) {return println_native (Log_id_main, DEBUG, Tag, msg); public static int D (string tags, string msg, Throwable tr) {return println_native (Log_id_main, DEBUG, tag, MSG + 
 ' \ n ' + getstacktracestring (TR); 
 public static int I (string tags, string msg) {return println_native (Log_id_main, INFO, Tag, msg); public static int I (string tags, string msg, Throwable tr) {return println_native (Log_id_main, INFO, tag, msg + ') 
 \ n ' + getstacktracestring (tr)); 
 public static int W (string tags, string msg) {return println_native (Log_id_main, WARN, Tag, msg); public static int W (string tags, string msg, Throwable tr) {return println_native (Log_id_main, WARN, tag, msg + ') 
 \ n ' + getstacktracestring (tr)); public static int W (String tag, Throwable tr) {return println_native (Log_id_main, WARN, Tag, getstacktracestring (tr)); 
 public static int E (string tag, string msg) {return println_native (Log_id_main, ERROR, Tag, msg); public static int E (string tags, string msg, Throwable tr) {return println_native (Log_id_main, ERROR, tag, MSG + 
 ' \ n ' + getstacktracestring (TR);  } 
 
..................................................................

Therefore, if you want to use the Java log interface, as long as the Log_tag constants and references defined in the class are android.util.Log:

private static final String Log_tag = "My_log_tag";

LOG.I (Log_tag, "This are the LOG printed by LOG.I in Android user spaces.");

To see the output of these logs, you can match the Logcat tool. If you're running an emulator in an eclipse environment and you've installed the Android plugin, it's easy to see directly in Eclipse:

If you are using it in your own compiled Android source project, run the emulator in the background:

user-name@machine-name:~/android$ Emulator &

To start the ADB shell tool:

user-name@machine-name:~/android$ adb Shell

To view logs using the Logcat command:

Root@android:/# Logcat

This will allow you to see the log of the output.

The above is the Android own log information implementation method, the need for friends to look down.

Related Article

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.