Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 2 | # Copyright (c) 2016 Google, Inc |
| 3 | # |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 4 | # Terminal output logging. |
| 5 | # |
| 6 | |
| 7 | import sys |
| 8 | |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 9 | from u_boot_pylib import terminal |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 10 | |
| 11 | # Output verbosity levels that we support |
Simon Glass | 655314e | 2025-05-11 16:18:20 +0200 | [diff] [blame^] | 12 | FATAL, ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(7) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 13 | |
Simon Glass | e6dafb8 | 2018-10-01 21:12:45 -0600 | [diff] [blame] | 14 | in_progress = False |
| 15 | |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 16 | """ |
| 17 | This class handles output of progress and other useful information |
| 18 | to the user. It provides for simple verbosity level control and can |
| 19 | output nothing but errors at verbosity zero. |
| 20 | |
| 21 | The idea is that modules set up an Output object early in their years and pass |
| 22 | it around to other modules that need it. This keeps the output under control |
| 23 | of a single class. |
| 24 | |
| 25 | Public properties: |
| 26 | verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug |
| 27 | """ |
| 28 | def __enter__(): |
| 29 | return |
| 30 | |
| 31 | def __exit__(unused1, unused2, unused3): |
| 32 | """Clean up and remove any progress message.""" |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 33 | clear_progress() |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 34 | return False |
| 35 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 36 | def user_is_present(): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 37 | """This returns True if it is likely that a user is present. |
| 38 | |
| 39 | Sometimes we want to prompt the user, but if no one is there then this |
| 40 | is a waste of time, and may lock a script which should otherwise fail. |
| 41 | |
| 42 | Returns: |
| 43 | True if it thinks the user is there, and False otherwise |
| 44 | """ |
Simon Glass | 655314e | 2025-05-11 16:18:20 +0200 | [diff] [blame^] | 45 | return stdout_is_tty and verbose > ERROR |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 46 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 47 | def clear_progress(): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 48 | """Clear any active progress message on the terminal.""" |
Simon Glass | e6dafb8 | 2018-10-01 21:12:45 -0600 | [diff] [blame] | 49 | global in_progress |
Simon Glass | 655314e | 2025-05-11 16:18:20 +0200 | [diff] [blame^] | 50 | if verbose > ERROR and stdout_is_tty and in_progress: |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 51 | _stdout.write('\r%s\r' % (" " * len (_progress))) |
| 52 | _stdout.flush() |
Simon Glass | e6dafb8 | 2018-10-01 21:12:45 -0600 | [diff] [blame] | 53 | in_progress = False |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 54 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 55 | def progress(msg, warning=False, trailer='...'): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 56 | """Display progress information. |
| 57 | |
| 58 | Args: |
| 59 | msg: Message to display. |
| 60 | warning: True if this is a warning.""" |
Simon Glass | e6dafb8 | 2018-10-01 21:12:45 -0600 | [diff] [blame] | 61 | global in_progress |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 62 | clear_progress() |
Simon Glass | 655314e | 2025-05-11 16:18:20 +0200 | [diff] [blame^] | 63 | if verbose > ERROR: |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 64 | _progress = msg + trailer |
| 65 | if stdout_is_tty: |
| 66 | col = _color.YELLOW if warning else _color.GREEN |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 67 | _stdout.write('\r' + _color.build(col, _progress)) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 68 | _stdout.flush() |
Simon Glass | e6dafb8 | 2018-10-01 21:12:45 -0600 | [diff] [blame] | 69 | in_progress = True |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 70 | else: |
| 71 | _stdout.write(_progress + '\n') |
| 72 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 73 | def _output(level, msg, color=None): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 74 | """Output a message to the terminal. |
| 75 | |
| 76 | Args: |
| 77 | level: Verbosity level for this message. It will only be displayed if |
| 78 | this as high as the currently selected level. |
| 79 | msg; Message to display. |
| 80 | error: True if this is an error message, else False. |
| 81 | """ |
| 82 | if verbose >= level: |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 83 | clear_progress() |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 84 | if color: |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 85 | msg = _color.build(color, msg) |
Simon Glass | 6e02f7c | 2020-07-09 18:39:39 -0600 | [diff] [blame] | 86 | if level < NOTICE: |
| 87 | print(msg, file=sys.stderr) |
| 88 | else: |
| 89 | print(msg) |
Simon Glass | 655314e | 2025-05-11 16:18:20 +0200 | [diff] [blame^] | 90 | if level == FATAL: |
| 91 | sys.exit(1) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 92 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 93 | def do_output(level, msg): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 94 | """Output a message to the terminal. |
| 95 | |
| 96 | Args: |
| 97 | level: Verbosity level for this message. It will only be displayed if |
| 98 | this as high as the currently selected level. |
| 99 | msg; Message to display. |
| 100 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 101 | _output(level, msg) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 102 | |
Simon Glass | 655314e | 2025-05-11 16:18:20 +0200 | [diff] [blame^] | 103 | def fatal(msg): |
| 104 | """Display an error message and exit |
| 105 | |
| 106 | Args: |
| 107 | msg; Message to display. |
| 108 | """ |
| 109 | _output(FATAL, msg, _color.RED) |
| 110 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 111 | def error(msg): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 112 | """Display an error message |
| 113 | |
| 114 | Args: |
| 115 | msg; Message to display. |
| 116 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 117 | _output(ERROR, msg, _color.RED) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 118 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 119 | def warning(msg): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 120 | """Display a warning message |
| 121 | |
| 122 | Args: |
| 123 | msg; Message to display. |
| 124 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 125 | _output(WARNING, msg, _color.YELLOW) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 126 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 127 | def notice(msg): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 128 | """Display an important infomation message |
| 129 | |
| 130 | Args: |
| 131 | msg; Message to display. |
| 132 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 133 | _output(NOTICE, msg) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 134 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 135 | def info(msg): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 136 | """Display an infomation message |
| 137 | |
| 138 | Args: |
| 139 | msg; Message to display. |
| 140 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 141 | _output(INFO, msg) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 142 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 143 | def detail(msg): |
Simon Glass | 233a26a9 | 2019-07-08 14:25:49 -0600 | [diff] [blame] | 144 | """Display a detailed message |
| 145 | |
| 146 | Args: |
| 147 | msg; Message to display. |
| 148 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 149 | _output(DETAIL, msg) |
Simon Glass | 233a26a9 | 2019-07-08 14:25:49 -0600 | [diff] [blame] | 150 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 151 | def debug(msg): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 152 | """Display a debug message |
| 153 | |
| 154 | Args: |
| 155 | msg; Message to display. |
| 156 | """ |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 157 | _output(DEBUG, msg) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 158 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 159 | def user_output(msg): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 160 | """Display a message regardless of the current output level. |
| 161 | |
| 162 | This is used when the output was specifically requested by the user. |
| 163 | Args: |
| 164 | msg; Message to display. |
| 165 | """ |
Simon Glass | 655314e | 2025-05-11 16:18:20 +0200 | [diff] [blame^] | 166 | _output(ERROR, msg) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 167 | |
Simon Glass | 9d7f598 | 2025-04-29 07:22:05 -0600 | [diff] [blame] | 168 | def init(_verbose=WARNING, stdout=sys.stdout, allow_colour=True): |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 169 | """Initialize a new output object. |
| 170 | |
| 171 | Args: |
Simon Glass | 655314e | 2025-05-11 16:18:20 +0200 | [diff] [blame^] | 172 | verbose: Verbosity level (0-6). |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 173 | stdout: File to use for stdout. |
| 174 | """ |
| 175 | global verbose, _progress, _color, _stdout, stdout_is_tty |
| 176 | |
| 177 | verbose = _verbose |
| 178 | _progress = '' # Our last progress message |
Simon Glass | 9d7f598 | 2025-04-29 07:22:05 -0600 | [diff] [blame] | 179 | _color = terminal.Color(terminal.COLOR_IF_TERMINAL if allow_colour |
| 180 | else terminal.COLOR_NEVER) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 181 | _stdout = stdout |
| 182 | |
| 183 | # TODO(sjg): Move this into Chromite libraries when we have them |
| 184 | stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 185 | stderr_is_tty = hasattr(sys.stderr, 'isatty') and sys.stderr.isatty() |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 186 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 187 | def uninit(): |
| 188 | clear_progress() |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 189 | |
Simon Glass | 011f1b3 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 190 | init() |