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