Skip to content

Commit

Permalink
twister: Remove ANSI sequences before writing to handler.log
Browse files Browse the repository at this point in the history
ANSI color is great for viewing output in a terminal, but currently it
also gets written out to the `handler.log` file. Text editors usually
don't render these, resulting in a hard-to-read file with a lot of
gibberish interspered. This commit strips ANSI sequences from lines
before writing them to the handler log file. This change does not
affect what is printed in Twister's console output, so one would still
see the colors there.

Signed-off-by: Tristan Honscheid <[email protected]>
  • Loading branch information
tristan-google authored and nashif committed Mar 28, 2024
1 parent dab9322 commit 36ad49b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions scripts/pylib/twister/twisterlib/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,9 @@ def parse_arguments(parser, args, options = None):

return options

def strip_ansi_sequences(s: str) -> str:
"""Remove ANSI escape sequences from a string."""
return re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', "", s)

class TwisterEnv:

Expand Down Expand Up @@ -967,8 +970,7 @@ def run_cmake_script(args=[]):
# for instance if twister is executed from inside a makefile. In such a
# scenario it is then necessary to remove them, as otherwise the JSON decoding
# will fail.
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
out = ansi_escape.sub('', out.decode())
out = strip_ansi_sequences(out.decode())

if p.returncode == 0:
msg = "Finished running %s" % (args[0])
Expand Down
8 changes: 4 additions & 4 deletions scripts/pylib/twister/twisterlib/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import time

from queue import Queue, Empty
from twisterlib.environment import ZEPHYR_BASE
from twisterlib.environment import ZEPHYR_BASE, strip_ansi_sequences
from twisterlib.error import TwisterException
sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/build_helpers"))
from domains import Domains
Expand Down Expand Up @@ -216,7 +216,7 @@ def _output_handler(self, proc, harness):
else:
stripped_line = line_decoded.rstrip()
logger.debug("OUTPUT: %s", stripped_line)
log_out_fp.write(line_decoded)
log_out_fp.write(strip_ansi_sequences(line_decoded))
log_out_fp.flush()
harness.handle(stripped_line)
if harness.state:
Expand Down Expand Up @@ -426,7 +426,7 @@ def monitor_serial(self, ser, halt_event, harness):
sl = serial_line.decode('utf-8', 'ignore').lstrip()
logger.debug("DEVICE: {0}".format(sl.rstrip()))

log_out_fp.write(sl.encode('utf-8'))
log_out_fp.write(strip_ansi_sequences(sl).encode('utf-8'))
log_out_fp.flush()
harness.handle(sl.rstrip())

Expand Down Expand Up @@ -921,7 +921,7 @@ def _thread(handler, timeout, outdir, logfile, fifo_fn, pid_fn, results,
continue

# line contains a full line of data output from QEMU
log_out_fp.write(line)
log_out_fp.write(strip_ansi_sequences(line))
log_out_fp.flush()
line = line.rstrip()
logger.debug(f"QEMU ({pid}): {line}")
Expand Down

0 comments on commit 36ad49b

Please sign in to comment.