In the development of Android software, it is important to increase the function of log, so that we know the execution and data of the program. The Eclipse development tool provides a visual tool, but still feels that the terminal is more efficient, and then writes a Python script to filter the log of a program through the package name.
Principle
The corresponding process ID (possibly multiple) is obtained through the package name, and then the program's log is obtained by using ADB logcat to filter the process ID.
Source
The code is as follows:
#!/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 (Sys.argv[1])
Command = "adb Shell PS | grep%s | awk ' {print $} '% (PackageName)
p = os.popen (command)
# #for Some applications,there is multiple processes,so we should get all the process ID
PID = P.readline (). Strip ()
filters = PID
while (pid! = ""):
PID = P.readline (). Strip ()
if (pid! = "):
Filters = filters + "|" + PID
#print ' command =%s;filters=%s '% (command, filters)
if (Filters! = "):
cmd = ' adb logcat | grep--color=always-e "%s" '% (filters)
Os.system (CMD)
How to use
The code is as follows:
Python logcatpkg.py Com.mx.browser
Latest Code
The code is as follows:
#!/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 (Sys.argv[1])
Command = "adb Shell PS | grep%s | awk ' {print $} '% (PackageName)
p = os.popen (command)
# #for Some applications,there is multiple processes,so we should get all the process ID
PID = P.readline (). Strip ()
filters = PID
while (pid! = ""):
PID = P.readline (). Strip ()
if (pid! = "):
Filters = filters + "|" + PID
#print ' command =%s;filters=%s '% (command, filters)
if (Filters! = "):
cmd = ' adb logcat | grep--color=always-e "%s" '% (filters)
Os.system (CMD)
Insufficient
When the script is executed, if the Android program shuts down or restarts, causing the process ID to change and the output log cannot be automatically resumed, the script can only be executed again.