通过Python从Android访问LogCat
在python中是否可以读取通过LogCat发送的信息 我有一个用java编写的程序。 它发送的每个画框标记:“Fps:”消息:编号通过Python从Android访问LogCat,android,python,events,logcat,android-logcat,Android,Python,Events,Logcat,Android Logcat,在python中是否可以读取通过LogCat发送的信息 我有一个用java编写的程序。 它发送的每个画框标记:“Fps:”消息:编号 我希望此消息触发一个我可以在python脚本中捕获的事件,以便我可以绘制fps表。我会将adb logcat重定向到您的python脚本。这看起来像: $ adb logcat | python yourscript.py 现在,您可以从上的logcat中读取数据,并根据自己的喜好对其进行解析。查看。以下代码改编自 当然,您需要自己编写is\u fps\u行和u
我希望此消息触发一个我可以在python脚本中捕获的事件,以便我可以绘制fps表。我会将
adb logcat
重定向到您的python脚本。这看起来像:
$ adb logcat | python yourscript.py
现在,您可以从上的logcat中读取数据,并根据自己的喜好对其进行解析。查看。以下代码改编自
当然,您需要自己编写
is\u fps\u行
和update\u fps
函数。很酷,谢谢你D恰好是我要找的:)))不起作用,尽管xD给出了这个错误:回溯(最近一次调用):文件“C:\Users\user\Desktop\Eclipse Workspace\ThirdWorkspace\test.pydev\src\FpsMeter.py”,第116行,在process=subprocess.Popen([“logcat”],stdout=subprocess.PIPE)文件“C:\Python27\lib\subprocess.py”,第679行,在init errread,errwrite)文件“C:\Python27\lib\subprocess.py”的第896行,在_execute\u child startupinfo)WindowsError:[错误2]系统找不到指定的文件标记的行包含以下代码:process=subprocess.Popen([“logcat”],stdout=subprocess.PIPE)此代码假定logcat在您的路径上。如果不在运行时路径上,则需要提供logcat的完整路径(例如“C:\Program Files\…\logcat”),以替换“logcat”字符串。logcat在android目录中不存在。您可以通过控制台中的“adb logcat”启动android。。。事件提供完整路径C:\Program Files(x86)\Android\Android sdk\platform tools\logcat它不起作用。
import Queue
import subprocess
import threading
class AsynchronousFileReader(threading.Thread):
'''
Helper class to implement asynchronous reading of a file
in a separate thread. Pushes read lines on a queue to
be consumed in another thread.
'''
def __init__(self, fd, queue):
assert isinstance(queue, Queue.Queue)
assert callable(fd.readline)
threading.Thread.__init__(self)
self._fd = fd
self._queue = queue
def run(self):
'''The body of the tread: read lines and put them on the queue.'''
for line in iter(self._fd.readline, ''):
self._queue.put(line)
def eof(self):
'''Check whether there is no more content to expect.'''
return not self.is_alive() and self._queue.empty()
# You'll need to add any command line arguments here.
process = subprocess.Popen(["logcat"], stdout=subprocess.PIPE)
# Launch the asynchronous readers of the process' stdout.
stdout_queue = Queue.Queue()
stdout_reader = AsynchronousFileReader(process.stdout, stdout_queue)
stdout_reader.start()
# Check the queues if we received some output (until there is nothing more to get).
while not stdout_reader.eof():
while not stdout_queue.empty():
line = stdout_queue.get()
if is_fps_line(line):
update_fps(line)