Skip to content

Commit

Permalink
Fix CR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk committed Feb 17, 2022
1 parent 2f9a5dd commit a5f29ef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
17 changes: 7 additions & 10 deletions src/googletest.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,12 @@ static inline void WriteToFile(const string& body, const string& file) {
fclose(fp);
}

static inline bool MungeAndDiffTestStderrOrStdout(const string& golden_filename,
bool is_stdout) {
CapturedStream* cap;
if (!is_stdout) {
cap = s_captured_streams[STDERR_FILENO];
CHECK(cap) << ": did you forget CaptureTestStderr()?";
} else {
cap = s_captured_streams[STDOUT_FILENO];
static inline bool MungeAndDiffTest(const string& golden_filename,
CapturedStream *cap) {
if ( cap == s_captured_streams[STDOUT_FILENO]) {
CHECK(cap) << ": did you forget CaptureTestStdout()?";
} else {
CHECK(cap) << ": did you forget CaptureTestStderr()?";
}

cap->StopCapture();
Expand Down Expand Up @@ -547,11 +544,11 @@ static inline bool MungeAndDiffTestStderrOrStdout(const string& golden_filename,
}

static inline bool MungeAndDiffTestStderr(const string& golden_filename) {
return MungeAndDiffTestStderrOrStdout(golden_filename, false);
return MungeAndDiffTest(golden_filename, s_captured_streams[STDERR_FILENO]);
}

static inline bool MungeAndDiffTestStdout(const string& golden_filename) {
return MungeAndDiffTestStderrOrStdout(golden_filename, true);
return MungeAndDiffTest(golden_filename, s_captured_streams[STDOUT_FILENO]);
}

// Save flags used from logging_unittest.cc.
Expand Down
52 changes: 27 additions & 25 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -743,23 +743,19 @@ inline void LogDestination::SetEmailLogging(LogSeverity min_severity,
LogDestination::addresses_ = addresses;
}

static void ColoredWriteToStderrOrStdout(bool is_stdout, LogSeverity severity,
const char* message, size_t len) {
// We also need to send logs to the stderr when the severity is
// higher or equal to the stderr threshold.
is_stdout = is_stdout && severity < FLAGS_stderrthreshold;

const GLogColor color = (LogDestination::terminal_supports_color() &&
((!is_stdout && FLAGS_colorlogtostderr) ||
(is_stdout && FLAGS_colorlogtostdout)))
? SeverityToColor(severity)
: COLOR_DEFAULT;
static void ColoredWriteToStderrOrStdout(FILE *output, LogSeverity severity,
const char* message, size_t len) {
bool is_stdout = (output == stdout);
const GLogColor color =
(LogDestination::terminal_supports_color()
&& ((!is_stdout && FLAGS_colorlogtostderr)
|| (is_stdout && FLAGS_colorlogtostdout))) ?
SeverityToColor(severity) : COLOR_DEFAULT;

FILE* output_fd = is_stdout ? stdout : stderr;
// Avoid using cerr from this module since we may get called during
// exit code, and cerr may be partially or fully destroyed by then.
if (COLOR_DEFAULT == color) {
fwrite(message, len, 1, output_fd);
fwrite(message, len, 1, output);
return;
}
#ifdef GLOG_OS_WINDOWS
Expand All @@ -774,28 +770,34 @@ static void ColoredWriteToStderrOrStdout(bool is_stdout, LogSeverity severity,
// We need to flush the stream buffers into the console before each
// SetConsoleTextAttribute call lest it affect the text that is already
// printed but has not yet reached the console.
fflush(output_fd);
fflush(output);
SetConsoleTextAttribute(output_handle,
GetColorAttribute(color) | FOREGROUND_INTENSITY);
fwrite(message, len, 1, output_fd);
fflush(output_fd);
fwrite(message, len, 1, output);
fflush(output);
// Restores the text color.
SetConsoleTextAttribute(output_handle, old_color_attrs);
#else
fprintf(output_fd, "\033[0;3%sm", GetAnsiColorCode(color));
fwrite(message, len, 1, output_fd);
fprintf(output_fd, "\033[m"); // Resets the terminal to default.
fprintf(output, "\033[0;3%sm", GetAnsiColorCode(color));
fwrite(message, len, 1, output);
fprintf(output, "\033[m"); // Resets the terminal to default.
#endif // GLOG_OS_WINDOWS
}

static void ColoredWriteToStdout(LogSeverity severity, const char* message,
size_t len) {
ColoredWriteToStderrOrStdout(true, severity, message, len);
static void ColoredWriteToStdout(LogSeverity severity,
const char* message, size_t len) {
FILE *output = stdout;
// We also need to send logs to the stderr when the severity is
// higher or equal to the stderr threshold.
if ( severity >= FLAGS_stderrthreshold ) {
output = stderr;
}
ColoredWriteToStderrOrStdout(output, severity, message, len);
}

static void ColoredWriteToStderr(LogSeverity severity, const char* message,
size_t len) {
ColoredWriteToStderrOrStdout(false, severity, message, len);
static void ColoredWriteToStderr(LogSeverity severity,
const char* message, size_t len) {
ColoredWriteToStderrOrStdout(stderr, severity, message, len);
}

static void WriteToStderr(const char* message, size_t len) {
Expand Down

0 comments on commit a5f29ef

Please sign in to comment.