]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - tools/u_boot_pylib/tout.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
4 # Terminal output logging.
9 from u_boot_pylib
import terminal
11 # Output verbosity levels that we support
12 ERROR
, WARNING
, NOTICE
, INFO
, DETAIL
, DEBUG
= range(6)
17 This class handles output of progress and other useful information
18 to the user. It provides for simple verbosity level control and can
19 output nothing but errors at verbosity zero.
21 The idea is that modules set up an Output object early in their years and pass
22 it around to other modules that need it. This keeps the output under control
26 verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
31 def __exit__(unused1
, unused2
, unused3
):
32 """Clean up and remove any progress message."""
36 def user_is_present():
37 """This returns True if it is likely that a user is present.
39 Sometimes we want to prompt the user, but if no one is there then this
40 is a waste of time, and may lock a script which should otherwise fail.
43 True if it thinks the user is there, and False otherwise
45 return stdout_is_tty
and verbose
> 0
48 """Clear any active progress message on the terminal."""
50 if verbose
> 0 and stdout_is_tty
and in_progress
:
51 _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."""
64 _progress
= msg
+ trailer
66 col
= _color
.YELLOW
if warning
else _color
.GREEN
67 _stdout
.write('\r' + _color
.build(col
, _progress
))
71 _stdout
.write(_progress
+ '\n')
73 def _output(level
, msg
, color
=None):
74 """Output a message to the terminal.
77 level: Verbosity level for this message. It will only be displayed if
78 this as high as the currently selected level.
79 msg; Message to display.
80 error: True if this is an error message, else False.
85 msg
= _color
.build(color
, msg
)
87 print(msg
, file=sys
.stderr
)
91 def do_output(level
, msg
):
92 """Output a message to the terminal.
95 level: Verbosity level for this message. It will only be displayed if
96 this as high as the currently selected level.
97 msg; Message to display.
102 """Display an error message
105 msg; Message to display.
107 _output(ERROR
, msg
, _color
.RED
)
110 """Display a warning message
113 msg; Message to display.
115 _output(WARNING
, msg
, _color
.YELLOW
)
118 """Display an important infomation message
121 msg; Message to display.
126 """Display an infomation message
129 msg; Message to display.
134 """Display a detailed message
137 msg; Message to display.
142 """Display a debug message
145 msg; Message to display.
149 def user_output(msg
):
150 """Display a message regardless of the current output level.
152 This is used when the output was specifically requested by the user.
154 msg; Message to display.
158 def init(_verbose
=WARNING
, stdout
=sys
.stdout
):
159 """Initialize a new output object.
162 verbose: Verbosity level (0-4).
163 stdout: File to use for stdout.
165 global verbose
, _progress
, _color
, _stdout
, stdout_is_tty
168 _progress
= '' # Our last progress message
169 _color
= terminal
.Color()
172 # TODO(sjg): Move this into Chromite libraries when we have them
173 stdout_is_tty
= hasattr(sys
.stdout
, 'isatty') and sys
.stdout
.isatty()
174 stderr_is_tty
= hasattr(sys
.stderr
, 'isatty') and sys
.stderr
.isatty()