1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
4 # Terminal output logging.
11 # Output verbosity levels that we support
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.
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
28 verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
33 def __exit__(unused1, unused2, unused3):
34 """Clean up and remove any progress message."""
39 """This returns True if it is likely that a user is present.
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.
45 True if it thinks the user is there, and False otherwise
47 return stdout_is_tty and verbose > 0
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)))
55 def Progress(msg, warning=False, trailer='...'):
56 """Display progress information.
59 msg: Message to display.
60 warning: True if this is a warning."""
63 _progress = msg + trailer
65 col = _color.YELLOW if warning else _color.GREEN
66 _stdout.write('\r' + _color.Color(col, _progress))
69 _stdout.write(_progress + '\n')
71 def _Output(level, msg, color=None):
72 """Output a message to the terminal.
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.
83 msg = _color.Color(color, msg)
84 _stdout.write(msg + '\n')
86 def DoOutput(level, msg):
87 """Output a message to the terminal.
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.
97 """Display an error message
100 msg; Message to display.
102 _Output(0, msg, _color.RED)
105 """Display a warning message
108 msg; Message to display.
110 _Output(1, msg, _color.YELLOW)
113 """Display an important infomation message
116 msg; Message to display.
121 """Display an infomation message
124 msg; Message to display.
129 """Display a debug message
132 msg; Message to display.
137 """Display a message regardless of the current output level.
139 This is used when the output was specifically requested by the user.
141 msg; Message to display.
145 def Init(_verbose=WARNING, stdout=sys.stdout):
146 """Initialize a new output object.
149 verbose: Verbosity level (0-4).
150 stdout: File to use for stdout.
152 global verbose, _progress, _color, _stdout, stdout_is_tty
155 _progress = '' # Our last progress message
156 _color = terminal.Color()
159 # TODO(sjg): Move this into Chromite libraries when we have them
160 stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()