This article mainly introduces how to filter the log scripts of a single Android program using Python. This article describes the principles, implementation code, usage methods, and the latest code, for more information about how to add logs, see Android software development. Eclipse development tools provide visual tools, but I still feel that the terminal efficiency is higher, so I wrote a python script to filter logs of a program by using the package name.
Principle
Obtain the corresponding process ID (possibly multiple) by using the package name, and then use adb logcat to filter the process ID to obtain the logs of the corresponding program.
Source code
The code is as follows:
#! /Usr/bin/env python
# Coding: UTF-8
# This script is aimed to grep logs by application (User shocould 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 $2}'" % (packageName)
P = OS. popen (command)
# For some applications, there are multiple processes, so we shocould 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)
Usage
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 shocould 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 $2}'" % (packageName)
P = OS. popen (command)
# For some applications, there are multiple processes, so we shocould 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
After the script is executed, if the Android program is closed or restarted, the process ID changes and logs cannot be automatically output. you can only execute the script again.