From 271da3fa3915db30cfa99d995bfa7bd91bdce25e Mon Sep 17 00:00:00 2001 From: matejcik Date: Wed, 12 Feb 2020 11:18:58 +0100 Subject: [PATCH] python: add detailed logging to emulator runner --- core/emu.py | 10 +++++----- python/src/trezorlib/_internal/emulator.py | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/core/emu.py b/core/emu.py index 245be2de19..ed39394a75 100755 --- a/core/emu.py +++ b/core/emu.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import gzip +import logging import os import platform import signal @@ -54,7 +55,6 @@ def watch_emulator(emulator): try: for _, type_names, _, _ in watch.event_gen(yield_nones=False): if "IN_CLOSE_WRITE" in type_names: - click.echo("Restarting...") emulator.restart() except KeyboardInterrupt: emulator.stop() @@ -198,6 +198,10 @@ def cli( if quiet: output = None + logger = logging.getLogger("trezorlib._internal.emulator") + logger.setLevel(logging.INFO) + logger.addHandler(logging.StreamHandler()) + emulator = CoreEmulator( executable, profile_dir, @@ -230,11 +234,7 @@ def cli( run_debugger(emulator) raise RuntimeError("run_debugger should not return") - click.echo("Waiting for emulator to come up... ", err=True) - start = time.monotonic() emulator.start() - end = time.monotonic() - click.echo(f"Emulator ready after {end - start:.3f} seconds", err=True) if mnemonics: if slip0014: diff --git a/python/src/trezorlib/_internal/emulator.py b/python/src/trezorlib/_internal/emulator.py index de95479b40..6873cd8526 100644 --- a/python/src/trezorlib/_internal/emulator.py +++ b/python/src/trezorlib/_internal/emulator.py @@ -14,6 +14,7 @@ # You should have received a copy of the License along with this library. # If not, see . +import logging import os import subprocess import time @@ -22,6 +23,10 @@ from pathlib import Path from trezorlib.debuglink import TrezorClientDebugLink from trezorlib.transport.udp import UdpTransport +LOG = logging.getLogger(__name__) + +EMULATOR_WAIT_TIME = 60 + def _rm_f(path): try: @@ -84,9 +89,10 @@ class Emulator: def _get_transport(self): return UdpTransport("127.0.0.1:{}".format(self.port)) - def wait_until_ready(self, timeout=60): + def wait_until_ready(self, timeout=EMULATOR_WAIT_TIME): transport = self._get_transport() transport.open() + LOG.info("Waiting for emulator to come up...") start = time.monotonic() try: while True: @@ -103,8 +109,11 @@ class Emulator: finally: transport.close() + LOG.info("Emulator ready after {:.3f} seconds".format(time.monotonic() - start)) + def wait(self, timeout=None): ret = self.process.wait(timeout=timeout) + self.process = None self.stop() return ret @@ -129,6 +138,7 @@ class Emulator: if self.process: if self.process.poll() is not None: # process has died, stop and start again + LOG.info("Starting from a stopped process.") self.stop() else: # process is running, no need to start again @@ -139,6 +149,9 @@ class Emulator: self.wait_until_ready() except TimeoutError: # Assuming that after the default 60-second timeout, the process is stuck + LOG.warning( + "Emulator did not come up after {} seconds".format(EMULATOR_WAIT_TIME) + ) self.process.kill() raise @@ -156,10 +169,15 @@ class Emulator: self.client = None if self.process: + LOG.info("Terminating emulator...") + start = time.monotonic() self.process.terminate() try: - self.process.wait(30) + self.process.wait(EMULATOR_WAIT_TIME) + end = time.monotonic() + LOG.info("Emulator shut down after {:.3f} seconds".format(end - start)) except subprocess.TimeoutExpired: + LOG.info("Emulator seems stuck. Sending kill signal.") self.process.kill() _rm_f(self.profile_dir / "trezor.pid")