From 9dee86cf533271deddd7f8ab077d98e4a1d3cb78 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Tue, 29 Apr 2025 07:22:05 -0600 Subject: u_boot_pylib: Allow disabling colour output When running tests there is no situation in which we want ANSI output as it makes it much harder to see what is going on in logs, tests, etc. Provide a way to disable this. Signed-off-by: Simon Glass --- tools/u_boot_pylib/tout.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'tools/u_boot_pylib/tout.py') diff --git a/tools/u_boot_pylib/tout.py b/tools/u_boot_pylib/tout.py index 6bd2806f88f..37849d1450e 100644 --- a/tools/u_boot_pylib/tout.py +++ b/tools/u_boot_pylib/tout.py @@ -155,7 +155,7 @@ def user_output(msg): """ _output(0, msg) -def init(_verbose=WARNING, stdout=sys.stdout): +def init(_verbose=WARNING, stdout=sys.stdout, allow_colour=True): """Initialize a new output object. Args: @@ -166,7 +166,8 @@ def init(_verbose=WARNING, stdout=sys.stdout): verbose = _verbose _progress = '' # Our last progress message - _color = terminal.Color() + _color = terminal.Color(terminal.COLOR_IF_TERMINAL if allow_colour + else terminal.COLOR_NEVER) _stdout = stdout # TODO(sjg): Move this into Chromite libraries when we have them -- cgit v1.2.3 From db5d98de55066db3b1ee3514bbf0fd304ec7775e Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 11 May 2025 16:18:20 +0200 Subject: u_boot_pylib: Support a fatal level in tout It is convenient to be able to print a message and exit. Add a new 'fatal' level to support this. Update some assumptions about the level, so that the tools continue to work as now. Signed-off-by: Simon Glass --- tools/u_boot_pylib/tout.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'tools/u_boot_pylib/tout.py') diff --git a/tools/u_boot_pylib/tout.py b/tools/u_boot_pylib/tout.py index 37849d1450e..ca72108d6bc 100644 --- a/tools/u_boot_pylib/tout.py +++ b/tools/u_boot_pylib/tout.py @@ -9,7 +9,7 @@ import sys from u_boot_pylib import terminal # Output verbosity levels that we support -ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(6) +FATAL, ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(7) in_progress = False @@ -42,12 +42,12 @@ def user_is_present(): Returns: True if it thinks the user is there, and False otherwise """ - return stdout_is_tty and verbose > 0 + return stdout_is_tty and verbose > ERROR def clear_progress(): """Clear any active progress message on the terminal.""" global in_progress - if verbose > 0 and stdout_is_tty and in_progress: + if verbose > ERROR and stdout_is_tty and in_progress: _stdout.write('\r%s\r' % (" " * len (_progress))) _stdout.flush() in_progress = False @@ -60,7 +60,7 @@ def progress(msg, warning=False, trailer='...'): warning: True if this is a warning.""" global in_progress clear_progress() - if verbose > 0: + if verbose > ERROR: _progress = msg + trailer if stdout_is_tty: col = _color.YELLOW if warning else _color.GREEN @@ -87,6 +87,8 @@ def _output(level, msg, color=None): print(msg, file=sys.stderr) else: print(msg) + if level == FATAL: + sys.exit(1) def do_output(level, msg): """Output a message to the terminal. @@ -98,6 +100,14 @@ def do_output(level, msg): """ _output(level, msg) +def fatal(msg): + """Display an error message and exit + + Args: + msg; Message to display. + """ + _output(FATAL, msg, _color.RED) + def error(msg): """Display an error message @@ -153,13 +163,13 @@ def user_output(msg): Args: msg; Message to display. """ - _output(0, msg) + _output(ERROR, msg) def init(_verbose=WARNING, stdout=sys.stdout, allow_colour=True): """Initialize a new output object. Args: - verbose: Verbosity level (0-4). + verbose: Verbosity level (0-6). stdout: File to use for stdout. """ global verbose, _progress, _color, _stdout, stdout_is_tty -- cgit v1.2.3