How to filter adb logcat output (reprint)

Source: Internet
Author: User
Tags egrep

Introduction :

This article describes several tips on how to filter the ADB logcat output from the shell command line .

Development often see other people's log like a flood of instant fill the screen, to their useful information is drowned, affect the mood also affect efficiency. Here are a few of the filtering methods I know.

1. Show only the required output, whitelist

The most convenient of course is to use grep filtering through the pipeline, which can be matched using grep's powerful regular expression. Simply match a string in a row, such as MyApp:

ADB Logcat | | grep-| grep--color=auto-i  MyApp #设置匹配字符串颜色. For more settings, see grep Help.

The advanced point can be matched using grep's regular expression. For example, the previous example matches the MyApp anywhere in a row, and can be set to match tag only. The default log output is as follows, and the corresponding expression is modified if the output format has been modified.

I/cacheservice (  665 for all thumbnails.

You can see that the tag starts with the third character at the beginning of a line and writes the expression according to this:

" ^.. MYAPP"

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

" ^e.myapp "

Of course, you can match more than one, using | Split multiple matching expressions to add escape characters. For example, to match the output of the tag for MyApp and myactivity:

" ^.. myapp\|^. MyActivity""^.. myapp|^. MyActivity"  #使用 egrep without escape character
2. Filter unwanted output, blacklist

or using grep, use the same as above, plus a-v. For example, to filter the output of the tag for MyApp and myactivity:

" ^.. myapp\|^. MyActivity""^.. myapp|^. MyActivity"  #使用 egrep without escape character
3. Show all outputs of the same process

Sometimes a program has more than one tag, you need to output the program (the same PID) of all the tag; using only tag filtering sometimes misses some error messages, and the general error message is the same PID as the program. 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 Java package name of the program (such as Com.android.media).

#!/bin/bashpackagename=$1pid'{print $}'| grep--color=auto $pid
4. Show from current start

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

ADB logcat-c && adb logcat
5. Filter log files

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

" ^.. myapp\|^. MyActivity" > Newmyapp.log

Windows recommends using notepad++, a free and powerful notepad, to support regular expression lookup replacements. You can highlight matches or delete unwanted content.

The above technique mainly uses the grep, actually LOGCAT itself also has the filtering function, may filter the log according to the tag, the priority level, specifically please refer to the Android Official document Reading and Writing Logs. If you prefer to use the graphical interface, please refer to the logcat inside the using DDMS,DDMS can also be filtered.

How to filter adb logcat output (reprint)

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.