blob: b638db72d954221ab59a13400639b6bed496dd26 [file] [log] [blame]
Simon Glassab344e32011-07-17 09:17:07 -07001# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import sys
7
8import cros_build_lib
9
10# Output verbosity levels that we support
11ERROR = 0
12WARNING = 1
13NOTICE = 2
14INFO = 3
15DEBUG = 4
16
17class Output:
18 """Output class for Chrome OS.
19
20 This class handles output of progress and other useful information
21 to the user. It provides for simple verbosity level control and can
22 output nothing but errors at verbosity zero.
23
24 The idea is that modules set up an Output object early in their years and pass
25 it around to other modules that need it. This keeps the output under control
26 of a single class.
27
28 TODO(sjg): Merge / join with Chromite libraries
29
30 Public properties:
31 verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
32 """
33 def __init__(self, verbose, stdout=sys.stdout):
34 """Initialize a new output object.
35
36 Args:
37 verbose: Verbosity level (0-4).
38 stdout: File to use for stdout.
39 """
40 self.verbose = verbose
41 self._progress = '' # Our last progress message
42 self._color = cros_build_lib.Color()
43 self._stdout = stdout
44
45 # TODO(sjg): Move this into Chromite libraries when we have them
46 self.stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
47
Simon Glass1f778c92011-08-09 13:30:43 -070048 def __enter__(self):
49 return self
50
51 def __exit__(self, type, value, traceback):
Simon Glassab344e32011-07-17 09:17:07 -070052 """Clean up and remove any progress message."""
53 self.ClearProgress()
Simon Glass1f778c92011-08-09 13:30:43 -070054 return False
Simon Glassab344e32011-07-17 09:17:07 -070055
56 def UserIsPresent(self):
57 """This returns True if it is likely that a user is present.
58
59 Sometimes we want to prompt the user, but if no one is there then this
60 is a waste of time, and may lock a script which should otherwise fail.
61
62 Returns:
63 True if it thinks the user is there, and False otherwise
64 """
65 return self.stdout_is_tty and self.verbose > 0
66
67 def ClearProgress(self):
68 """Clear any active progress message on the terminal."""
69 if self.verbose > 0 and self.stdout_is_tty:
70 self._stdout.write('\r%s\r' % (" " * len (self._progress)))
71 self._stdout.flush()
72
73 def Progress(self, msg, warning=False):
74 """Display progress information.
75
76 Args:
77 msg: Message to display.
78 warning: True if this is a warning."""
79 self.ClearProgress()
80 if self.verbose > 0:
81 self._progress = msg + '...'
82 if self.stdout_is_tty:
83 col = self._color.YELLOW if warning else self._color.GREEN
84 self._stdout.write('\r' + self._color.Color(col, self._progress))
85 self._stdout.flush()
86 else:
87 self._stdout.write(self._progress + '\n')
88
89 def _Output(self, level, msg, error=False):
90 """Output a message to the terminal.
91
92 Args:
93 level: Verbosity level for this message. It will only be displayed if
94 this as high as the currently selected level.
95 msg; Message to display.
96 error: True if this is an error message, else False.
97 """
98 self.ClearProgress()
99 if self.verbose >= level:
100 if error:
101 msg = self._color.Color(self._color.RED, msg)
102 self._stdout.write(msg + '\n')
103
104 def DoOutput(self, level, msg):
105 """Output a message to the terminal.
106
107 Args:
108 level: Verbosity level for this message. It will only be displayed if
109 this as high as the currently selected level.
110 msg; Message to display.
111 """
112 self._Output(level, msg)
113
114 def Error(self, msg):
115 """Display an error message
116
117 Args:
118 msg; Message to display.
119 """
120 self._Output(0, msg, True)
121
122 def Warning(self, msg):
123 """Display a warning message
124
125 Args:
126 msg; Message to display.
127 """
128 self._Output(1, msg)
129
130 def Notice(self, msg):
131 """Display an important infomation message
132
133 Args:
134 msg; Message to display.
135 """
136 self._Output(2, msg)
137
138 def Info(self, msg):
139 """Display an infomation message
140
141 Args:
142 msg; Message to display.
143 """
144 self._Output(3, msg)
145
146 def Debug(self, msg):
147 """Display a debug message
148
149 Args:
150 msg; Message to display.
151 """
152 self._Output(4, msg)