1 // Copyright 2013 The Chromium 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.
5 #include "chrome/browser/diagnostics/diagnostics_writer.h"
7 #include "build/build_config.h"
16 #include "base/basictypes.h"
17 #include "base/command_line.h"
18 #include "base/logging.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "ui/base/ui_base_paths.h"
25 namespace diagnostics
{
27 // This is a minimalistic interface to wrap the platform console.
36 virtual ~SimpleConsole() {}
38 // Init must be called before using any other method. If it returns
39 // false there will be no console output.
40 virtual bool Init() = 0;
42 // Writes a string to the console with the current color.
43 virtual bool Write(const base::string16
& text
) = 0;
45 // Called when the program is about to exit.
46 virtual void OnQuit() = 0;
48 // Sets the foreground text color.
49 virtual bool SetColor(Color color
) = 0;
51 // Create an appropriate SimpleConsole instance. May return NULL if there is
52 // no implementation for the current platform.
53 static SimpleConsole
* Create();
59 // Wrapper for the windows console operating in high-level IO mode.
60 class WinConsole
: public SimpleConsole
{
62 // The ctor allocates a console. This avoids having to ask the user to start
63 // chrome from a command prompt.
65 : std_out_(INVALID_HANDLE_VALUE
),
66 std_in_(INVALID_HANDLE_VALUE
) {
70 virtual ~WinConsole() {
75 return SetIOHandles();
78 virtual bool Write(const base::string16
& txt
) {
79 DWORD sz
= txt
.size();
80 return (TRUE
== ::WriteConsoleW(std_out_
, txt
.c_str(), sz
, &sz
, NULL
));
83 // Reads a string from the console. Internally it is limited to 256
85 virtual void OnQuit() {
86 // Block here so the user can see the results.
87 SetColor(SimpleConsole::DEFAULT
);
88 Write(L
"Press [enter] to continue\n");
90 DWORD read
= arraysize(buf
);
91 ::ReadConsoleW(std_in_
, buf
, read
, &read
, NULL
);
94 // Sets the foreground and background color.
95 virtual bool SetColor(Color color
) {
96 uint16 color_combo
= FOREGROUND_RED
| FOREGROUND_GREEN
| FOREGROUND_BLUE
|
100 color_combo
= FOREGROUND_RED
| FOREGROUND_INTENSITY
;
103 color_combo
= FOREGROUND_GREEN
| FOREGROUND_INTENSITY
;
110 return (TRUE
== ::SetConsoleTextAttribute(std_out_
, color_combo
));
114 bool SetIOHandles() {
115 std_out_
= ::GetStdHandle(STD_OUTPUT_HANDLE
);
116 std_in_
= ::GetStdHandle(STD_INPUT_HANDLE
);
117 return ((std_out_
!= INVALID_HANDLE_VALUE
) &&
118 (std_in_
!= INVALID_HANDLE_VALUE
));
121 // The input and output handles to the screen. They seem to be
122 // implemented as pipes but they have non-documented protocol.
126 DISALLOW_COPY_AND_ASSIGN(WinConsole
);
131 SimpleConsole
* SimpleConsole::Create() { return new WinConsole(); }
133 #elif defined(OS_POSIX)
136 class PosixConsole
: public SimpleConsole
{
138 PosixConsole() : use_color_(false) {}
140 virtual bool Init() OVERRIDE
{
141 // Technically, we should also check the terminal capabilities before using
142 // color, but in practice this is unlikely to be an issue.
143 use_color_
= isatty(STDOUT_FILENO
);
147 virtual bool Write(const base::string16
& text
) OVERRIDE
{
148 // We're assuming that the terminal is using UTF-8 encoding.
149 printf("%s", base::UTF16ToUTF8(text
).c_str());
153 virtual void OnQuit() OVERRIDE
{
154 // The "press enter to continue" prompt isn't very unixy, so only do that on
158 virtual bool SetColor(Color color
) OVERRIDE
{
162 const char* code
= "\033[m";
182 DISALLOW_COPY_AND_ASSIGN(PosixConsole
);
187 SimpleConsole
* SimpleConsole::Create() { return new PosixConsole(); }
189 #else // !defined(OS_WIN) && !defined(OS_POSIX)
190 SimpleConsole
* SimpleConsole::Create() { return NULL
; }
193 ///////////////////////////////////////////////////////////
196 DiagnosticsWriter::DiagnosticsWriter(FormatType format
)
197 : failures_(0), format_(format
) {
198 // Only create consoles for non-log output.
199 if (format_
!= LOG
) {
200 console_
.reset(SimpleConsole::Create());
205 DiagnosticsWriter::~DiagnosticsWriter() {
210 bool DiagnosticsWriter::WriteInfoLine(const std::string
& info_text
) {
211 if (format_
== LOG
) {
212 LOG(WARNING
) << info_text
;
215 if (console_
.get()) {
216 console_
->SetColor(SimpleConsole::DEFAULT
);
217 console_
->Write(base::UTF8ToUTF16(info_text
+ "\n"));
223 void DiagnosticsWriter::OnTestFinished(int index
, DiagnosticsModel
* model
) {
224 const DiagnosticsModel::TestInfo
& test_info
= model
->GetTest(index
);
225 bool success
= (DiagnosticsModel::TEST_OK
== test_info
.GetResult());
228 test_info
.GetTitle(),
229 test_info
.GetOutcomeCode(),
230 test_info
.GetAdditionalInfo());
233 void DiagnosticsWriter::OnAllTestsDone(DiagnosticsModel
* model
) {
235 base::StringPrintf("Finished %d tests.", model
->GetTestRunCount()));
238 void DiagnosticsWriter::OnRecoveryFinished(int index
, DiagnosticsModel
* model
) {
239 const DiagnosticsModel::TestInfo
& test_info
= model
->GetTest(index
);
240 WriteInfoLine("Finished Recovery for: " + test_info
.GetTitle());
243 void DiagnosticsWriter::OnAllRecoveryDone(DiagnosticsModel
* model
) {
244 WriteInfoLine("Finished All Recovery operations.");
247 bool DiagnosticsWriter::WriteResult(bool success
,
248 const std::string
& id
,
249 const std::string
& name
,
251 const std::string
& extra
) {
253 SimpleConsole::Color color
;
257 color
= SimpleConsole::GREEN
;
259 color
= SimpleConsole::RED
;
264 if (format_
!= LOG
) {
265 if (console_
.get()) {
266 console_
->SetColor(color
);
267 console_
->Write(base::ASCIIToUTF16(result
));
269 if (format_
== MACHINE
) {
270 return WriteInfoLine(base::StringPrintf(
271 "%03d %s (%s)", outcome_code
, id
.c_str(), extra
.c_str()));
273 return WriteInfoLine(name
+ "\n " + extra
+ "\n");
277 // For log output, we only care about the tests that failed.
278 return WriteInfoLine(base::StringPrintf("%s%03d %s (%s)",
288 } // namespace diagnostics