String Verbose::Colorize(rcString inMsg, Color inColor, Style inStyle) { if (!ShouldUseColor()) return inMsg; std::stringstream s; s << "\033[" << inStyle << ";" << inColor << "m" << inMsg << "\033[0m"; return s.str(); }
// 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); }
Verbose::Verbose(): mIsStartOfLine(true) { sShouldUseColor = ShouldUseColor(); }