[Python] [technology blog] written in Python get the phone number of log operations App

Some written in Python get operation log of phone App

  1. How to get the package name of the phone currently open App
  2. How to obtain the PID of the current process App
  3. How to view the current log App
  4. How to save the log to file
  5. How to turn off process
  6. How the command line window does not show

1. How to get the package name of the phone currently open App

Can be entered directly on the command lineadb shell dumpsys window | findstr mCurrentFocus

Mobile phone QQ, for example, to read the information for mCurrentFocus

mCurrentFocus=Window{cb7270e u0 com.tencent.mobileqq/com.tencent.mobileqq.activity.AddAccountActivity}

Then dividing by the string, extract the package name "com.tencent.mobileqq", if necessary, can also obtain the current activity name.

If in Python, you can use the following method

import os
data = os.popen("adb shell dumpsys window | findstr mCurrentFocus")
mCurrentFocus = data.read()
list1 = mCurrentFocus.split(' ')
list2 = list1[4].split('/')
packageName = list2[0]
print(packageName)

2. How to get the current process PID App

Entered directly on the command line command adb shell "ps | grep com.tencent.mobileqq"can get all the application processes the information, and some App only one process, there will be some App multiple processes. Note here that the grep command is used in Linux, if you want to use in the Windows environment, the need to add double quotes around, otherwise it will error.

u0_a98         991   744 2202676 125564 0                   0 S com.tencent.mobileqq:tool
u0_a98       31810   744 1938984  68956 0                   0 S com.tencent.mobileqq:MSF
u0_a98       32714   744 2218736 226968 0                   0 S com.tencent.mobileqq

The second column is where the process of PID, then we can obtain the PID by Python string splitting and other operations.

3. How to view the current log App

Some online posts have used findthe method or grepmethods, which will attach some other log when looking for, by looking adb logcat help files and found that a

--pid=<pid> Only prints logs from the given pid

In this way we can get PID developed by the following command log

adb shell logcat --pid=32714

Or easier

adb shell logcat --pid=$(pidof -s com.tencent.mobileqq)

We can also be constraints, such as log as long as the above Warning

adb shell logcat *:W --pid=$(pidof -s com.tencent.mobileqq)

The resulting log is as follows

--------- beginning of main
06-04 20:39:56.804 32714   710 E DingdongPluginBizHandler: 0x51d_1 respond msf error: retCode[1002].
06-04 20:40:52.953 32714 32714 W InputMethodManager: startInputReason = 1
06-04 20:40:52.971 32714 32747 W libEGL  : EGLNativeWindowType 0xe0e6d808 disconnect failed

4. How to save the log to a file

There are two ways

A is added directly in the command > filepath, the command is

adb shell logcat *:W > E:/log.txt

2. Change the properties of subprocess.Popen

logfile = open("E:/log.txt", 'w')
command = "adb shell logcat *:W"
subprocess.Popen(command, stdout = logfile, shell=True)

5. How to shut down the process

After running the code, it will open a total of two processes, one is cmd.exe, the second is adb.exe, to be closed

log = subprocess.Popen(command)

##关闭
try:
    log.terminate() #关闭 cmd.exe
    os.popen("adb kill-server") #关闭 adb.exe
except:
    pass

6. How does the command line window display

By changing the subprocess in startupinfo

st = subprocess.STARTUPINFO
st.dwFlags = subprocess.STARTF_USESHOWWINDOW
st.wShowWindow = subprocess.SW_HIDE
cmd = subprocess.Popen(command, startupinfo=st)

Guess you like

Origin www.cnblogs.com/bhlt1998/p/10980826.html