PythonでLogCatを介して送信されている情報を読み取ることは可能ですか?
Javaで書かれたプログラムがあります。送信するすべての描画フレーム tag:"Fps: " message: number
このメッセージで、Python スクリプトでキャッチできるイベントを発生させて、fps メーターを描画できるようにしたいと考えています。
PythonでLogCatを介して送信されている情報を読み取ることは可能ですか?
Javaで書かれたプログラムがあります。送信するすべての描画フレーム tag:"Fps: " message: number
このメッセージで、Python スクリプトでキャッチできるイベントを発生させて、fps メーターを描画できるようにしたいと考えています。
subprocessを見てください。次のコードはStefaan Lippensから改作されました
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)
もちろん、is_fps_line
andupdate_fps
関数は自分で書く必要があります。
adb logcat
私はあなたのpythonスクリプトにリダイレクトします。これは次のようになります。
$ adb logcat | python yourscript.py
これで、 sys.stdinの logcat から読み取り、好きなように解析できるようになりました。