In Android software development, increase the role of the log is very important, easy for us to understand the implementation of the program and data.Eclipse development tools will provide a visualization tool, but still feel that the terminal efficiency will be higher, so they wrote a python script to filter the log of a particular program through the package name.
principle
Get the corresponding process ID from the package name (there may be more than one), then use adb logcat to filter the process ID to get the log of the corresponding program.
source code (computing)
#!/usr/bin/env python
#coding:utf-8
#This script is aimed to grep logs by application(User should input a packageName and then we look up for the process ids then separate logs by process ids).
import os
import sys
packageName=str([1])
command = "adb shell ps | grep %s | awk '{print $2}'"%(packageName)
p = (command)
##for some applications,there are multiple processes,so we should get all the process id
pid = ().strip()
filters = pid
while(pid != ""):
pid = ().strip()
if (pid != ''):
filters = filters + "|" + pid
#print 'command = %s;filters=%s'%(command, filters)
if (filters != '') :
cmd = 'adb logcat | grep --color=always -E "%s" '%(filters)
(cmd)
Usage
python
Latest Code
#!/usr/bin/env python
#coding:utf-8
#This script is aimed to grep logs by application(User should input a packageName and then we look up for the process ids then separate logs by process ids).
import os
import sys
packageName=str([1])
command = "adb shell ps | grep %s | awk '{print $2}'"%(packageName)
p = (command)
##for some applications,there are multiple processes,so we should get all the process id
pid = ().strip()
filters = pid
while(pid != ""):
pid = ().strip()
if (pid != ''):
filters = filters + "|" + pid
#print 'command = %s;filters=%s'%(command, filters)
if (filters != '') :
cmd = 'adb logcat | grep --color=always -E "%s" '%(filters)
(cmd)
not enough
When the script is executed, if the Android program is shut down or restarted, resulting in a change in the process ID, it cannot automatically continue to output logs, and can only execute this script again.