Skip to content

Commit

Permalink
Merge pull request #1242 from Bastian-Krause/bst/bareboxdriver-run-lo…
Browse files Browse the repository at this point in the history
…glevel

driver/bareboxdriver: run commands with saved log level
  • Loading branch information
Emantor committed Jul 21, 2023
2 parents 58ba833 + 44fd0f9 commit dacfe5f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~
Expand Down
17 changes: 12 additions & 5 deletions labgrid/driver/bareboxdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -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):
Expand All @@ -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}")
Expand Down

0 comments on commit dacfe5f

Please sign in to comment.