Python Implementation filter single Android program log script sharing

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

  • Related Article

    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.