Example of the log method added to Android source code debugging

Source: Internet
Author: User

In the process of viewing the Android source code, just look at the code, often there is no way to verify that the estimation of the code is accurate, then we often by inserting their own log to test whether a function is called, the value of a parameter in the run process. The following jwisp the method of adding a log statement to the Android layers as follows.

1. Java code

In the Android source, as long as the Java code is basically in the framework, all the Java code, the way to join the logo is three steps:

(a) Import the log packet

Import Android.util.Log;

(b) Define the log label

The first sentence of many Java classes already defines the tag for the current class, such as the first sentence in the Devicestoragemonitorservice.java class

private static final String TAG = "Devicestoragemonitorservice";

Of course, we can also define our own tags so that from top to bottom, we only print our own log of concern

private static final String MyTag = "Mytagtest";

(c) Add log statement, the output of log is: V, I, D, W, E five levels

For example, we need to print out the log after each charge change in the battery management Batteryservice.java, how to do it?

Find the update () method in Batteryservice.java (Frameworks/base/services/java/com/android/server), where you add the following code:

if (mbatterylevel! = mlastbatterylevel) {log.i (MyTag, "batteryservice.java-update:mbatterylevel =" +mBatteryLevel );}

After the device or emulator is running, simply type the following command to print the most current battery value when the battery is changed.

Logcat-s Mytagtest

2. C + + code

In a variety of JNI, HAL layer, drive layer are C and C + + code, here to insert log, can help us find the top of the call down the number of parameters, which is called the specific function and so on.

Many people on the internet to give advice to modify the Android.mk file, in fact, far from the trouble.

(a) Add tag tag, many tags have been added, we can comment out the system tag, and then add their own tag

For example, in the distance sensor ProximitySensor.cpp in the HAL layer

Before the top include of the code

#define LOG_TAG "Proximitysensor"

(b) Introduction of the Log Library

#include <utils/Log.h>

(c) Adding a log statement

In this step, unlike Java, can not be added to a different tag tag can only use the current source file label

such as Jwisp want to output from the distance sensor, each time from the driver escalated Psensor value is how much

Adding a while loop in the Readevents function

Logi ("ProximitySensor.cpp, Readevent (): Event->value =%d", event->value);

3. Add log to the Mk file

Many people do not know, the Mk file at the time of operation (that is, the Android source code compile time) can also output log, which greatly facilitates us to debug the compilation information.

There are two types of log-level error and warning in the make file:

$ (Error TEXT ...) $ (Warning TEXT ...)

Error can be used to accurately locate the log information we need, because the compilation will terminate, in general, where the termination will have our log information,

Warning will not terminate on our log information, you can choose according to their own circumstances.

In addition, you can also include the MK-defined variable information in the log statement of the compiled output

For example, for example, we need to know what name is called after so compiled in the libsensors directory and output log by adding the following code to the Android.mk file in this directory:

After the definition of Logcal_module, write

$ (Error module is $ (local_module))

Output Result:

HARDWARE/SANSUNG/LIBSENSORS/ANDROID.MK:27: * * * module is Sensors.sansung. Stop.

Over

Example of the log method added to Android source code debugging

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.