blob: d8891bf3e79b32f42822bc59434aba96a451cb3d [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
48 def __del__(self):
49 """Clean up and remove any progress message."""
50 self.ClearProgress()
51
52 def UserIsPresent(self):
53 """This returns True if it is likely that a user is present.
54
55 Sometimes we want to prompt the user, but if no one is there then this
56 is a waste of time, and may lock a script which should otherwise fail.
57
58 Returns:
59 True if it thinks the user is there, and False otherwise
60 """
61 return self.stdout_is_tty and self.verbose > 0
62
63 def ClearProgress(self):
64 """Clear any active progress message on the terminal."""
65 if self.verbose > 0 and self.stdout_is_tty:
66 self._stdout.write('\r%s\r' % (" " * len (self._progress)))
67 self._stdout.flush()
68
69 def Progress(self, msg, warning=False):
70 """Display progress information.
71
72 Args:
73 msg: Message to display.
74 warning: True if this is a warning."""
75 self.ClearProgress()
76 if self.verbose > 0:
77 self._progress = msg + '...'
78 if self.stdout_is_tty:
79 col = self._color.YELLOW if warning else self._color.GREEN
80 self._stdout.write('\r' + self._color.Color(col, self._progress))
81 self._stdout.flush()
82 else:
83 self._stdout.write(self._progress + '\n')
84
85 def _Output(self, level, msg, error=False):
86 """Output a message to the terminal.
87
88 Args:
89 level: Verbosity level for this message. It will only be displayed if
90 this as high as the currently selected level.
91 msg; Message to display.
92 error: True if this is an error message, else False.
93 """
94 self.ClearProgress()
95 if self.verbose >= level:
96 if error:
97 msg = self._color.Color(self._color.RED, msg)
98 self._stdout.write(msg + '\n')
99
100 def DoOutput(self, level, msg):
101 """Output a message to the terminal.
102
103 Args:
104 level: Verbosity level for this message. It will only be displayed if
105 this as high as the currently selected level.
106 msg; Message to display.
107 """
108 self._Output(level, msg)
109
110 def Error(self, msg):
111 """Display an error message
112
113 Args:
114 msg; Message to display.
115 """
116 self._Output(0, msg, True)
117
118 def Warning(self, msg):
119 """Display a warning message
120
121 Args:
122 msg; Message to display.
123 """
124 self._Output(1, msg)
125
126 def Notice(self, msg):
127 """Display an important infomation message
128
129 Args:
130 msg; Message to display.
131 """
132 self._Output(2, msg)
133
134 def Info(self, msg):
135 """Display an infomation message
136
137 Args:
138 msg; Message to display.
139 """
140 self._Output(3, msg)
141
142 def Debug(self, msg):
143 """Display a debug message
144
145 Args:
146 msg; Message to display.
147 """
148 self._Output(4, msg)