1 # Copyright (c) 2016 Google, Inc
3 # SPDX-License-Identifier: GPL-2.0+
5 # Terminal output logging.
12 # Output verbosity levels that we support
20 This class handles output of progress and other useful information
21 to the user. It provides for simple verbosity level control and can
22 output nothing but errors at verbosity zero.
24 The idea is that modules set up an Output object early in their years and pass
25 it around to other modules that need it. This keeps the output under control
29 verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
34 def __exit__(unused1, unused2, unused3):
35 """Clean up and remove any progress message."""
40 """This returns True if it is likely that a user is present.
42 Sometimes we want to prompt the user, but if no one is there then this
43 is a waste of time, and may lock a script which should otherwise fail.
46 True if it thinks the user is there, and False otherwise
48 return stdout_is_tty and verbose > 0
51 """Clear any active progress message on the terminal."""
52 if verbose > 0 and stdout_is_tty:
53 _stdout.write('\r%s\r' % (" " * len (_progress)))
56 def Progress(msg, warning=False, trailer='...'):
57 """Display progress information.
60 msg: Message to display.
61 warning: True if this is a warning."""
64 _progress = msg + trailer
66 col = _color.YELLOW if warning else _color.GREEN
67 _stdout.write('\r' + _color.Color(col, _progress))
70 _stdout.write(_progress + '\n')
72 def _Output(level, msg, color=None):
73 """Output a message to the terminal.
76 level: Verbosity level for this message. It will only be displayed if
77 this as high as the currently selected level.
78 msg; Message to display.
79 error: True if this is an error message, else False.
84 msg = _color.Color(color, msg)
85 _stdout.write(msg + '\n')
87 def DoOutput(level, msg):
88 """Output a message to the terminal.
91 level: Verbosity level for this message. It will only be displayed if
92 this as high as the currently selected level.
93 msg; Message to display.
98 """Display an error message
101 msg; Message to display.
103 _Output(0, msg, _color.RED)
106 """Display a warning message
109 msg; Message to display.
111 _Output(1, msg, _color.YELLOW)
114 """Display an important infomation message
117 msg; Message to display.
122 """Display an infomation message
125 msg; Message to display.
130 """Display a debug message
133 msg; Message to display.
138 """Display a message regardless of the current output level.
140 This is used when the output was specifically requested by the user.
142 msg; Message to display.
146 def Init(_verbose=WARNING, stdout=sys.stdout):
147 """Initialize a new output object.
150 verbose: Verbosity level (0-4).
151 stdout: File to use for stdout.
153 global verbose, _progress, _color, _stdout, stdout_is_tty
156 _progress = '' # Our last progress message
157 _color = terminal.Color()
160 # TODO(sjg): Move this into Chromite libraries when we have them
161 stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()