1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
4 # Terminal output logging.
11 # Output verbosity levels that we support
21 This class handles output of progress and other useful information
22 to the user. It provides for simple verbosity level control and can
23 output nothing but errors at verbosity zero.
25 The idea is that modules set up an Output object early in their years and pass
26 it around to other modules that need it. This keeps the output under control
30 verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
35 def __exit__(unused1, unused2, unused3):
36 """Clean up and remove any progress message."""
41 """This returns True if it is likely that a user is present.
43 Sometimes we want to prompt the user, but if no one is there then this
44 is a waste of time, and may lock a script which should otherwise fail.
47 True if it thinks the user is there, and False otherwise
49 return stdout_is_tty and verbose > 0
52 """Clear any active progress message on the terminal."""
54 if verbose > 0 and stdout_is_tty and in_progress:
55 _stdout.write('\r%s\r' % (" " * len (_progress)))
59 def Progress(msg, warning=False, trailer='...'):
60 """Display progress information.
63 msg: Message to display.
64 warning: True if this is a warning."""
68 _progress = msg + trailer
70 col = _color.YELLOW if warning else _color.GREEN
71 _stdout.write('\r' + _color.Color(col, _progress))
75 _stdout.write(_progress + '\n')
77 def _Output(level, msg, color=None):
78 """Output a message to the terminal.
81 level: Verbosity level for this message. It will only be displayed if
82 this as high as the currently selected level.
83 msg; Message to display.
84 error: True if this is an error message, else False.
89 msg = _color.Color(color, msg)
90 _stdout.write(msg + '\n')
92 def DoOutput(level, msg):
93 """Output a message to the terminal.
96 level: Verbosity level for this message. It will only be displayed if
97 this as high as the currently selected level.
98 msg; Message to display.
103 """Display an error message
106 msg; Message to display.
108 _Output(0, msg, _color.RED)
111 """Display a warning message
114 msg; Message to display.
116 _Output(1, msg, _color.YELLOW)
119 """Display an important infomation message
122 msg; Message to display.
127 """Display an infomation message
130 msg; Message to display.
135 """Display a debug message
138 msg; Message to display.
143 """Display a message regardless of the current output level.
145 This is used when the output was specifically requested by the user.
147 msg; Message to display.
151 def Init(_verbose=WARNING, stdout=sys.stdout):
152 """Initialize a new output object.
155 verbose: Verbosity level (0-4).
156 stdout: File to use for stdout.
158 global verbose, _progress, _color, _stdout, stdout_is_tty
161 _progress = '' # Our last progress message
162 _color = terminal.Color()
165 # TODO(sjg): Move this into Chromite libraries when we have them
166 stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()