Transferred from: http://blog.csdn.net/yihongyuelan/article/details/46409389
What is isloggable?
In the Android source code, we can often see the following codes:
[Java]View PlainCopy
- Packages/apps/incallui/src/com/android/incallui/log.java
- Public static final String TAG = "Incall";
- Public Static final Boolean DEBUG = Android.util.Log.isLoggable (TAG, Android.util.Log.DEBUG);
- Public static void D (string tag, string msg) {
- if (DEBUG) {
- ANDROID.UTIL.LOG.D (tag, delimit (tag) + msg);
- }
- }
- Packages/apps/incallui/src/com/android/incallui/incallpresenter.java
- Private Incallstate Startorfinishui (Incallstate newstate) {
- LOG.D (This, "Startorfinishui:" + mincallstate + "+ newstate");
- //... ...
- }
The Startorfinishui Log can be output normally only if the Android.util.Log.isLoggable return value is true. When will the Isloggable return true? How to quickly and easily open the isloggable and make log output normally?
This article is from Http://blog.csdn.NET/yihongyuelan reprint please be sure to indicate the source
Isloggable definition
What is isloggable? Isloggable is a method provided by Android.util.Log that checks the level of the specified tag, satisfies the output condition, returns true if satisfied, and returns false instead. Isloggable is defined in the source code as follows:
[Java]View PlainCopy
- /**
- * Checks to see whether or not a log for the specified tag was loggable at the specified level.
- *
- * The default level of any tag are set to INFO. This means, above and including
- * INFO'll be logged. Before calls to a logging method you should check to see
- * If your tag should be logged. You can change the default level by setting a system property:
- * ' SetProp log.tag.<your_log_tag> <LEVEL> '
- * Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or suppress. Suppress would
- * Turn off all logging for your tag. You can also create a local.prop file the
- * Following in it:
- * ' log.tag.<your_log_tag>=<level> '
- * and place that in/data/local.prop.
- *
- * @param tag the tag to check.
- * @param level to check.
- * @return Whether or not, this was allowed to be logged.
- * @throws IllegalArgumentException is thrown if the tag.length () > 23.
- */
- Public static native boolean isloggable (String tag, int. level);
From the above definition, you can know:
1. Isloggable default level is Android.util.Log.INFO;
2. Only level >= info can be output, that is, level >= info isloggable returns True, and vice versa returns false;
3. You can change the default level of log by SetProp log.tag.<your_log_tag> <LEVEL>, such as adb shell SetProp log.tag.InCall D. These attributes can also be written to/data/local.prop in the form of log.tag.incall=d;
4. If the length of the tag exceeds 23 characters, the IllegalArgumentException exception will be thrown;
The 6 level of log is defined in the Android.util.Log class, as follows:
[Java]View PlainCopy
- /**
- * 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;
The size of the log,level values of 6 levels corresponding to different grades increases with the increase of the log weight.
Isloggable Enable method
As mentioned earlier, isloggable is commonly used in custom log classes to determine whether to output log, such as code:
[Java]View PlainCopy
- Packages/apps/incallui/src/com/android/incallui/log.java
- Public static final String TAG = "Incall";
- Public Static final Boolean DEBUG = Android.util.Log.isLoggable (TAG, Android.util.Log.DEBUG);
- Public static void D (string tag, string msg) {
- if (DEBUG) {
- ANDROID.UTIL.LOG.D (tag, delimit (tag) + msg);
- }
- }
- Packages/apps/incallui/src/com/android/incallui/incallpresenter.java
- Private Incallstate Startorfinishui (Incallstate newstate) {
- LOG.D (This, "Startorfinishui:" + mincallstate + "+ newstate");
- //... ...
- }
If you want to output log in the Startorfinishui () method, you can do this in the following ways:
①. Modify the LOG.D to ANDROID.UTIL.LOG.D , use the Android.util.Log class instead of the Incallui custom Log class, recompile the system app and push it to the phone;
②. Modify the debug limit in the Com.android.incallui.Log , set the value of debug directly to True, or comment out if (debug). can also be isloggable (TAG, Android.util.Log.DEBUG) The Android.util.Log.DEBUG in is modified to Android.util.Log.INFO, which increases the level of Log, so that isloggable () returns True and the debug value is true;
③. Set Log.tag.InCall property values , SetProp log.tag.InCall D through adb shell, or write "Log.tag.incall=d" to/data/local.prop ( Do not include quotation marks, such as Local.prop does not exist, you need to create it yourself, set the permission to 644). Depending on the description in the definition, setting the property value needs to restart the Incallui process before the method is called. By modifying the method of the property value so that the return value of Isloggable () is true, the debug value is true.
The first way to directly modify, simple and rude, but if the number of changes is more cumbersome, and need to recompile the code.
The second way to modify points is uniform, but as with the first one, it also needs to be recompiled.
The third way to use attributes makes Isloggable return true, which is convenient and works well for user/userdebug/eng versions of devices. This method needs to be set Log.tag.InCall before the method is called, through the code:
[Java]View PlainCopy
- Public Static final Boolean DEBUG = Android.util.Log.isLoggable (TAG, Android.util.Log.DEBUG);
As you can tell, because debug is a static variable, its value is set when the log class is loaded. If you want to make isloggable return to true, then SetProp needs to be set before the log class is loaded, so you need to restart the corresponding process after using SetProp, such as the com.android.incallui here. But some code in the framework also uses isloggable,framework to belong to each process, how do you restart the framework? You can use:
[Java]View PlainCopy
- ADB shell stop
- ADB shell start
The ADB shell stop kills the zygote process and all the child processes hatched by zygote. ADB shell Start restarts the zygote process, and the zygote process launches other Android core processes. When Zygote restarts, the framework-related resources are reloaded, and the properties are already set. This method is simple, but when the device restarts, all of the settings of the log.tag.<tag> will fail, and if you want to enable it again, you need to reset it.
It is also possible to add Log.tag.incall=d to the/data/local.prop file, which is similar to SetProp, which is to set the property value, but the advantage of this is that the Log.tag.InCall is still valid if the device is restarted after Setup. However, because of the permissions control of the/data directory, only the Userdebug/eng version can modify the/data/local.prop file. [/SYSTEM/CORE/INIT/PROPERTY_SERVICE.C] the load_override_properties () function has a/data/local.prop enable method.
Summary
The Enable method for Isloggable is as follows:
Figure 1 Comparison of isloggable enable scheme
If you just want to enable the Isloggable method of a process, setting properties through SetProp is a good choice, colleagues, if you want to restart the device is still valid, you need to set/data/local.prop, but/data directory only in userdebug/ Can only be written in the Eng version. If you want to set the Log.tag property in the user version and still work after a reboot, you can do the following:
1. Get root access to the user version of the system;
2. Append the Log.tag.incall=d to the/system/build.prop file;
3. ADB reboot restart the device;
The above plan you may be puzzled, since the user version gets root permissions, why not directly modify/data/local.prop? This is because if the user version is ro.debuggable=0, the/data/local.prop file will not be read when the system starts, and eventually the settings Log.tag property will not take effect. Detailed principles will be explored in the next article, "How does Android 5.0 correctly enable isloggable (i) __ principle analysis".
Android 5.0 How to correctly enable Isloggable (a) __ use detailed