blob: ae04c30f1db637c12ddf8684f160b76d7e5dedf5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass0faf6142016-07-25 18:59:09 -06002# Copyright (c) 2016 Google, Inc
3#
Simon Glass0faf6142016-07-25 18:59:09 -06004# Terminal output logging.
5#
6
Simon Glass7b773162019-07-20 12:23:26 -06007from __future__ import print_function
8
Simon Glass0faf6142016-07-25 18:59:09 -06009import sys
10
11import terminal
12
13# Output verbosity levels that we support
14ERROR = 0
15WARNING = 1
16NOTICE = 2
17INFO = 3
18DEBUG = 4
19
Simon Glass008b0302018-10-01 21:12:45 -060020in_progress = False
21
Simon Glass0faf6142016-07-25 18:59:09 -060022"""
23This class handles output of progress and other useful information
24to the user. It provides for simple verbosity level control and can
25output nothing but errors at verbosity zero.
26
27The idea is that modules set up an Output object early in their years and pass
28it around to other modules that need it. This keeps the output under control
29of a single class.
30
31Public properties:
32 verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
33"""
34def __enter__():
35 return
36
37def __exit__(unused1, unused2, unused3):
38 """Clean up and remove any progress message."""
39 ClearProgress()
40 return False
41
42def UserIsPresent():
43 """This returns True if it is likely that a user is present.
44
45 Sometimes we want to prompt the user, but if no one is there then this
46 is a waste of time, and may lock a script which should otherwise fail.
47
48 Returns:
49 True if it thinks the user is there, and False otherwise
50 """
51 return stdout_is_tty and verbose > 0
52
53def ClearProgress():
54 """Clear any active progress message on the terminal."""
Simon Glass008b0302018-10-01 21:12:45 -060055 global in_progress
56 if verbose > 0 and stdout_is_tty and in_progress:
Simon Glass0faf6142016-07-25 18:59:09 -060057 _stdout.write('\r%s\r' % (" " * len (_progress)))
58 _stdout.flush()
Simon Glass008b0302018-10-01 21:12:45 -060059 in_progress = False
Simon Glass0faf6142016-07-25 18:59:09 -060060
61def Progress(msg, warning=False, trailer='...'):
62 """Display progress information.
63
64 Args:
65 msg: Message to display.
66 warning: True if this is a warning."""
Simon Glass008b0302018-10-01 21:12:45 -060067 global in_progress
Simon Glass0faf6142016-07-25 18:59:09 -060068 ClearProgress()
69 if verbose > 0:
70 _progress = msg + trailer
71 if stdout_is_tty:
72 col = _color.YELLOW if warning else _color.GREEN
73 _stdout.write('\r' + _color.Color(col, _progress))
74 _stdout.flush()
Simon Glass008b0302018-10-01 21:12:45 -060075 in_progress = True
Simon Glass0faf6142016-07-25 18:59:09 -060076 else:
77 _stdout.write(_progress + '\n')
78
79def _Output(level, msg, color=None):
80 """Output a message to the terminal.
81
82 Args:
83 level: Verbosity level for this message. It will only be displayed if
84 this as high as the currently selected level.
85 msg; Message to display.
86 error: True if this is an error message, else False.
87 """
88 if verbose >= level:
89 ClearProgress()
90 if color:
91 msg = _color.Color(color, msg)
Simon Glass7b773162019-07-20 12:23:26 -060092 print(msg)
Simon Glass0faf6142016-07-25 18:59:09 -060093
94def DoOutput(level, msg):
95 """Output a message to the terminal.
96
97 Args:
98 level: Verbosity level for this message. It will only be displayed if
99 this as high as the currently selected level.
100 msg; Message to display.
101 """
102 _Output(level, msg)
103
104def Error(msg):
105 """Display an error message
106
107 Args:
108 msg; Message to display.
109 """
110 _Output(0, msg, _color.RED)
111
112def Warning(msg):
113 """Display a warning message
114
115 Args:
116 msg; Message to display.
117 """
118 _Output(1, msg, _color.YELLOW)
119
120def Notice(msg):
121 """Display an important infomation message
122
123 Args:
124 msg; Message to display.
125 """
126 _Output(2, msg)
127
128def Info(msg):
129 """Display an infomation message
130
131 Args:
132 msg; Message to display.
133 """
134 _Output(3, msg)
135
Simon Glasseea264e2019-07-08 14:25:49 -0600136def Detail(msg):
137 """Display a detailed message
138
139 Args:
140 msg; Message to display.
141 """
142 _Output(4, msg)
143
Simon Glass0faf6142016-07-25 18:59:09 -0600144def Debug(msg):
145 """Display a debug message
146
147 Args:
148 msg; Message to display.
149 """
Simon Glasseea264e2019-07-08 14:25:49 -0600150 _Output(5, msg)
Simon Glass0faf6142016-07-25 18:59:09 -0600151
152def UserOutput(msg):
153 """Display a message regardless of the current output level.
154
155 This is used when the output was specifically requested by the user.
156 Args:
157 msg; Message to display.
158 """
159 _Output(0, msg)
160
161def Init(_verbose=WARNING, stdout=sys.stdout):
162 """Initialize a new output object.
163
164 Args:
165 verbose: Verbosity level (0-4).
166 stdout: File to use for stdout.
167 """
168 global verbose, _progress, _color, _stdout, stdout_is_tty
169
170 verbose = _verbose
171 _progress = '' # Our last progress message
172 _color = terminal.Color()
173 _stdout = stdout
174
175 # TODO(sjg): Move this into Chromite libraries when we have them
176 stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
177
178def Uninit():
179 ClearProgress()
180
181Init()