Introduction to the use of log 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. In this article, we will briefly introduce how to use and view logs in the android kernel space and user space.

1. log usage during kernel development. The android kernel is based on Linux kerne 2.36. Therefore, the Log Mechanism of Linux kernel is also suitable for the android kernel. It is a famous printk, which is the same as that of the C language printf. Similar to printf, printk provides the formatting input function, and also features all log mechanisms-providing the log-level excessive consideration function. Printk provides eight log 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 is the log printed by printk in Linux kernel space .");

Kern_alert indicates the log level, followed by the string to be formatted.

In the Android system, the log information output by printk is saved in/proc/kmsg. To view/proc/kmsg content, download, compile, and install the latest Android kernel source code (Linux kernel) on Ubuntu, and run the simulator on the background:

User-name @ machine-Name :~ /Android $ emulator &

Start the ADB shell tool:

User-name @ machine-Name :~ /Android $ ADB Shell

View the/proc/kmsg file:

Root @ Android: // # Cat/proc/kmsg

2. log usage during user space program development. 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. In the Android system, two different logger access interfaces are provided for C/C ++ and Java respectively. C/C ++ log interfaces are generally used when writing Hardware Abstraction Layer modules or JNI methods, while Java interfaces are generally used when writing apps at the application layer.

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:

/* * 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 be last */} android_LogPriority;

In System/CORE/include/cutils/log. H, the corresponding macro is defined, for example, 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 current 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 be 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 you to 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 not be used directly. */#define android_printLog(prio, tag, fmt...) \     __android_log_print(prio, tag, fmt)

Therefore, to use the C/C ++ log interface, you only need to define your own log_tag macro and include the header file system/CORE/include/cutils/log. h:

# Define log_tag "my log tag"

# Include <cutils/log. h>

You can, for example, use logv:

Logv ("this is the log printed by logv in Android user space .");

Let's take a look at the Java log interface in the Android system. The Android system defines the log interface (frameworks/base/CORE/Java/Android/util/log. Java) in the frameworks layer ):

................................................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 tag, String msg) {return println_native(LOG_ID_MAIN, DEBUG, tag, msg);}public static int d(String tag, String msg, Throwable tr) {return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));}public static int i(String tag, String msg) {return println_native(LOG_ID_MAIN, INFO, tag, msg);}public static int i(String tag, String msg, Throwable tr) {return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));}public static int w(String tag, String msg) {return println_native(LOG_ID_MAIN, WARN, tag, msg);}public static int w(String tag, 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 tag, String msg, Throwable tr) {return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));}................................................................../**@hide */ public static native int println_native(int bufID,int priority, String tag, String msg);}

Therefore, to use the Java log interface, you only need to define the log_tag constant in the class and reference Android. util. log:

Private Static final string log_tag = "my_log_tag ";

Log. I (log_tag, "this is the log printed by log. I in Android user space .");

To view the output of these logs, you can use the logcat tool. If you run the simulator in eclipse and installed the android plug-in, you can view it directly in Eclipse:

If it is used in a self-compiled Android source code project, run the simulator in the background:

User-name @ machine-Name :~ /Android $ emulator &

Start the ADB shell tool:

User-name @ machine-Name :~ /Android $ ADB Shell

Use the logcat command to view logs:

Root @ Android:/# logcat

In this way, you can see the output log.

Lao Luo's Sina Weibo: http://weibo.com/shengyangluo. welcome to the attention!

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.