From 44fd0f91ffbbd66d199c64b816185fabb95ee561 Mon Sep 17 00:00:00 2001 From: Bastian Krause Date: Thu, 20 Jul 2023 15:35:50 +0200 Subject: [PATCH] driver/bareboxdriver: run commands with saved log level [1] introduced lowering barebox' log level to 0 (emerg) right after the barebox prompt has been detected and recovering the log level right before booting. Tests may rely on log output, though. To enable capturing log messages a command may emit, set the log level to the initially remembered level before the command is executed and reset it to 0 (emerg) after the exit code was emitted. barebox' poller should not run between the echos, so this should be safe. No asynchronous prints are expected to happen during this time. [1] #1221, 6f7bf661 ("driver/bareboxdriver: silence barebox in _await_prompt() and unsilence on boot()") Signed-off-by: Bastian Krause --- CHANGES.rst | 3 ++- labgrid/driver/bareboxdriver.py | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 9a4fb26a1..e62ac01a9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -57,7 +57,8 @@ Breaking changes in 23.1 unchanged), ``-vvv`` is an alias for ``--log-cli-level=CONSOLE``, and ``-vvvv`` is an alias for ``--log-cli-level=DEBUG``. - The `BareboxDriver` now remembers the log level, sets it to ``0`` on initial - activation/reset and recovers it on ``boot()``. + activation/reset and recovers it on ``boot()``. During + ``run()``/``run_check()`` the initially detected log level is used. Known issues in 23.1 ~~~~~~~~~~~~~~~~~~~~ diff --git a/labgrid/driver/bareboxdriver.py b/labgrid/driver/bareboxdriver.py index 144080c98..910f25300 100644 --- a/labgrid/driver/bareboxdriver.py +++ b/labgrid/driver/bareboxdriver.py @@ -65,7 +65,7 @@ def on_deactivate(self): def run(self, cmd: str, *, timeout: int = 30): return self._run(cmd, timeout=timeout) - def _run(self, cmd: str, *, timeout: int = 30, codec: str = "utf-8", decodeerrors: str = "strict"): # pylint: disable=unused-argument,line-too-long + def _run(self, cmd: str, *, timeout: int = 30, adjust_log_level: bool = True, codec: str = "utf-8", decodeerrors: str = "strict"): # pylint: disable=unused-argument,line-too-long """ Runs the specified command on the shell and returns the output. @@ -80,7 +80,14 @@ def _run(self, cmd: str, *, timeout: int = 30, codec: str = "utf-8", decodeerror marker = gen_marker() # hide marker from expect hidden_marker = f'"{marker[:4]}""{marker[4:]}"' - cmp_command = f'''echo -o /cmd {shlex.quote(cmd)}; echo {hidden_marker}; sh /cmd; echo {hidden_marker} $?;''' # pylint: disable=line-too-long + # generate command with marker and log level adjustment + cmp_command = f'echo -o /cmd {shlex.quote(cmd)}; echo {hidden_marker};' + if self.saved_log_level and adjust_log_level: + cmp_command += f' global.loglevel={self.saved_log_level};' + cmp_command += f' sh /cmd; echo {hidden_marker} $?;' + if self.saved_log_level and adjust_log_level: + cmp_command += ' global.loglevel=0;' + if self._status == 1: self.console.sendline(cmp_command) _, _, match, _ = self.console.expect( @@ -191,14 +198,14 @@ def _await_prompt(self): # remember barebox' log level - we don't expect to be interrupted here # by pollers because no hardware interaction is triggered by echo, so # it should be safe to use the usual shell wrapper via _run() - stdout, _, exitcode = self._run("echo $global.loglevel") + stdout, _, exitcode = self._run("echo $global.loglevel", adjust_log_level=False) [saved_log_level] = stdout if exitcode == 0 and saved_log_level.isnumeric(): self.saved_log_level = saved_log_level # silence barebox, the driver can get confused by asynchronous messages # logged to the console otherwise - self._run("global.loglevel=0") + self._run("global.loglevel=0", adjust_log_level=False) @Driver.check_active def await_boot(self): @@ -214,7 +221,7 @@ def boot(self, name: str): Args: name (str): name of the entry to boot""" # recover saved log level - self._run(f"global.loglevel={self.saved_log_level}") + self._run(f"global.loglevel={self.saved_log_level}", adjust_log_level=False) if name: self.console.sendline(f"boot -v {name}")