2a384851b0d186fe1ecd7ddd273e201dd94495bf
[oweals/u-boot.git] / tools / patman / tout.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
3 #
4 # Terminal output logging.
5 #
6
7 from __future__ import print_function
8
9 import sys
10
11 import terminal
12
13 # Output verbosity levels that we support
14 ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(6)
15
16 in_progress = False
17
18 """
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.
22
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
25 of a single class.
26
27 Public properties:
28     verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
29 """
30 def __enter__():
31     return
32
33 def __exit__(unused1, unused2, unused3):
34     """Clean up and remove any progress message."""
35     ClearProgress()
36     return False
37
38 def UserIsPresent():
39     """This returns True if it is likely that a user is present.
40
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.
43
44     Returns:
45         True if it thinks the user is there, and False otherwise
46     """
47     return stdout_is_tty and verbose > 0
48
49 def ClearProgress():
50     """Clear any active progress message on the terminal."""
51     global in_progress
52     if verbose > 0 and stdout_is_tty and in_progress:
53         _stdout.write('\r%s\r' % (" " * len (_progress)))
54         _stdout.flush()
55         in_progress = False
56
57 def Progress(msg, warning=False, trailer='...'):
58     """Display progress information.
59
60     Args:
61         msg: Message to display.
62         warning: True if this is a warning."""
63     global in_progress
64     ClearProgress()
65     if verbose > 0:
66         _progress = msg + trailer
67         if stdout_is_tty:
68             col = _color.YELLOW if warning else _color.GREEN
69             _stdout.write('\r' + _color.Color(col, _progress))
70             _stdout.flush()
71             in_progress = True
72         else:
73             _stdout.write(_progress + '\n')
74
75 def _Output(level, msg, color=None):
76     """Output a message to the terminal.
77
78     Args:
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.
83     """
84     if verbose >= level:
85         ClearProgress()
86         if color:
87             msg = _color.Color(color, msg)
88         print(msg)
89
90 def DoOutput(level, msg):
91     """Output a message to the terminal.
92
93     Args:
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.
97     """
98     _Output(level, msg)
99
100 def Error(msg):
101     """Display an error message
102
103     Args:
104         msg; Message to display.
105     """
106     _Output(ERROR, msg, _color.RED)
107
108 def Warning(msg):
109     """Display a warning message
110
111     Args:
112         msg; Message to display.
113     """
114     _Output(WARNING, msg, _color.YELLOW)
115
116 def Notice(msg):
117     """Display an important infomation message
118
119     Args:
120         msg; Message to display.
121     """
122     _Output(NOTICE, msg)
123
124 def Info(msg):
125     """Display an infomation message
126
127     Args:
128         msg; Message to display.
129     """
130     _Output(INFO, msg)
131
132 def Detail(msg):
133     """Display a detailed message
134
135     Args:
136         msg; Message to display.
137     """
138     _Output(DETAIL, msg)
139
140 def Debug(msg):
141     """Display a debug message
142
143     Args:
144         msg; Message to display.
145     """
146     _Output(DEBUG, msg)
147
148 def UserOutput(msg):
149     """Display a message regardless of the current output level.
150
151     This is used when the output was specifically requested by the user.
152     Args:
153         msg; Message to display.
154     """
155     _Output(0, msg)
156
157 def Init(_verbose=WARNING, stdout=sys.stdout):
158     """Initialize a new output object.
159
160     Args:
161         verbose: Verbosity level (0-4).
162         stdout: File to use for stdout.
163     """
164     global verbose, _progress, _color, _stdout, stdout_is_tty
165
166     verbose = _verbose
167     _progress = ''                    # Our last progress message
168     _color = terminal.Color()
169     _stdout = stdout
170
171     # TODO(sjg): Move this into Chromite libraries when we have them
172     stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
173
174 def Uninit():
175     ClearProgress()
176
177 Init()