Python implements log script sharing for filtering a single Android program

Source: Internet
Author: User
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.

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.