Android 5.0 怎样正确启用isLoggable(一)__使用具体解释
isLoggable是什么
在Android源代码中,我们常常能够看到例如以下代码:
//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);
//... ...
}
仅仅有在android.util.Log.isLoggable返回值为true的时候,startOrFinishUi的log才干正常输出。那什么时候isLoggable才会返回true呢?怎样简单快捷的开启isLoggable并使log正常输出呢?
本文来自http://blog.csdn.net/yihongyuelan 转载请务必注明出处
isLoggable定义
什么是isLoggable?isLoggable是android.util.Log提供的方法,用于检查指定TAG的level,是否满足输出条件,如满足则返回true反之则返回false。isLoggable在源代码中的定义例如以下:
/**
* Checks to see whether or not a log for the specified tag is loggable at the specified level.
*
* The default level of any tag is set to INFO. This means that any level above and including
* INFO will be logged. Before you make any 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 will
* turn off all logging for your tag. You can also create a local.prop file that with 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 The level to check.
* @return Whether or not that this is allowed to be logged.
* @throws IllegalArgumentException is thrown if the tag.length() > 23.
*/
public static native boolean isLoggable(String tag, int level);
从以上定义中能够知道:
1. isLoggable默认level为android.util.Log.INFO;
2. 仅仅有 level >= INFO才干输出,即level >= INFO时isLoggable返回true,反之则返回false;
3. 能够通过setprop log.tag.<YOUR_LOG_TAG> <LEVEL>来改变log的默认level,如adb shell setprop log.tag.InCall D。也能够将这些属性依照log.tag.InCall=D的形式,写入/data/local.prop中;
4. tag的长度假设超过23个字符则会抛出IllegalArgumentException异常;
在android.util.Log类中定义了Log的6种Level。例如以下:
/**
* 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;
6种level相应不同等级的log。level值的大小随着log权重的升高而增大。
isLoggable使能方法
前文提到,在自己定义的Log类中通常使用isLoggable来推断是否输出log,如代码:
//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);
//... ...
}
假设想要输出startOrFinishUi()方法中的log,能够採用下面方式:
①. 将Log.d改动为android.util.Log.d;使用android.util.Log类代替InCallUI自己定义的Log类,又一次编译系统APP并push到手机中就可以;
②. 改动com.android.incallui.Log中的DEBUG限制;将DEBUG的值直接置为true,或者凝视掉if(DEBUG)。也能够将isLoggable(TAG, android.util.Log.DEBUG)中的android.util.Log.DEBUG改动为android.util.Log.INFO,即将log的level增大,从而使isLoggable()返回true并使得DEBUG的值为true;
③. 设置log.tag.InCall的属性值;通过adb shell setprop log.tag.InCall D,或者将"log.tag.InCall=D"写入/data/local.prop中(不包括引號,如local.prop不存在则需自行创建,权限设为644)。依据定义中的描写叙述,设置属性值须要在方法被调用之前。因此须要重新启动InCallUI进程。通过改动属性值的方法。从而使得isLoggable()的返回值为true,从而使得DEBUG的值为true。
第一种方式直接改动,简单粗暴,但若改动点较多则比較麻烦。且须要又一次编译代码。
另外一种方式改动点统一,但和第一种一样,也须要又一次编译。
第三种使用属性的方式使得isLoggable返回true。这样的方式比較方便,同一时候适用于user/userdebug/eng各个版本号的设备。这样的方式须要在方法被调用前设置log.tag.InCall,通过代码:
public static final boolean DEBUG = android.util.Log.isLoggable(TAG, android.util.Log.DEBUG);
能够知道,由于DEBUG是static的变量,所以当Log类被载入时。其值就已经设置好了。
假设要使得isLoggable返回为true。那么setprop须要在Log类被载入前设置好,因此使用setprop之后须要重新启动相应的进程,如这里的com.android.incallui。但在framework中有些代码也使用isLoggable。framework属于每个进程。怎样重新启动framework呢?能够使用:
adb shell stop
adb shell start
adb shell stop会杀掉zygote进程以及全部由zygote孵化而来的子进程。adb
shell start则会重新启动zygote进程,再由zygote进程启动其他Android核心进程。
当zygote又一次启动时。会又一次载入framework相关资源,而此时属性已经设置。这样的方法尽管简单,但当设备重新启动后,全部设置的log.tag.<TAG>都会失效,若想再次启用则需又一次设置。
也能够通过将log.tag.InCall=D增加到/data/local.prop文件里,这样的方式与setprop类似。都是设置属性值,但这样的方式的优点是,设置之后重新启动设备该log.tag.InCall依旧有效。只是由于/data文件夹的权限控制,仅仅有userdebug/eng版本号才干够改动/data/local.prop文件。
小结
isLoggable的使能方法例如以下图所看到的:
图 1 isLoggable使能方案对照
假设仅仅是想启用某个进程的isLoggable方法,通过setprop设置属性是不错的选择,同事,想要重新启动设备后依旧有效则须要设置/data/local.prop,只是/data文件夹仅仅有在userdebug/eng版本号中才干够写入。那假设想在user版中设置log.tag属性并在重新启动后依旧有效,能够採取下面方法:
1. 获取user版系统的root权限;
2. 将log.tag.InCall=D追加到/system/build.prop文件里。
3. adb reboot重新启动设备;
以上方案大家可能会疑惑,既然user版获取到了root权限,为什么不直接改动/data/local.prop呢?这是由于假设是user版,则ro.debuggable=0,所以系统启动时不会读取/data/local.prop文件,并终于导致设置log.tag属性不会生效。
具体原理将在下一篇文章《Android 5.0 怎样正确启用isLoggable(一)__原理分析》中探讨。
Android 5.0 怎样正确启用isLoggable(一)__使用具体解释的更多相关文章
- Android 5.0 怎样正确启用isLoggable(二)__原理分析
前置文章 <Android 5.0 怎样正确启用isLoggable(一)__使用具体解释> 概要 在上文<Android 5.0 怎样正确启用isLoggable(一)__使用具体 ...
- Android 5.0 如何正确启用isLoggable(一)__使用详解
转自:http://blog.csdn.net/yihongyuelan/article/details/46409389 isLoggable是什么 在Android源码中,我们经常可以看到如下代码 ...
- Android5.0如何正确启用isLoggable(二) 理分析
转自:http://www.it165.net/pro/html/201506/43374.html 概要 在上文<Android 5.0 如何正确启用isLoggable(一)__使用详解&g ...
- Android 4.0以后正确的获取外部sd卡存储目录
刚解决这个棘手的问题 找了很久,随笔记下. 网上搜索 android 获取外部sd卡存储目录 普遍都是: 1) Environment.getExternalStorageDirectory() 这个 ...
- 【译】Android 6.0 Changes (机翻加轻微人工校对)
Android 6.0 Changes In this document Runtime Permissions Doze and App Standby Apache HTTP Client Rem ...
- Android 6.0 新功能及主要 API 变更
运行时权限 这个版本中引入了新的权限模型,现在用户可以在运行时直接管理应用程序的权限.这个模型基于用户对权限控制的更多可见性,同时为应用程序的开发者提供更流畅的应用安装和自动升级.用户可以为已安装的每 ...
- Android 6.0 Changes
原文链接:http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html 伴随着新特性和功能,Andr ...
- 还在期待安卓9.0吗?Android 10.0要来了
目前,美国 Google公司的 AndroidP (安卓9.0),已经正式全面推出有几个多月了.众多手机品牌厂商也都在积极的进行更新适配 Android 9.0 系统(修改UI界面也算是二次开发,嗯) ...
- Android 9.0更新
北京时间2018年8月7日上午,Google 发布了 Android 9.0 操作系统.并宣布系统版本 Android P 被正式命名为代号"Pie". Android 9.0 利 ...
随机推荐
- 【剑指offer】面试题 15. 二进制中 1 的个数
面试题 15. 二进制中 1 的个数 题目描述 题目:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. Java 实现 方法一 public class Solution { // y ...
- Windows10 Docker加速
参考地址:https://blog.csdn.net/wanderlustlee/article/details/80216588 在刚开始使用时,有可能因为网络的问题导致整个镜像的下载过程不是太顺畅 ...
- Python和xml简介
python提供越来越多的技术来支持xml,本文旨在面向初学利用Python处理xml的读者,以教程的形式介绍一些基本的xml出来概念.前提是读者必须知道一些xml常用术语. 先决条件 本文所有的例子 ...
- 洛谷P2751[USACO]工序安排
题目传送门 怎么说呢,这个题目我刚开始随便乱搞了几下,交了个暴力代码上去居然还水了49分,数据确实有点弱啊,然后看到洛谷上那位大佬Redbag的题解瞬间就佩服的五体投地,那真的是简洁.易懂又高效.直接 ...
- python脚本实现ipv6的ddns功能
话说在国家的推动下,ipv6地址已经开始优先在三大运营商推广,我自己家用的就是联通宽带100M光纤入户.有一次,不经意间发现,我们的光猫竟然获取到了ipv6地址,于是我开始浮想联翩. 说干就干,我先是 ...
- Trie树【P3879】 [TJOI2010]阅读理解
Description 英语老师留了N篇阅读理解作业,但是每篇英文短文都有很多生词需要查字典,为了节约时间,现在要做个统计,算一算某些生词都在哪几篇短文中出现过. Input 第一行为整数N,表示短文 ...
- sed 插入和替换
sed -i '/参考行/i\插入内容' *.ksh sed -i 's,原内容,替换后内容,g' *.ksh
- AvalonJS学习笔记(一)
一.关于AvalonJS avalon是国内的一个MVVM框架,是从knockout发展起来的 分为两个版本 avalon.js版本,支持IE6及非常老的标准浏览器.这里的标准浏览器特指W3C阵营中的 ...
- hdu 3547 (polya定理 + 小高精)
DIY CubeTime Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- Problem F: 逆序排列
#include<stdio.h> void sort(int *p,int n) { int i,t; ;i<n/;i++) { t=*(p+i); *(p+i)=*(p+n-i- ...