当前位置:   article > 正文

Android logcat日志分析_安卓 processbuilder 執行locgcat命令

安卓 processbuilder 執行locgcat命令


一、简介

logcatandroid 中的一个命令行工具,可以用于得到程序的log信息。下面介绍 adb logcat 中的详细参数命令以及如何才能高效的打印日志,或者把日志保存到我们指定的位置。


二、logcat命令

可以输入 adb logcat --help,查看一下一些简单的数据格式:

adb logcat --help
  • 1

输出结果:

Usage: logcat [options] [filterspecs]

General options:
  -b, --buffer=<buffer>       Request alternate ring buffer(s):
                                main system radio events crash default all
                              Additionally, 'kernel' for userdebug and eng builds, and
                              'security' for Device Owner installations.
                              Multiple -b parameters or comma separated list of buffers are
                              allowed. Buffers are interleaved.
                              Default -b main,system,crash,kernel.
  -L, --last                  Dump logs from prior to last reboot from pstore.
  -c, --clear                 Clear (flush) the entire log and exit.
                              if -f is specified, clear the specified file and its related rotated
                              log files instead.
                              if -L is specified, clear pstore log instead.
  -d                          Dump the log and then exit (don't block).
  --pid=<pid>                 Only print logs from the given pid.
  --wrap                      Sleep for 2 hours or when buffer about to wrap whichever
                              comes first. Improves efficiency of polling by providing
                              an about-to-wrap wakeup.

Formatting:
  -v, --format=<format>       Sets log print format verb and adverbs, where <format> is one of:
                                brief help long process raw tag thread threadtime time
                              Modifying adverbs can be added:
                                color descriptive epoch monotonic printable uid usec UTC year zone
                              Multiple -v parameters or comma separated list of format and format
                              modifiers are allowed.
  -D, --dividers              Print dividers between each log buffer.
  -B, --binary                Output the log in binary.

Outfile files:
  -f, --file=<file>           Log to file instead of stdout.
  -r, --rotate-kbytes=<n>     Rotate log every <n> kbytes. Requires -f option.
  -n, --rotate-count=<count>  Sets max number of rotated logs to <count>, default 4.
  --id=<id>                   If the signature <id> for logging to file changes, then clear the
                              associated files and continue.

Logd control:
 These options send a control message to the logd daemon on device, print its return message if
 applicable, then exit. They are incompatible with -L, as these attributes do not apply to pstore.
  -g, --buffer-size           Get the size of the ring buffers within logd.
  -G, --buffer-size=<size>    Set size of a ring buffer in logd. May suffix with K or M.
                              This can individually control each buffer's size with -b.
  -S, --statistics            Output statistics.
                              --pid can be used to provide pid specific stats.
  -p, --prune                 Print prune rules. Each rule is specified as UID, UID/PID or /PID. A
                              '~' prefix indicates that elements matching the rule should be pruned
                              with higher priority otherwise they're pruned with lower priority. All
                              other pruning activity is oldest first. Special case ~! represents an
                              automatic pruning for the noisiest UID as determined by the current
                              statistics.  Special case ~1000/! represents pruning of the worst PID
                              within AID_SYSTEM when AID_SYSTEM is the noisiest UID.
  -P, --prune='<list> ...'    Set prune rules, using same format as listed above. Must be quoted.

Filtering:
  -s                          Set default filter to silent. Equivalent to filterspec '*:S'
  -e, --regex=<expr>          Only print lines where the log message matches <expr> where <expr> is
                              an ECMAScript regular expression.
  -m, --max-count=<count>     Quit after printing <count> lines. This is meant to be paired with
                              --regex, but will work on its own.
  --print                     This option is only applicable when --regex is set and only useful if
                              --max-count is also provided.
                              With --print, logcat will print all messages even if they do not
                              match the regex. Logcat will quit after printing the max-count number
                              of lines that match the regex.
  -t <count>                  Print only the most recent <count> lines (implies -d).
  -t '<time>'                 Print the lines since specified time (implies -d).
  -T <count>                  Print only the most recent <count> lines (does not imply -d).
  -T '<time>'                 Print the lines since specified time (not imply -d).
                              count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'
                              'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format.
  --uid=<uids>                Only display log messages from UIDs present in the comma separate list
                              <uids>. No name look-up is performed, so UIDs must be provided as
                              numeric values. This option is only useful for the 'root', 'log', and
                              'system' users since only those users can view logs from other users.

filterspecs are a series of
  <tag>[:priority]

where <tag> is a log component tag (or * for all) and priority is:
  V    Verbose (default for <tag>)
  D    Debug (default for '*')
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent (suppress all output)

'*' by itself means '*:D' and <tag> by itself means <tag>:V.
If no '*' filterspec or -s on command line, all filter defaults to '*:V'.
eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.

If not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.

If not specified with -v on command line, format is set from ANDROID_PRINTF_LOG
or defaults to "threadtime"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

2.1 adb logcat 命令格式

adb logcat [<option>] ... [<filter-spec>] ...
adb logcat [选项...] [过滤项...],
  • 1
  • 2

2.2 adb logcat 命令参数

选项描述举例
-s输出指定 tag 的日志,相当于过滤器表达式 *:Slogcat -s tag
-f <file>设置logcat 内容保存的位置,默认是stdoutlogcat -f sdcard/log.txt
-r <kbytes>每输出 时轮替日志文件,默认是16 必须配合 -flogcat -f sdcard/log.txt -r 1
-n <count>设置日志输出的最大数目, 需要 -r 参数
-v <format>设置日志的输出格式,注意只能设置一项;详见下文 格式化输出logcat -v threadtime
-D输出各个日志缓冲区之间的分隔线logcat -D …
-c清除(清空)所选的缓冲区并退出,默认清除 mainsystemcrashlogcat -c;logcat -b all -c
-d将日志转储到屏幕并退出logcat -d > log.txt
-e <expr>输出正则匹配的日志消息logcat -e [匹配数据] -m 5
-m <count>输出 行后退出logcat -m 5
-t <count>仅输出最新的行数,此选项包括 -d 功能logcat -t 5
-t '<time>'输出自指定时间以来的最新行,此选项包括 -d 功能logcat -t ‘01-26 20:52:41.820’
-g获取指定日志缓冲区的大小并退出logcat -g
-G设置日志环形缓冲区的大小,可以在结尾处添加 KMlogcat -G 2M
-b加载可供查看的日志缓冲区,更多可见下文 日志缓冲区logcat -b system
-B以二进制文件形式输出日志
-S在输出中包含统计信息,以识别和定位日志垃圾信息发送者
--pid=<pid>仅输出来自给定 PID 的日志logcat --pid=4321

2.3 adb logcat 日志缓冲区

Android 日志系统为日志消息保留了多个环形缓冲区,但并非多有的日志消息都会发送到默认的环形缓冲区。这里可以采用 logcat -b 命令查看设备的其他缓冲区:

缓冲区描述举例
radio输出通信系统的日志,包含无线装置/电话相关消息logcat -b radio
events输出event模块的日志logcat -b events
main主日志缓冲区(默认),不包含系统和崩溃日志消息logcat -b main
system输出系统日志logcat -b system
crash输出崩溃日志logcat -b crash
all输出所有缓冲区日志logcat -b all
default输出main、system、crash缓冲区日志logcat -b default

2.4 adb logcat 格式化输出

使用 -v 命令来修改 log 的输出格式,以显示特定的元数据字段:

格式描述
brief显示优先级、标记以及发出消息的进程的 PID
long显示所有元数据字段,并使用空白行分隔消息
process仅显示 PID
raw显示不包含其他元数据字段的原始日志消息
tag仅显示优先级和标记
thread旧版格式,显示优先级、PID 以及发出消息的线程的 TID
threadtime(默认值)显示日期、调用时间、优先级、标记、PID 以及发出消息的线程的 TID
time显示日期、调用时间、优先级、标记以及发出消息的进程的 PID
color使用不同的颜色来显示每个优先级
descriptive显示日志缓冲区事件说明。此修饰符仅影响事件日志缓冲区消息,不会对其他非二进制文件缓冲区产生任何影响
epoch显示自 1970 年 1 月 1 日以来的时间(以秒为单位)
uid如果访问控制允许,则显示 UID 或记录的进程的 Android ID
usec显示精确到微秒的时间
UTC显示 UTC 时间
year将年份添加到显示的时间
zone将本地时区添加到显示的时间

对于 -v 选项,如果需要查看线程区别使用 logcat -v threadtime 就可以了,其他日志基本也是少用的。

下面列举几种输出格式进行说明。


2.4.1 logcat -v brief

格式:

<priority>/<tag>(<pid>): <message>
  • 1

示例:

D/TAG( 1785): Disconnected process message: 10, size: 0
  • 1

2.4.2 logcat -v long

格式:

[ <datetime> <pid>:<tid> <priority>/<tag> ]
  • 1

示例:

[ 08-28 22:39:39.974  1785: 1832 D/TAG ] Disconnected process message: 10, size: 0
  • 1

2.4.3 logcat -v process

格式:

<priority>(<pid>) <message>
  • 1

示例:

D( 1785) Disconnected process message: 10, size: 0 
  • 1

2.4.4 logcat -v tag

格式:

<priority>/<tag>: <message>
  • 1

示例:

D/TAG: Disconnected process message: 10, size: 0
  • 1

2.4.5 logcat -v raw

格式:

<message>
  • 1

示例:

Disconnected process message: 10, size: 0
  • 1

2.4.6 logcat -v time

格式:

<datetime> <priority>/<tag>(<pid>): <message>
  • 1

示例:

08-28 22:39:39.974 D/TAG( 1785): Disconnected process message: 10, size: 0
  • 1

2.4.7 logcat -v threadtime

格式:

<datetime> <pid> <tid> <priority> <tag>: <message>
  • 1

示例:

08-28 22:39:39.974  1785 1832 D TAG: Disconnected process message: 10, size: 0
  • 1

2.5 adb logcat 日志级别

按日志级别过滤日志:

adb logcat <tag>[:priority]
  • 1

tag 表示标签;priority 表示输出级别;

日志默认级别是 V,如果错误日志我们选择 E 就可以,Android 的日志分为如下几个优先级(priority):

选项描述举例
V–Verbose(最低优先级)adb logcat *:V
D– Debugadb logcat *:d
I– Infoadb logcat *:I
W– Warningadb logcat *:W
E– Erroradb logcat *:E
F– Fataladb logcat *:F
S– Silentadb logcat *:S

*可以是某个tag,如果没有指明,就表示所有。


三、adb logcat 示例

多数情况下,logcat命令参数都是组合使用。


3.1 adb logcat -b all -v threadtime --pid=4321 > /Users/xxx/logcat.log

将进程ID为4321的应用,所有缓冲区日志,按照 threadtime 格式输出到 logcat.log 日志文件中。

adb logcat -b all -v threadtime --pid=4321 > /Users/xxx/logcat.log
  • 1

3.2 adb logcat -b all -c

将所有缓冲区日志清空。

adb logcat -b all -c
  • 1

3.3 adb logcat -b all -d -v threadtime --pid=4321 -t 10

将进程ID为4321的应用,所有缓冲区日志,按照 threadtime 格式输出最新的10行日志(最多10行)。

adb logcat -b all -d -v threadtime --pid=4321 -t 10
  • 1

四、Android 代码操作logcat日志

4.1 Android代码读取logcat日志

private void getLogcat(){
	String[] comand = new String[]{"logcat", "-b", "all", "-d", "-v", "threadtime", "--pid=4321", "-t", "10"};
    try{
        Process exec = Runtime.getRuntime().exec(comand);
        BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        int i = 0;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\r\n");
        }
        Log.i("TAG", "logcat content = " + sb.toString());
    }catch (Exception ex){
        Log.e("TAG", "Exception = " + ex.toString());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

4.2 Android代码实现将logcat日志输出到文件

public class LogcatProcesser {
	private Process logcatToFileProcess;

	/**
     * 获取日志输出文件
     */
	private String getLogcatFile(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "cwlogcat";
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        return new File(file.getAbsolutePath() + File.separator + "cw_logcat.log").getAbsolutePath();
    }
    
	/**
     * 开启日志输出到文件
     */
	private void startLogcatToFile(){
        int pid = android.os.Process.myPid();
        String path = getLogcatFile();
        try{
            String[] cmds = new String[]{"logcat","-b", "all", "-v", "threadtime", "--pid=" + pid, "-f", path};
            if(logcatToFileProcess == null) {
            	// 持续将日志输出到日志文件中
                ProcessBuilder processBuilder = new ProcessBuilder(cmds);
                // 启动进程并重定向输出
                logcatToFileProcess = processBuilder.start();
                // 读取进程的错误流,以防止阻塞
                readStream(logcatToFileProcess.getErrorStream());
            }
        }catch (Exception ex){
            Log.e("TAG", "Exception = " + ex.toString());
        }
    }
    
    /**
     * 停止日志输出到文件
     */
	private void stopLogcatToFile(){
        try{
            if(logcatToFileProcess == null){
                return;
            }

            logcatToFileProcess.destroy();
            logcatToFileProcess = null;
        }catch (Exception ex){
            Log.e("TAG", "Exception = " + ex.toString());
        }
    }

	/**
	 * 读取进程的错误流
	 */
	private void readStream(final java.io.InputStream is) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] buffer = new byte[1024];
                    while (is.read(buffer) != -1) {
                        // 读取并忽略输入流中的数据
                    }
                    is.close();
                } catch (IOException e) {
                    Log.e("TAG", "Exception = " + ex.toString());
                }
            }
        }).start();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/318109
推荐阅读
相关标签
  

闽ICP备14008679号