Several ways to filter adb logcat output in the Android shell command line _android

Source: Internet
Author: User
Tags log log egrep

We can always see in Android development that the log log content of the program is full of screens, and the information that really makes sense to the developer is submerged in the torrent, leaving developers at a loss and seriously impacting development efficiency. This article introduces several ways to filter adb logcat output in the shell command line.

1, only show the required output (white list)

The easiest thing to do, of course, is to use grep filtering through the pipes, so that you can use grep's powerful regular expression to match. A simple match for a string in a row, such as MyApp:

    1. ADB Logcat | grep MyApp
    2. ADB Logcat | Grep-i MyApp #忽略大小写.
    3. ADB Logcat | grep--color=auto-i MyApp #设置匹配字符串颜色. For more settings, see grep Help.

An advanced point can be used to match grep's regular expression. For example, the previous example would match the MyApp of any position in a row and could be set to match only the tag. The default log output is as follows, and if you modify the output format the corresponding expression will also be modified.

I/cacheservice (665): Preparing diskcache for all thumbnails.

You can see that tag is the beginning of the third character at the beginning of a line, writing an expression based on this:

ADB Logcat | grep "^.. MyApp "

Depending on this format, you can also set a log that displays only one priority, and then the first character at the beginning of the line. For example, only output with the Error level tag as MyApp is displayed:

ADB Logcat | grep "^e.myapp"

Of course, you can also match multiple, use | Splits multiple matching expressions with an escape character. For example, to match the output of the tag as MYAPP and myactivity:

ADB Logcat | grep "^.. myapp\|^.. MyActivity "
ADB Logcat | Grep-e "^. myapp|^.. MyActivity "#使用 egrep without escape characters

2, the filter does not need the output (blacklist)

Or use grep, the same usage as above, plus a-V can be. For example, to filter the output of tag for MyApp and myactivity:

ADB Logcat | Grep-v "^. myapp\|^.. MyActivity "
ADB Logcat | Grep-ve "^. myapp|^.. MyActivity "#使用 egrep without escape characters

3, show all output of the same process

Sometimes a program inside the tag has more than one, need to output the program (the same PID) of all the tag; only use tag filter sometimes will miss some error messages, and the general error message is the same as the program with the PID. or through grep, the idea is to first find the PID number according to the package name, and then match the PID. Write the shell script as follows, the parameter is the program's Java package name (such as Com.android.media).

  #!/bin/bash
  packagename=$1
  pid= ' adb shell PS | grep $packageName | awk ' {print $} '
  adb logcat | grep–color= Auto $pid

4, from the current start display

The Logcat has a cache, and if you only need to view the currently started log, you need to clear the previous one.

ADB logcat-c && adb logcat

5, filter log files

Sometimes you need to parse log files, filter log files, or use grep. For example, log file for Myapp.log, to match the tag for MyApp and myactivity output, and then output to Newmyapp.log:

Cat Myapp.log | grep "^.. myapp\|^.. MyActivity "> Newmyapp.log

Windows recommends the use of notepad++, a free and powerful notepad that supports regular expression lookup replacements. You can highlight matching content, or you can delete unwanted content.

The above techniques are mainly used grep, in fact Logcat itself also has filtering function, can be based on tag, priority filter log, please refer to the official Android documentation reading and writing Logs. If you prefer to use a graphical interface, refer to the Logcat in the using DDMS,DDMS to filter the same.

Above is the Android Shell command line to filter the ADB logcat output of several methods of data collation, follow-up continue to supplement the relevant information, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.