Files
u-boot/tools/u_boot_pylib/tout.py
Simon Glass 89bae548f1 u_boot_pylib: tout: Add newline parameter to output functions
Add a newline parameter to all output functions (info, error, warning,
notice, detail, debug, user_output, do_output) to allow suppressing the
trailing newline. This is useful for progress output where multiple
calls should appear on the same line.

Example:
    tout.info('Processing...', newline=False)
    tout.info('done')

Also fix typos in docstrings (msg; -> msg:).

Signed-off-by: Simon Glass <simon.glass@canonical.com>
2026-01-02 18:13:48 -07:00

191 lines
5.4 KiB
Python

# SPDX-License-Identifier: GPL-2.0+
# Copyright (c) 2016 Google, Inc
#
# Terminal output logging.
#
import sys
from u_boot_pylib import terminal
# Output verbosity levels that we support
FATAL, ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(7)
"""
This class handles output of progress and other useful information
to the user. It provides for simple verbosity level control and can
output nothing but errors at verbosity zero.
The idea is that modules set up an Output object early in their years and pass
it around to other modules that need it. This keeps the output under control
of a single class.
Public properties:
verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
"""
def __enter__():
return
def __exit__(unused1, unused2, unused3):
"""Clean up and remove any progress message."""
clear_progress()
return False
def user_is_present():
"""This returns True if it is likely that a user is present.
Sometimes we want to prompt the user, but if no one is there then this
is a waste of time, and may lock a script which should otherwise fail.
Returns:
True if it thinks the user is there, and False otherwise
"""
return stdout_is_tty and verbose > ERROR
def clear_progress():
"""Clear any active progress message on the terminal."""
if verbose > ERROR and stdout_is_tty:
terminal.print_clear()
def progress(msg, warning=False, trailer='...'):
"""Display progress information.
Args:
msg: Message to display.
warning: True if this is a warning."""
clear_progress()
if verbose > ERROR:
_progress = msg + trailer
if stdout_is_tty:
col = _color.YELLOW if warning else _color.GREEN
terminal.tprint('\r' + _progress, newline=False, colour=col, col=_color)
else:
terminal.tprint(_progress)
def _output(level, msg, color=None, newline=True):
"""Output a message to the terminal.
Args:
level: Verbosity level for this message. It will only be displayed if
this as high as the currently selected level.
msg: Message to display.
color: Colour to use for the text, None for default.
newline: True to add a newline at the end.
"""
if verbose >= level:
clear_progress()
if level <= WARNING:
terminal.tprint(msg, newline=newline, colour=color, col=_color,
stderr=True)
else:
terminal.tprint(msg, newline=newline, colour=color, col=_color)
if level == FATAL:
sys.exit(1)
def do_output(level, msg, newline=True):
"""Output a message to the terminal.
Args:
level: Verbosity level for this message. It will only be displayed if
this as high as the currently selected level.
msg: Message to display.
newline: True to add a newline at the end.
"""
_output(level, msg, newline=newline)
def fatal(msg):
"""Display an error message and exit
Args:
msg: Message to display.
"""
_output(FATAL, msg, _color.RED)
def error(msg, newline=True):
"""Display an error message
Args:
msg: Message to display.
newline: True to add a newline at the end.
"""
_output(ERROR, msg, _color.RED, newline=newline)
def warning(msg, newline=True):
"""Display a warning message
Args:
msg: Message to display.
newline: True to add a newline at the end.
"""
_output(WARNING, msg, _color.YELLOW, newline=newline)
def notice(msg, newline=True):
"""Display an important infomation message
Args:
msg: Message to display.
newline: True to add a newline at the end.
"""
_output(NOTICE, msg, newline=newline)
def info(msg, newline=True):
"""Display an infomation message
Args:
msg: Message to display.
newline: True to add a newline at the end.
"""
_output(INFO, msg, newline=newline)
def detail(msg, newline=True):
"""Display a detailed message
Args:
msg: Message to display.
newline: True to add a newline at the end.
"""
_output(DETAIL, msg, newline=newline)
def debug(msg, newline=True):
"""Display a debug message
Args:
msg: Message to display.
newline: True to add a newline at the end.
"""
_output(DEBUG, msg, newline=newline)
def user_output(msg, newline=True):
"""Display a message regardless of the current output level.
This is used when the output was specifically requested by the user.
Args:
msg: Message to display.
newline: True to add a newline at the end.
"""
_output(ERROR, msg, newline=newline)
def init(_verbose=WARNING, stdout=sys.stdout, allow_colour=True):
"""Initialize a new output object.
Args:
verbose: Verbosity level (0-6).
stdout: File to use for stdout.
"""
global verbose, _progress, _color, _stdout, stdout_is_tty
verbose = _verbose
_progress = '' # Our last progress message
_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
stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
stderr_is_tty = hasattr(sys.stderr, 'isatty') and sys.stderr.isatty()
def uninit():
clear_progress()
init()