static void Write_VA(Verbosity verbosity, CLIColour colour, const char* fmt, va_list args) { if (_verbosity < verbosity) return; COLOUR_METHOD colourMethod = GetColourMethod(); if (colour == CLIColour::DEFAULT || colourMethod == COLOUR_METHOD_NONE) { vprintf(fmt, args); } else if (colourMethod == COLOUR_METHOD_ANSI) { printf("\033[0;3%sm", GetAnsiColorCode(colour)); vprintf(fmt, args); printf("\033[m"); } else if (colourMethod == COLOUR_METHOD_WINDOWS) { #ifdef __WINDOWS__ HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); WORD defaultAttr = GetCurrentWindowsConsoleAttribute(hStdOut); SetConsoleTextAttribute(hStdOut, GetWindowsConsoleAttribute(colour, defaultAttr)); vprintf(fmt, args); SetConsoleTextAttribute(hStdOut, defaultAttr); #endif } }
// Linux print colored string to stdout. void ColoredPrintf(ConsoleColor color, const char* fmt, ...) { va_list args; va_start(args, fmt); // if (!use_color_) // { // vprintf(fmt, args); // va_end(args); // return; // } string color_code = GetAnsiColorCode(color); printf("%s", color_code.c_str()); vprintf(fmt, args); printf("%s", "\033[m"); // reset the terminal to default. va_end(args); }
// Helpers for printing colored strings to stdout. Note that on Windows, we // cannot simply emit special characters and have the terminal change colors. // This routine must actually emit the characters rather than return a string // that would be colored when printed, as can be done on Linux. void ColoredPrintf(LogogColor color, const char* fmt, ...) { va_list args; va_start(args, fmt); #if !defined(LOGOG_FLAVOR_WINDOWS) && !defined(LOGOG_FLAVOR_POSIX) const bool use_color = false; #else static const bool in_color_mode = ShouldUseColor(true);//isatty(fileno(stdout)) != 0); const bool use_color = in_color_mode && (color != COLOR_DEFAULT); #endif // !LOGOG_FLAVOR_POSIX && !LOGOG_FLAVOR_WINDOWS // The '!= 0' comparison is necessary to satisfy MSVC 7.1. if (!use_color) { vprintf(fmt, args); va_end(args); return; } #ifdef LOGOG_FLAVOR_WINDOWS const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); // Gets the current text color. CONSOLE_SCREEN_BUFFER_INFO buffer_info; GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); const WORD old_color_attrs = buffer_info.wAttributes; // 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(stdout); SetConsoleTextAttribute(stdout_handle, GetColorAttribute(color) | FOREGROUND_INTENSITY); vprintf(fmt, args); fflush(stdout); // Restores the text color. SetConsoleTextAttribute(stdout_handle, old_color_attrs); #else printf("\033[0;3%sm", GetAnsiColorCode(color)); vprintf(fmt, args); printf("\033[m"); // Resets the terminal to default. #endif // LOGOG_FLAVOR_WINDOWS va_end(args); }
void ColoredPrintf(GTestColor color, const char* fmt, ...) { va_list args; va_start(args, fmt); static const bool in_color_mode = true; const bool use_color = in_color_mode && (color != COLOR_DEFAULT); if (!use_color) { vprintf(fmt, args); va_end(args); return; } printf("\033[0;3%sm", GetAnsiColorCode(color)); vprintf(fmt, args); printf("\033[m"); // Resets the terminal to default. va_end(args); }
void TestSystem::printMetrics(int is_accurate, double cpu_time, double gpu_time, double gpu_full_time, double speedup, double fullspeedup) { cout << setiosflags(ios_base::left); stringstream stream; #if 0 if(is_accurate == 1) stream << "Pass"; else if(is_accurate_ == 0) stream << "Fail"; else if(is_accurate == -1) stream << " "; else { std::cout<<"is_accurate errer: "<<is_accurate<<"\n"; exit(-1); } #endif std::stringstream &cur_subtest_description = getCurSubtestDescription(); #if GTEST_OS_WINDOWS&&!GTEST_OS_WINDOWS_MOBILE WORD color; const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); // Gets the current text color. CONSOLE_SCREEN_BUFFER_INFO buffer_info; GetConsoleScreenBufferInfo(stdout_handle, &buffer_info); const WORD old_color_attrs = buffer_info.wAttributes; // 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(stdout); if(is_accurate == 1||is_accurate == -1) { color = old_color_attrs; printMetricsUti(cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup, stream, cur_subtest_description); } else { color = GetColorAttribute(COLOR_RED); SetConsoleTextAttribute(stdout_handle, color| FOREGROUND_INTENSITY); printMetricsUti(cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup, stream, cur_subtest_description); fflush(stdout); // Restores the text color. SetConsoleTextAttribute(stdout_handle, old_color_attrs); } #else GTestColor color = COLOR_RED; if(is_accurate == 1|| is_accurate == -1) { printMetricsUti(cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup, stream, cur_subtest_description); } else { printf("\033[0;3%sm", GetAnsiColorCode(color)); printMetricsUti(cpu_time, gpu_time, gpu_full_time, speedup, fullspeedup, stream, cur_subtest_description); printf("\033[m"); // Resets the terminal to default. } #endif }