This source file includes following definitions.
- std_in_
- Init
- Write
- OnQuit
- SetColor
- SetIOHandles
- Create
- Init
- Write
- OnQuit
- SetColor
- Create
- Create
- format_
- WriteInfoLine
- OnTestFinished
- OnAllTestsDone
- OnRecoveryFinished
- OnAllRecoveryDone
- WriteResult
#include "chrome/browser/diagnostics/diagnostics_writer.h"
#include "build/build_config.h"
#if defined(OS_POSIX)
#include <stdio.h>
#include <unistd.h>
#endif
#include <string>
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/logging.h"
#include "base/strings/string16.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/chrome_switches.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
namespace diagnostics {
class SimpleConsole {
public:
enum Color {
DEFAULT,
RED,
GREEN,
};
virtual ~SimpleConsole() {}
virtual bool Init() = 0;
virtual bool Write(const base::string16& text) = 0;
virtual void OnQuit() = 0;
virtual bool SetColor(Color color) = 0;
static SimpleConsole* Create();
};
#if defined(OS_WIN)
namespace {
class WinConsole : public SimpleConsole {
public:
WinConsole()
: std_out_(INVALID_HANDLE_VALUE),
std_in_(INVALID_HANDLE_VALUE) {
::AllocConsole();
}
virtual ~WinConsole() {
::FreeConsole();
}
virtual bool Init() {
return SetIOHandles();
}
virtual bool Write(const base::string16& txt) {
DWORD sz = txt.size();
return (TRUE == ::WriteConsoleW(std_out_, txt.c_str(), sz, &sz, NULL));
}
virtual void OnQuit() {
SetColor(SimpleConsole::DEFAULT);
Write(L"Press [enter] to continue\n");
wchar_t buf[256];
DWORD read = arraysize(buf);
::ReadConsoleW(std_in_, buf, read, &read, NULL);
}
virtual bool SetColor(Color color) {
uint16 color_combo = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
FOREGROUND_INTENSITY;
switch (color) {
case RED:
color_combo = FOREGROUND_RED | FOREGROUND_INTENSITY;
break;
case GREEN:
color_combo = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
break;
case DEFAULT:
break;
default:
NOTREACHED();
}
return (TRUE == ::SetConsoleTextAttribute(std_out_, color_combo));
}
private:
bool SetIOHandles() {
std_out_ = ::GetStdHandle(STD_OUTPUT_HANDLE);
std_in_ = ::GetStdHandle(STD_INPUT_HANDLE);
return ((std_out_ != INVALID_HANDLE_VALUE) &&
(std_in_ != INVALID_HANDLE_VALUE));
}
HANDLE std_out_;
HANDLE std_in_;
DISALLOW_COPY_AND_ASSIGN(WinConsole);
};
}
SimpleConsole* SimpleConsole::Create() { return new WinConsole(); }
#elif defined(OS_POSIX)
namespace {
class PosixConsole : public SimpleConsole {
public:
PosixConsole() : use_color_(false) {}
virtual bool Init() OVERRIDE {
use_color_ = isatty(STDOUT_FILENO);
return true;
}
virtual bool Write(const base::string16& text) OVERRIDE {
printf("%s", base::UTF16ToUTF8(text).c_str());
return true;
}
virtual void OnQuit() OVERRIDE {
}
virtual bool SetColor(Color color) OVERRIDE {
if (!use_color_)
return false;
const char* code = "\033[m";
switch (color) {
case RED:
code = "\033[1;31m";
break;
case GREEN:
code = "\033[1;32m";
break;
case DEFAULT:
break;
default:
NOTREACHED();
}
printf("%s", code);
return true;
}
private:
bool use_color_;
DISALLOW_COPY_AND_ASSIGN(PosixConsole);
};
}
SimpleConsole* SimpleConsole::Create() { return new PosixConsole(); }
#else
SimpleConsole* SimpleConsole::Create() { return NULL; }
#endif
DiagnosticsWriter::DiagnosticsWriter(FormatType format)
: failures_(0), format_(format) {
if (format_ != LOG) {
console_.reset(SimpleConsole::Create());
console_->Init();
}
}
DiagnosticsWriter::~DiagnosticsWriter() {
if (console_.get())
console_->OnQuit();
}
bool DiagnosticsWriter::WriteInfoLine(const std::string& info_text) {
if (format_ == LOG) {
LOG(WARNING) << info_text;
return true;
} else {
if (console_.get()) {
console_->SetColor(SimpleConsole::DEFAULT);
console_->Write(base::UTF8ToUTF16(info_text + "\n"));
}
}
return true;
}
void DiagnosticsWriter::OnTestFinished(int index, DiagnosticsModel* model) {
const DiagnosticsModel::TestInfo& test_info = model->GetTest(index);
bool success = (DiagnosticsModel::TEST_OK == test_info.GetResult());
WriteResult(success,
test_info.GetName(),
test_info.GetTitle(),
test_info.GetOutcomeCode(),
test_info.GetAdditionalInfo());
}
void DiagnosticsWriter::OnAllTestsDone(DiagnosticsModel* model) {
WriteInfoLine(
base::StringPrintf("Finished %d tests.", model->GetTestRunCount()));
}
void DiagnosticsWriter::OnRecoveryFinished(int index, DiagnosticsModel* model) {
const DiagnosticsModel::TestInfo& test_info = model->GetTest(index);
WriteInfoLine("Finished Recovery for: " + test_info.GetTitle());
}
void DiagnosticsWriter::OnAllRecoveryDone(DiagnosticsModel* model) {
WriteInfoLine("Finished All Recovery operations.");
}
bool DiagnosticsWriter::WriteResult(bool success,
const std::string& id,
const std::string& name,
int outcome_code,
const std::string& extra) {
std::string result;
SimpleConsole::Color color;
if (success) {
result = "[PASS] ";
color = SimpleConsole::GREEN;
} else {
color = SimpleConsole::RED;
result = "[FAIL] ";
failures_++;
}
if (format_ != LOG) {
if (console_.get()) {
console_->SetColor(color);
console_->Write(base::ASCIIToUTF16(result));
}
if (format_ == MACHINE) {
return WriteInfoLine(base::StringPrintf(
"%03d %s (%s)", outcome_code, id.c_str(), extra.c_str()));
} else {
return WriteInfoLine(name + "\n " + extra + "\n");
}
} else {
if (!success) {
return WriteInfoLine(base::StringPrintf("%s%03d %s (%s)",
result.c_str(),
outcome_code,
id.c_str(),
extra.c_str()));
}
}
return true;
}
}