1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
4 # Terminal output logging.
7 from __future__ import print_function
13 # Output verbosity levels that we support
14 ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(6)
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."""
52 if verbose > 0 and stdout_is_tty and in_progress:
53 _stdout.write('\r%s\r' % (" " * len (_progress)))
57 def Progress(msg, warning=False, trailer='...'):
58 """Display progress information.
61 msg: Message to display.
62 warning: True if this is a warning."""
66 _progress = msg + trailer
68 col = _color.YELLOW if warning else _color.GREEN
69 _stdout.write('\r' + _color.Color(col, _progress))
73 _stdout.write(_progress + '\n')
75 def _Output(level, msg, color=None):
76 """Output a message to the terminal.
79 level: Verbosity level for this message. It will only be displayed if
80 this as high as the currently selected level.
81 msg; Message to display.
82 error: True if this is an error message, else False.
87 msg = _color.Color(color, msg)
90 def DoOutput(level, msg):
91 """Output a message to the terminal.
94 level: Verbosity level for this message. It will only be displayed if
95 this as high as the currently selected level.
96 msg; Message to display.
101 """Display an error message
104 msg; Message to display.
106 _Output(ERROR, msg, _color.RED)
109 """Display a warning message
112 msg; Message to display.
114 _Output(WARNING, msg, _color.YELLOW)
117 """Display an important infomation message
120 msg; Message to display.
125 """Display an infomation message
128 msg; Message to display.
133 """Display a detailed message
136 msg; Message to display.
141 """Display a debug message
144 msg; Message to display.
149 """Display a message regardless of the current output level.
151 This is used when the output was specifically requested by the user.
153 msg; Message to display.
157 def Init(_verbose=WARNING, stdout=sys.stdout):
158 """Initialize a new output object.
161 verbose: Verbosity level (0-4).
162 stdout: File to use for stdout.
164 global verbose, _progress, _color, _stdout, stdout_is_tty
167 _progress = '' # Our last progress message
168 _color = terminal.Color()
171 # TODO(sjg): Move this into Chromite libraries when we have them
172 stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()