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