ADB Logcat Tool 和 DebugLevel的设定

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写入到配置文件中,这样只需要修改配置文件就可以控制输出的级别,更加方便.

时间: 2024-08-01 22:52:31

ADB Logcat Tool 和 DebugLevel的设定的相关文章

【LOGCAT】adb logcat -h的帮助信息

adb logcat -h的帮助信息 Usage: logcat [options] [filterspecs] options include:   -s              Set default filter to silent.                   Like specifying filterspec '*:s'   -f <filename>   Log to file. Default to stdout   -r [<kbytes>]   Rot

adb logcat 查看日志

使用 logcat 命令 查看和跟踪系统日志缓冲区的命令logcat的一般用法是: [adb] logcat [<option>] ... [<filter-spec>] ... 下文介绍过滤器和命令选项,详细内容可参见Listing of logcat Command Options.    可以在开发机中通过远程shell的方式使用logcat命令查看日志输出: $ adb logcat    如果是在远程shell中可直接使用命令: # logcat 过滤日志输出 每一条日志

adb logcat 基本用法

入门android ,至少需要了解 adb 吧,那么打 log 也是必不可少的了. 下面简单介绍一下 adb logcat 基本用法: Usage: logcat [options] [filterspecs]options include:  -s              Set default filter to silent.                  Like specifying filterspec '*:s'  -f <filename>   Log to file.

adb logcat通过包名过滤(dos命令find后跟变量)

adb命令中似乎没有直接通过报名来过滤的功能,但是可以通过过滤进程的pid来过滤该应用的日志 过滤条件:该app在运行 实现原理: 1.获取该app运行时的pid 2.通过find命令,过滤pid的日志,就是该包的运行日志 实现: 1.在同一目录建立一文件:getpid.bat @echo off adb shell "ps | grep com.example.testprogram" 复制以上代码,保存 2.在同一目录建立另外一个问文件:getpid1.bat @echo off

如何过滤 adb logcat 输出(转载)

转自:http://www.cnblogs.com/imouto/archive/2012/12/11/filtering-adb-logcat-output.html 简介: 本文介绍如何在 shell 命令行中过滤 adb logcat 输出的几个小技巧. 开发当中经常看到别人的 log 如洪水般瞬间刷满了屏幕,对自己有用的信息都被淹没了,影响心情也影响效率.下面是几个我所知道的过滤方法. 1. 只显示需要的输出,白名单 最方便的当然是通过管道使用 grep 过滤了,这样可以使用 grep

如何使用 adb logcat 查看某个进程的输出日志

adb logcat 默认是没有这个功能的,我实现了一个小bash函数,添加到你$HOME/.bashrc 文件中: # 作用:能够通过进程名显示log # 用法:alogcat com.android.calendar or alogcat calendar # 当监控的进程异常退出时,需要重新运行此命令 function alogcat() { OUT=$(adb shell ps | grep -i $1 | awk '{print $2}') OUT=$(echo $OUT | sed

几种在shell命令行中过滤adb logcat输出的方法

我们在Android开发中总能看到程序的log日志内容充满了屏幕,而真正对开发者有意义的信息被淹没在洪流之中,让开发者无所适从,严重影响开发效率.本文就具体介绍几种在shell命令行中过滤adb logcat输出的方法. 1.只显示需要的输出(白名单) 最方便的当然是通过管道使用 grep 过滤了,这样可以使用 grep 强大的正则表达式匹配.简单的匹配一行当中的某个字符串,例如 MyApp: adb logcat | grep MyApp       adb logcat | grep -i

android studio 中 adb logcat 在这里敲

官方视频和网络大都没有详细介绍命令行(IDE集成的那个比较容易看到)下 logcat 最初怎么进入,倒是具体用法详细的翻译了官网的文档. 所以记录如下: (1)下面的官网上写了 https://developer.android.com/studio/command-line/logcat.html#Syntax (科学上网) (2)摘要如下: Command-line Syntax [adb] logcat [<option>] ... [<filter-spec>] ... Y

adb logcat 查看日志 (转载)

转自:http://blog.csdn.net/xyz_lmn/article/details/7004710 使用 logcat 命令 查看和跟踪系统日志缓冲区的命令logcat的一般用法是: [adb] logcat [<option>] ... [<filter-spec>] ... 下文介绍过滤器和命令选项,详细内容可参见Listing of logcat Command Options.   可以在开发机中通过远程shell的方式使用logcat命令查看日志输出:   $