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 | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 9 | from patman import terminal |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 10 | |
| 11 | # Output verbosity levels that we support |
Simon Glass | 6162f1a | 2019-07-20 12:23:35 -0600 | [diff] [blame] | 12 | ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(6) |
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.""" |
| 33 | ClearProgress() |
| 34 | return False |
| 35 | |
| 36 | def UserIsPresent(): |
| 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 | """ |
| 45 | return stdout_is_tty and verbose > 0 |
| 46 | |
| 47 | def ClearProgress(): |
| 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 |
| 50 | if verbose > 0 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 | |
| 55 | def Progress(msg, warning=False, trailer='...'): |
| 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 | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 62 | ClearProgress() |
| 63 | if verbose > 0: |
| 64 | _progress = msg + trailer |
| 65 | if stdout_is_tty: |
| 66 | col = _color.YELLOW if warning else _color.GREEN |
| 67 | _stdout.write('\r' + _color.Color(col, _progress)) |
| 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 | |
| 73 | def _Output(level, msg, color=None): |
| 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: |
| 83 | ClearProgress() |
| 84 | if color: |
| 85 | msg = _color.Color(color, msg) |
Simon Glass | dc4aeef | 2019-07-20 12:23:26 -0600 | [diff] [blame] | 86 | print(msg) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 87 | |
| 88 | def DoOutput(level, msg): |
| 89 | """Output a message to the terminal. |
| 90 | |
| 91 | Args: |
| 92 | level: Verbosity level for this message. It will only be displayed if |
| 93 | this as high as the currently selected level. |
| 94 | msg; Message to display. |
| 95 | """ |
| 96 | _Output(level, msg) |
| 97 | |
| 98 | def Error(msg): |
| 99 | """Display an error message |
| 100 | |
| 101 | Args: |
| 102 | msg; Message to display. |
| 103 | """ |
Simon Glass | 6162f1a | 2019-07-20 12:23:35 -0600 | [diff] [blame] | 104 | _Output(ERROR, msg, _color.RED) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 105 | |
| 106 | def Warning(msg): |
| 107 | """Display a warning message |
| 108 | |
| 109 | Args: |
| 110 | msg; Message to display. |
| 111 | """ |
Simon Glass | 6162f1a | 2019-07-20 12:23:35 -0600 | [diff] [blame] | 112 | _Output(WARNING, msg, _color.YELLOW) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 113 | |
| 114 | def Notice(msg): |
| 115 | """Display an important infomation message |
| 116 | |
| 117 | Args: |
| 118 | msg; Message to display. |
| 119 | """ |
Simon Glass | 6162f1a | 2019-07-20 12:23:35 -0600 | [diff] [blame] | 120 | _Output(NOTICE, msg) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 121 | |
| 122 | def Info(msg): |
| 123 | """Display an infomation message |
| 124 | |
| 125 | Args: |
| 126 | msg; Message to display. |
| 127 | """ |
Simon Glass | 6162f1a | 2019-07-20 12:23:35 -0600 | [diff] [blame] | 128 | _Output(INFO, msg) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 129 | |
Simon Glass | 233a26a9 | 2019-07-08 14:25:49 -0600 | [diff] [blame] | 130 | def Detail(msg): |
| 131 | """Display a detailed message |
| 132 | |
| 133 | Args: |
| 134 | msg; Message to display. |
| 135 | """ |
Simon Glass | 6162f1a | 2019-07-20 12:23:35 -0600 | [diff] [blame] | 136 | _Output(DETAIL, msg) |
Simon Glass | 233a26a9 | 2019-07-08 14:25:49 -0600 | [diff] [blame] | 137 | |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 138 | def Debug(msg): |
| 139 | """Display a debug message |
| 140 | |
| 141 | Args: |
| 142 | msg; Message to display. |
| 143 | """ |
Simon Glass | 6162f1a | 2019-07-20 12:23:35 -0600 | [diff] [blame] | 144 | _Output(DEBUG, msg) |
Simon Glass | a2b840a | 2016-07-25 18:59:09 -0600 | [diff] [blame] | 145 | |
| 146 | def UserOutput(msg): |
| 147 | """Display a message regardless of the current output level. |
| 148 | |
| 149 | This is used when the output was specifically requested by the user. |
| 150 | Args: |
| 151 | msg; Message to display. |
| 152 | """ |
| 153 | _Output(0, msg) |
| 154 | |
| 155 | def Init(_verbose=WARNING, stdout=sys.stdout): |
| 156 | """Initialize a new output object. |
| 157 | |
| 158 | Args: |
| 159 | verbose: Verbosity level (0-4). |
| 160 | stdout: File to use for stdout. |
| 161 | """ |
| 162 | global verbose, _progress, _color, _stdout, stdout_is_tty |
| 163 | |
| 164 | verbose = _verbose |
| 165 | _progress = '' # Our last progress message |
| 166 | _color = terminal.Color() |
| 167 | _stdout = stdout |
| 168 | |
| 169 | # TODO(sjg): Move this into Chromite libraries when we have them |
| 170 | stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() |
| 171 | |
| 172 | def Uninit(): |
| 173 | ClearProgress() |
| 174 | |
| 175 | Init() |