Python ADB real time monitoring Logcat log

Next: adb command to intercept logcat log with one key
One day, the person in charge of system stability development came to me, hoping that I could run android system monkey,
Monitor the output of logcat in real time. If "java.lang.NullPointerException" null pointer exception is found,
Then use the adb bugreport command to export the log compression package at that time.


Preparation stage
  1. ADB logcat - V threadime > D: \ logcat.Txt can print and save the log by thread time to a file.
  2. Since the adb logcat command is a continuous output command, if it is not destroyed (kill process), it will continue to intercept.
  3. The subprocess.Popen() class supports continuous output and line reading through stdout=subprocess.PIPE.
  4. adb bugreport is just a command, which can pack the information of tombstone, getprop, proc, cache, etc.

Python batch script form
# coding=utf-8

import os
import re
import subprocess

command = "adb logcat -v threadtime"  # order of the day
keyword_reg = r".*java.lang.NullPointerException.*"  # regular expression 

# Start adb command
p_obj = subprocess.Popen(
        args=command,
        stdin=None, stdout=subprocess.PIPE,
        stderr=subprocess.PIPE, shell=False)

# Monitor and filter the keywords in the log generated by each line in real time
print("Logcat catching and filtering...")
with p_obj:
    for line in p_obj.stdout:
        if re.match(keyword_reg, line.decode("utf-8")):
            print("Found %s" % keyword_reg)
            print("running adb bugreport to pull releated logs...pls wait")
            os.system("adb bugreport")  # Export a bugreport log compressed package

os.system("pause")

The matching, searching, replacing and other operations of re module can only operate on strings.
The output of p_obj.stdout is bytes, so utf-8 decoding is required before it becomes a string.

Python procedure oriented function form
# coding=utf-8

import os
import re
import subprocess

command = "adb logcat -v threadtime"  # order of the day
keyword_reg = r".*java.lang.NullPointerException.*"  # regular expression 


def filter_logcat():
    # Start adb command
    p_obj = subprocess.Popen(
            args=command,
            stdin=None, stdout=subprocess.PIPE,
            stderr=subprocess.PIPE, shell=False)

    # Monitor and filter the keywords in the log generated by each line in real time
    print("Logcat catching and filtering...")
    with p_obj:
        for line in p_obj.stdout:
            if re.match(keyword_reg, line.decode("utf-8")):
                print("Found %s" % keyword_reg)
                print("running adb bugreport to pull releated logs...pls wait")
                os.system("adb bugreport")  # Export a bugreport log compressed package


filter_logcat()
os.system("pause")


Python object oriented form
# coding=utf-8

import os
import re
import subprocess

command = "adb logcat -v threadtime"  # order of the day
keyword_reg = r".*java.lang.NullPointerException.*"  # regular expression 


class LogcatFilter(object):
    def __init__(self):
        # Start adb command
        self.p_obj = subprocess.Popen(
                args=command, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)

    def filter_logcat(self):
        # Monitor and filter the keywords in the log generated by each line in real time
        print("Logcat catching and filtering...")
        with self.p_obj:
            for line in self.p_obj.stdout:
                if re.match(keyword_reg, line.decode("utf-8")):
                    print("Found %s" % keyword_reg)
                    print("running adb bugreport to pull releated logs...pls wait")
                    os.system("adb bugreport")  # Export a bugreport log compressed package


if __name__ == '__main__':
    l_obj = LogcatFilter()
    l_obj.filter_logcat()
    os.system("pause")


Code operation mode and effect

Make sure the Android device is connected to the computer through the USB cable, and the adb device is effectively connected,
The three implementation forms of the above code can be run directly, for example, saved as filter_logcat.py and placed on the desktop,
It is recommended to run Python filter ﹣ logcat.py. Of course, you can double-click to run it. The effect is as follows:


For more and better original articles, please visit the official website: www.zipython.com
Selfie course (Python course of automatic test, compiled by Wu Sanren)
Original link: https://www.zipython.com/#/detail?id=389e4bab6097442a8e72e063a0eafa97
You can also follow the wechat subscription number of "wusanren" and accept the article push at any time.

Tags: Python Java Android shell

Posted on Wed, 18 Mar 2020 09:58:02 -0700 by rdog157h