adb command of self timer tutorial 40 export Log with one click

Android devices, such as on-board system in the process of real vehicle road test,
It is troublesome to operate the computer on the car. If there is a Bug in the car system,
How to easily export the related log (logcat, dbus, Gaud map log, gps positioning log, etc.) at one time?
In fact, we only need a python script to do it!


Preparation stage
  1. The adb pull command can pull the files in Android system to the computer
  2. For the exported Log, you need to create a new folder in the current path for storage, preferably named after the time stamp
  3. os.system() can call the adb command

Default Log storage path of Android car system
Android Log type Default path
logcat sequence log /data/logs/logcat.txt
/data/logs/logcat.txt.1
...
/data/logs/logcat.txt.20
dbus sequence log /data/logs/dbus.txt
/data/logs/dbus.txt.1
...
/data/logs/dbus.txt.20
kernel sequence log /data/logs/kernel.txt
/data/logs/kernel.txt.1
...
/data/logs/kernel.txt.20
tombstone log /data/tombstones/*
anr card dies log /data/anr/*
dropbox log /data/system/dropbox/*
gps log /sdcard/map/gps/*
Gaud map log /sdcard/amapauto8/logs/*


Auto generated sequence Log

What is the automatically generated sequence log? In general, during system startup,
By default, a certain type of log is enabled, and the new log will automatically flush out the old log.


For example, the logcat sequence log,
Generally, all of them are written to logcat.txt, and when they are full of 5M,
Rename logcat.txt to logcat.txt.1, and so on...
So logcat.txt is always the latest log,
Then logcat.txt.1,
And then logcat.txt.2,
.....
Finally, go to the sequence logcat.txt.20


Of course, the above depends on the setting of the logcat self startup command in init.rc,
For example, the init.rc configuration file is set as follows:

/system/bin/logcat -b system -b events -b main
-b radio -n 20 -r5000 -v threadtime -f /data/logs/logcat.txt

The above startup command means: intercept all logcat logs including system, events, main and radio buffers,
And set the automatic sequence as 20 sequence fragments, - r5000 stands for 5M,
-V threadime is the print thread level timestamp,
-f is the output path, and logcat.txt is the initial and default handle file.


Other dbus.txt sequence logs and kernel sequence logs are also consistent.
As shown in the following screenshot:


Python batch script mode
# coding=utf-8

import os
from datetime import datetime

# First, create a new folder with time stamp to store the exported Log
now_time = datetime.now().strftime("%Y%m%d_%H%M%S")
log_folder = "Logs_" + now_time
os.mkdir(log_folder)  # Because no specific path is specified, it is created under the current path by default.

# Export all sequence log s first
os.system("adb pull /data/logs/logcat.txt %s" % log_folder)
os.system("adb pull /data/logs/dbus.txt %s" % log_folder)
os.system("adb pull /data/logs/kernel.txt %s" % log_folder)
for i in range(1, 21):
    os.system("adb pull /data/logs/logcat.txt.%s %s" % (i, log_folder))
    os.system("adb pull /data/logs/dbus.txt.%s %s" % (i, log_folder))
    os.system("adb pull /data/logs/kernel.txt.%s %s" % (i, log_folder))

# Export other log s
os.system("adb pull /data/tombstones %s" % log_folder)
os.system("adb pull /data/anr %s" % log_folder)
os.system("adb pull /data/system/dropbox %s" % log_folder)
os.system("adb pull /sdcard/map/gps %s" % log_folder)
os.system("adb pull /sdcard/amapauto8/logs %s" % log_folder)

# Print the absolute path of the storage address.
print("All real vehicles log All have been exported, storage address:%s" % os.path.abspath(log_folder))

os.system("pause")


Python procedure oriented function form

We can subdivide it into five functions,
One of the functions is to create a folder with a time stamp,
One of the functions exports the logcat.txt sequence log,
One of the functions exports the dbus.txt sequence log,
One of the functions exports the kernel.txt sequence log,
The last function is to export all functions containing other log s.

# coding=utf-8

import os
from datetime import datetime

# First, create a new folder with time stamp to store the exported Log
def creat_floder():
    now_time = datetime.now().strftime("%Y%m%d_%H%M%S")
    log_folder = "Logs_" + now_time
    os.mkdir(log_folder)  # Because no specific path is specified, it is created under the current path by default.
    return os.path.abspath(log_folder)  # Return to absolute path

# Export logcat sequence log
def pull_logcat_logs(save_path):
    os.system("adb pull /data/logs/logcat.txt %s" % save_path)
    for i in range(1, 21):
        os.system("adb pull /data/logs/logcat.txt.%s %s" % (i, save_path))

# Export dbus sequence log
def pull_dbus_logs(save_path):
    os.system("adb pull /data/logs/dbus.txt %s" % save_path)
    for i in range(1, 21):
        os.system("adb pull /data/logs/dbus.txt.%s %s" % (i, save_path))

# Export kernel sequence log
def pull_kernel_logs(save_path):
    os.system("adb pull /data/logs/kernel.txt %s" % save_path)
    for i in range(1, 21):
        os.system("adb pull /data/logs/kernel.txt.%s %s" % (i, save_path))

def pull_all_logs(save_path):
    pull_logcat_logs(save_path)  # function call
    pull_dbus_logs(save_path)    # function call
    pull_kernel_logs(save_path)  # function call
    os.system("adb pull /data/tombstones %s" % save_path)
    os.system("adb pull /data/anr %s" % save_path)
    os.system("adb pull /data/system/dropbox %s" % save_path)
    os.system("adb pull /sdcard/map/gps %s" % save_path)
    os.system("adb pull /sdcard/amapauto8/logs %s" % save_path)
    # Print the absolute path of the storage address.
    print("All real vehicles log All have been exported, storage address:%s" % save_path)

log_path = creat_floder()
pull_all_logs(log_path)
os.system("pause")

Above, the return value of the function is realized, and the parameter is passed, and the function is called in-line.

The form of Python object-oriented classes
  1. With the idea of "all things can be classified", we first abstract a class,
    In general, "noun" is recommended for class name, so we call it "LogPuller",
    Represents the Log exporter, and is generally humped (capitalized) to specify the class name.
  2. Form a good habit of class init ialization,
    During initialization, you can pass in the path of the export.
  3. For this LogPuller class, it is recommended to refine the export functions of various types of logs.
  4. Class is an abstract thing, which must be instantiated into concrete objects,
    It can be called, so we instantiate and name it as lju obj, indicating that it is an object.
  5. After the instance becomes a concrete object, the object can call the pull all logs function
# coding=utf-8

import os
from datetime import datetime


# First, create a new folder with time stamp to store the exported Log
# As a common function, this function does not belong to a class, but exists as a function
def creat_floder():
    now_time = datetime.now().strftime("%Y%m%d_%H%M%S")
    log_folder = "Logs_" + now_time
    os.mkdir(log_folder)  # Because no specific path is specified, it is created under the current path by default.
    return os.path.abspath(log_folder)


# Export all real vehicle logs
class LogPuller():
    def __init__(self, save_path):
        self.save_path = save_path

    # Export logcat sequence log
    def pull_logcat_logs(self):
        os.system("adb pull /data/logs/logcat.txt %s" % self.save_path)
        for i in range(1, 21):
            os.system("adb pull /data/logs/logcat.txt.%s %s" % (i, self.save_path))

    # Export dbus sequence log
    def pull_dbus_logs(self):
        os.system("adb pull /data/logs/dbus.txt %s" % self.save_path)
        for i in range(1, 21):
            os.system("adb pull /data/logs/dbus.txt.%s %s" % (i, self.save_path))

    # Export kernel sequence log
    def pull_kernel_logs(self):
        os.system("adb pull /data/logs/kernel.txt %s" % self.save_path)
        for i in range(1, 21):
            os.system("adb pull /data/logs/kernel.txt.%s %s" % (i, self.save_path))

    def pull_tombstones_logs(self):
        os.system("adb pull /data/tombstones %s" % self.save_path)

    def pull_anr_logs(self):
        os.system("adb pull /data/anr %s" % self.save_path)

    def pull_dropbox_logs(self):
        os.system("adb pull /data/system/dropbox %s" % self.save_path)

    def pull_gps_logs(self):
        os.system("adb pull /sdcard/map/gps %s" % self.save_path)

    def pull_amapauto_logs(self):
        os.system("adb pull /sdcard/amapauto8/logs %s" % self.save_path)

    def pull_all_logs(self):
        self.pull_logcat_logs()
        self.pull_dbus_logs()
        self.pull_kernel_logs()
        self.pull_tombstones_logs()
        self.pull_anr_logs()
        self.pull_dropbox_logs()
        self.pull_gps_logs()
        self.pull_amapauto_logs()
        # Print the absolute path of the storage address.
        print("All real vehicles log All have been exported, storage address:%s" % self.save_path)

# More scientific operation mode in documents
if __name__ == '__main__':
    log_path = creat_floder()
    l_obj = LogPuller(log_path)
    l_obj.pull_all_logs()
    os.system("pause")


Code operation mode

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 pull logs.py and placed on the desktop,
Every time you need to take a screenshot, double-click to run pull logs.py,
A folder similar to logs will be generated under the current folder.

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=884c9f1c4f2e4a159457d7993d723aec
You can also follow the wechat subscription number of "wusanren" and accept the article push at any time.

Tags: Python DBus Android

Posted on Wed, 11 Mar 2020 00:48:07 -0400 by Zepo.