OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/diagnostics/diagnostics_main.h" | 5 #include "chrome/browser/diagnostics/diagnostics_main.h" |
6 | 6 |
7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 #endif | 10 #endif |
11 | 11 |
12 #include <iostream> | |
13 #include <string> | 12 #include <string> |
14 | 13 |
15 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
16 #include "base/command_line.h" | 15 #include "base/command_line.h" |
17 #include "base/i18n/icu_util.h" | 16 #include "base/i18n/icu_util.h" |
18 #include "base/logging.h" | 17 #include "base/logging.h" |
19 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
20 #include "base/sys_string_conversions.h" | 19 #include "base/sys_string_conversions.h" |
21 #include "base/time.h" | 20 #include "base/time.h" |
22 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
(...skipping 16 matching lines...) Expand all Loading... |
39 | 38 |
40 virtual ~SimpleConsole() { } | 39 virtual ~SimpleConsole() { } |
41 | 40 |
42 // Init must be called before using any other method. If it returns | 41 // Init must be called before using any other method. If it returns |
43 // false there would be no console output. | 42 // false there would be no console output. |
44 virtual bool Init() = 0; | 43 virtual bool Init() = 0; |
45 | 44 |
46 // Writes a string to the console with the current color. | 45 // Writes a string to the console with the current color. |
47 virtual bool Write(const std::wstring& text) = 0; | 46 virtual bool Write(const std::wstring& text) = 0; |
48 | 47 |
49 // Reads a string from the console. Internally it may be limited to 256 | 48 // Called when the program is about to exit. |
50 // characters. | 49 virtual void OnQuit() = 0; |
51 virtual bool Read(std::wstring* txt) = 0; | |
52 | 50 |
53 // Sets the foreground and background color. | 51 // Sets the foreground and background color. |
54 virtual bool SetColor(Color color) = 0; | 52 virtual bool SetColor(Color color) = 0; |
55 | 53 |
56 // Create an appropriate SimpleConsole instance. May return NULL if there is | 54 // Create an appropriate SimpleConsole instance. May return NULL if there is |
57 // no implementation for the current platform. | 55 // no implementation for the current platform. |
58 static SimpleConsole* Create(); | 56 static SimpleConsole* Create(); |
59 }; | 57 }; |
60 | 58 |
61 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
(...skipping 16 matching lines...) Expand all Loading... |
78 return SetIOHandles(); | 76 return SetIOHandles(); |
79 } | 77 } |
80 | 78 |
81 virtual bool Write(const std::wstring& txt) { | 79 virtual bool Write(const std::wstring& txt) { |
82 DWORD sz = txt.size(); | 80 DWORD sz = txt.size(); |
83 return (TRUE == ::WriteConsoleW(std_out_, txt.c_str(), sz, &sz, NULL)); | 81 return (TRUE == ::WriteConsoleW(std_out_, txt.c_str(), sz, &sz, NULL)); |
84 } | 82 } |
85 | 83 |
86 // Reads a string from the console. Internally it is limited to 256 | 84 // Reads a string from the console. Internally it is limited to 256 |
87 // characters. | 85 // characters. |
88 virtual bool Read(std::wstring* txt) { | 86 virtual void OnQuit() { |
| 87 // Block here so the user can see the results. |
| 88 SetColor(SimpleConsole::DEFAULT); |
| 89 Write(L"Press [enter] to continue\n"); |
89 wchar_t buf[256]; | 90 wchar_t buf[256]; |
90 DWORD read = sizeof(buf) - sizeof(buf[0]); | 91 DWORD read = arraysize(buf); |
91 if (!::ReadConsoleW(std_in_, buf, read, &read, NULL)) | 92 ::ReadConsoleW(std_in_, buf, read, &read, NULL); |
92 return false; | |
93 // Note that |read| is in bytes. | |
94 txt->assign(buf, read/2); | |
95 return true; | |
96 } | 93 } |
97 | 94 |
98 // Sets the foreground and background color. | 95 // Sets the foreground and background color. |
99 virtual bool SetColor(Color color) { | 96 virtual bool SetColor(Color color) { |
100 uint16 color_combo = | 97 uint16 color_combo = |
101 FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY; | 98 FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY; |
102 switch (color) { | 99 switch (color) { |
103 case RED: | 100 case RED: |
104 color_combo = FOREGROUND_RED|FOREGROUND_INTENSITY; | 101 color_combo = FOREGROUND_RED|FOREGROUND_INTENSITY; |
105 break; | 102 break; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 // color, but in practice this is unlikely to be an issue. | 142 // color, but in practice this is unlikely to be an issue. |
146 use_color_ = isatty(STDOUT_FILENO); | 143 use_color_ = isatty(STDOUT_FILENO); |
147 return true; | 144 return true; |
148 } | 145 } |
149 | 146 |
150 virtual bool Write(const std::wstring& text) { | 147 virtual bool Write(const std::wstring& text) { |
151 printf("%s", base::SysWideToNativeMB(text).c_str()); | 148 printf("%s", base::SysWideToNativeMB(text).c_str()); |
152 return true; | 149 return true; |
153 } | 150 } |
154 | 151 |
155 virtual bool Read(std::wstring* txt) { | 152 virtual void OnQuit() { |
156 std::string input; | 153 // The "press enter to continue" prompt isn't very unixy, so only do that on |
157 if (!std::getline(std::cin, input)) { | 154 // Windows. |
158 std::cin.clear(); | |
159 return false; | |
160 } | |
161 *txt = UTF8ToWide(input); | |
162 return true; | |
163 } | 155 } |
164 | 156 |
165 virtual bool SetColor(Color color) { | 157 virtual bool SetColor(Color color) { |
166 if (!use_color_) | 158 if (!use_color_) |
167 return false; | 159 return false; |
168 | 160 |
169 const char* code = "\033[m"; | 161 const char* code = "\033[m"; |
170 switch (color) { | 162 switch (color) { |
171 case RED: | 163 case RED: |
172 code = "\033[1;31m"; | 164 code = "\033[1;31m"; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 chrome::RegisterPathProvider(); | 346 chrome::RegisterPathProvider(); |
355 | 347 |
356 TestWriter writer(console); | 348 TestWriter writer(console); |
357 DiagnosticsModel* model = MakeDiagnosticsModel(command_line); | 349 DiagnosticsModel* model = MakeDiagnosticsModel(command_line); |
358 TestController controller(&writer); | 350 TestController controller(&writer); |
359 | 351 |
360 // Run all the diagnostic tests. | 352 // Run all the diagnostic tests. |
361 controller.Run(model); | 353 controller.Run(model); |
362 delete model; | 354 delete model; |
363 | 355 |
364 // The "press enter to continue" prompt isn't very unixy, so only do that on | |
365 // Windows. | |
366 #if defined(OS_WIN) | |
367 // Block here so the user can see the results. | |
368 writer.WriteInfoText(L"Press [enter] to continue\n"); | |
369 std::wstring txt; | |
370 console->Read(&txt); | |
371 #endif | |
372 delete console; | 356 delete console; |
373 return 0; | 357 return 0; |
374 } | 358 } |
OLD | NEW |