Welcome Reprint! When reproduced, please indicate the source:http://blog.csdn.net/nfer_zhuang/article/details/44701657
Introduction
Usage scenarios:
I occasionally need to develop or debug the apk in my usual work, the focus is on the application of the Logcat output, but also because very reluctant to open ADT this eat memory large, then I chose to directly in the serial port input logcat and combined with grep filter out the process required to debug all the log. One of the key points in this is that you need to kill and restart the app multiple times during the debugging process, that is, the app's process ID is not a fixed value throughout the debugging process.
General usage:
- Input Logcat
- View the currently running APK process ID
- Save the process ID to a temporary variable
- Using Logcat | grep PID filters out all log of the process
Sentence script usage:
Pid= ' logcat-d-S XXX | Tail-n 1 | Sed ' s/.* (*\ ([0-9]*\)). */\1/' && logcat-v Time | grep--color $PID
The above script decomposition steps are:
First output the logcat of the specified tag
Then intercept the last line (if the first line is intercepted, it is likely the log information of the last process)
Filter out the process number
Output log again and filter using process number
logcat-d-S XXX description
-D Dump the log and then exit (Don ' t block)
-S Set default filter to silent.
Using the-d parameter means that only the current logcat is required for dump, because in the first two steps we are trying to get the process number to the app, and if you do not use the-D parameter, you will not be able to get the last line after using the pipe.
The-s parameter is any tag in the specified application. Some people have doubts, why not directly using the tag to filter, because: usually in an application, each submodule will have a separate tag to identify.
Tail-n 1 Description
-N,--lines=k output the last K-lines, instead of the last 10;
Tail command and Head command instead, the default is to output the last 10 lines of content, we only need the last line here, the use of the-N 1 parameter.
Sed ' s/.* (*\ ([0-9]*\)). */\1/' Description
First look at logcat-d-S XXX | Output after Tail-n 1:
V/xxx (238): This is a test log ...
Here we use SED to remove the part of the process number in parentheses, here are two ways to do it:
Idea one: match the contents of the parentheses (the method is used in the script in the preceding sentence)
Idea two: Delete the content outside the brackets (that is, leave the parentheses inside)
Both of these methods are not complicated, please choose your own.
Logcat-v Time | grep--color $PID Description
This part is simple, that is, the logcat output is then filtered with grep, but the--color parameter is used here, which is actually highlighting the results of grep. It also makes the output of All Blacks add a little color.
Summarize
The main part of the script is the use of SED to filter out the target string, and the method of passing the numeric value through a temporary variable.
Another benefit of writing this blog is that I learned another way to filter out the target content:
Pid= ' logcat-d-S P2plib | Tail-n 1 | Sed ' s/[^)]* (*\|) [^ (]*//g ' && logcat-v time | grep--color $PID
The logcat of a sentence script series filters the log of a specified process