ADB Logcat Tool 和 DebugLevel的设定

来源:互联网 发布:美橙域名怎么样 编辑:程序博客网 时间:2024/06/06 01:18

1. logcat Base

ADB logcat 过滤方法(抓取日志)

2. 1 logcat tool

在《ADB logcat 过滤方法(抓取日志) 》这篇文章中提到了过滤log的一些方法,

但是这并不代表不输出日志,只是没有显示出来。

此外在某些情况下需要对log设置开关,不少人都是通过添加一条条的判断语句,这有些太低效,所以自己就搞了一个工具类.


2.2 原理

2.2.1 DEBUG_LEVEL

首先设置几个DebugLevel(和adb logcat 相对应)

    private static byte LOGE = 0x01;     //0000 0001    private static byte LOGW = 0x02;     //0000 0010    private static byte LOGI = 0x04;     //0000 0100    private static byte LOGD = 0x08;     //0000 1000    private static byte LOGV = 0x10;     //0001 0000

需要过滤的DebugLevel:

    private static byte DEBUG_LEVEL = 0xff;    

这样要判断该日志是否需要打印只要将二者相与即可, 

    DEBUG_LEVEL & LOGX
    DEBUG_LEVEL_E     0x01    0000 0001
    DEBUG_LEVEL_W     0x03    0000 0011
    DEBUG_LEVEL_I     0x07    0000 0111
    DEBUG_LEVEL_D     0x0f    0000 1111
    DEBUG_LEVEL_V     0x1f    0001 1111

比如:

    public static void i(String tag, String msg) {        if ((LOGI & DEBUG_LEVEL) != 0) {            tag = PROJECT_NAME + tag;            Log.i(tag, msg);        }    }

想要输出何种级别的日志只需要设定好DEBUG_LEVEL:

根据adb logcat 的机制设定了集中默认的DEBUG_LEVEL:

    public static byte DEBUG_LEVEL_N = 0x00;    //0000 0000    public static byte DEBUG_LEVEL_E = 0x01;    //0000 0001    public static byte DEBUG_LEVEL_W = 0x03;    //0000 0011    public static byte DEBUG_LEVEL_I = 0x07;    //0000 0111    public static byte DEBUG_LEVEL_D = 0x0f;    //0000 1111    public static byte DEBUG_LEVEL_V = 0x1f;    //0001 1111

其中

DEBUG_LEVEL_N: 不输出任何日志

DEBUG_LEVEL_V: 输出所有日志


2.2.2 TAG 标签

根据需求我扩展了adb的标签,将其分为2级:

第一级为项目标签:

private static String PROJECT_NAME = "PROJECT-";

第二级为模块标签tag

通过函数传入

    public static void i(String tag, String msg) {        if ((LOGI & DEBUG_LEVEL) != 0) {            tag = PROJECT_NAME + tag;            Log.i(tag, msg);        }    }<strong></strong>

2.2.3 过滤日志

这样我们可以过滤这个项目的日志,

adb logcat | grep -E '^./PROJECT'

也可以过滤某一模块的日志

adb logcat | grep -E '^./PROJECT-TOOL'

3. 实现代码

import android.util.Log;public class LogTool {    private static String PROJECT_NAME = "PROJECT-";    private static byte LOGE = 0x01;     //0000 0001    private static byte LOGW = 0x02;     //0000 0010    private static byte LOGI = 0x04;     //0000 0100    private static byte LOGD = 0x08;     //0000 1000    private static byte LOGV = 0x10;     //0001 0000    //DEBUG_LEVEL & LOGX    //DEBUG_LEVEL_E     0x01    0000 0001    //DEBUG_LEVEL_W     0x03    0000 0011    //DEBUG_LEVEL_I     0x07    0000 0111    //DEBUG_LEVEL_D     0x0f    0000 1111    //DEBUG_LEVEL_V     0x1f    0001 1111    public static byte DEBUG_LEVEL_N = 0x00;    //0000 0000    public static byte DEBUG_LEVEL_E = 0x01;    //0000 0001    public static byte DEBUG_LEVEL_W = 0x03;    //0000 0011    public static byte DEBUG_LEVEL_I = 0x07;    //0000 0111    public static byte DEBUG_LEVEL_D = 0x0f;    //0000 1111    public static byte DEBUG_LEVEL_V = 0x1f;    //0001 1111    private static byte DEBUG_LEVEL = 0x1f;     //0001 1111    public static void setUp(String projectTAG, byte debugLevel) {        if (projectTAG != null && projectTAG.length() > 0)            PROJECT_NAME = projectTAG + "-";        DEBUG_LEVEL = debugLevel;    }    public static void i(String tag, String msg) {        if ((LOGI & DEBUG_LEVEL) != 0) {            tag = PROJECT_NAME + tag;            Log.i(tag, msg);        }    }    public static void d(String tag, String msg) {        if ((LOGD & DEBUG_LEVEL) != 0) {            tag = PROJECT_NAME + tag;            Log.d(tag, msg);        }    }    public static void w(String tag, String msg) {        if ((LOGW & DEBUG_LEVEL) != 0) {            tag = PROJECT_NAME + tag;            Log.w(tag, msg);        }    }    public static void w(String tag, String msg, Throwable tr) {        if ((LOGW & DEBUG_LEVEL) != 0) {            tag = PROJECT_NAME + tag;            Log.w(tag, msg, tr);        }    }    public static void e(String tag, String msg) {        if ((LOGE & DEBUG_LEVEL) != 0) {            tag = PROJECT_NAME + tag;            Log.e(tag, msg);        }    }    public static void v(String tag, String msg) {        if ((LOGV & DEBUG_LEVEL) != 0) {            tag = PROJECT_NAME + tag;            Log.v(tag, msg);        }    }}

4. 使用实例

在使用前需要设定DEBUG_LEVEL和PROJECT_NAME

LogTool.setUp("MyProject", VideoLogger.DEBUG_LEVEL_V);

打印日志:

private final String TAG = "Main";LogTool.v(TAG, "Do Test >>>");

过滤日志参见:2.2.3


5. 扩展

当前的DEBUG_LEVEL是手动设定的, 进一步的话可以将DEBUG_LEVEL写入到配置文件中,这样只需要修改配置文件就可以控制输出的级别,更加方便.

0 0