#include "chrome/browser/diagnostics/diagnostics_writer.h"
#include <stdint.h>
#include <string>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/common/chrome_switches.h"
#include "ui/base/ui_base_paths.h"
#if BUILDFLAG(IS_POSIX)
#include <stdio.h>
#include <unistd.h>
#elif BUILDFLAG(IS_WIN)
#include <windows.h>
#endif
namespace diagnostics {
class SimpleConsole { … };
#if BUILDFLAG(IS_WIN)
namespace {
class WinConsole : public SimpleConsole {
public:
WinConsole()
: std_out_(INVALID_HANDLE_VALUE),
std_in_(INVALID_HANDLE_VALUE) {
::AllocConsole();
}
WinConsole(const WinConsole&) = delete;
WinConsole& operator=(const WinConsole&) = delete;
~WinConsole() override { ::FreeConsole(); }
bool Init() override { return SetIOHandles(); }
bool Write(const std::u16string& txt) override {
DWORD sz = txt.size();
return (TRUE ==
::WriteConsoleW(std_out_, base::as_wcstr(txt), sz, &sz, NULL));
}
void OnQuit() override {
SetColor(SimpleConsole::DEFAULT);
Write(u"Press [enter] to continue\n");
wchar_t buf[256];
DWORD read = std::size(buf);
::ReadConsoleW(std_in_, buf, read, &read, NULL);
}
bool SetColor(Color color) override {
uint16_t 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_IN_MIGRATION();
}
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_;
};
}
SimpleConsole* SimpleConsole::Create() { return new WinConsole(); }
#elif BUILDFLAG(IS_POSIX)
namespace {
class PosixConsole : public SimpleConsole { … };
}
SimpleConsole* SimpleConsole::Create() { … }
#else
SimpleConsole* SimpleConsole::Create() { return NULL; }
#endif
DiagnosticsWriter::DiagnosticsWriter(FormatType format)
: … { … }
DiagnosticsWriter::~DiagnosticsWriter() { … }
bool DiagnosticsWriter::WriteInfoLine(const std::string& info_text) { … }
void DiagnosticsWriter::OnTestFinished(int index, DiagnosticsModel* model) { … }
void DiagnosticsWriter::OnAllTestsDone(DiagnosticsModel* model) { … }
void DiagnosticsWriter::OnRecoveryFinished(int index, DiagnosticsModel* model) { … }
void DiagnosticsWriter::OnAllRecoveryDone(DiagnosticsModel* model) { … }
bool DiagnosticsWriter::WriteResult(bool success,
const std::string& id,
const std::string& name,
int outcome_code,
const std::string& extra) { … }
}