diff options
Diffstat (limited to 'src/common/logging/text_formatter.cpp')
| -rw-r--r-- | src/common/logging/text_formatter.cpp | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp index 3fe435346..f6b02fd47 100644 --- a/src/common/logging/text_formatter.cpp +++ b/src/common/logging/text_formatter.cpp | |||
| @@ -54,12 +54,22 @@ void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len) { | |||
| 54 | TrimSourcePath(entry.location.c_str()), entry.message.c_str()); | 54 | TrimSourcePath(entry.location.c_str()), entry.message.c_str()); |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | static void ChangeConsoleColor(Level level) { | 57 | void PrintMessage(const Entry& entry) { |
| 58 | std::array<char, 4 * 1024> format_buffer; | ||
| 59 | FormatLogMessage(entry, format_buffer.data(), format_buffer.size()); | ||
| 60 | fputs(format_buffer.data(), stderr); | ||
| 61 | fputc('\n', stderr); | ||
| 62 | } | ||
| 63 | |||
| 64 | void PrintColoredMessage(const Entry& entry) { | ||
| 58 | #ifdef _WIN32 | 65 | #ifdef _WIN32 |
| 59 | static HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE); | 66 | static HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE); |
| 60 | 67 | ||
| 68 | CONSOLE_SCREEN_BUFFER_INFO original_info = {0}; | ||
| 69 | GetConsoleScreenBufferInfo(console_handle, &original_info); | ||
| 70 | |||
| 61 | WORD color = 0; | 71 | WORD color = 0; |
| 62 | switch (level) { | 72 | switch (entry.log_level) { |
| 63 | case Level::Trace: // Grey | 73 | case Level::Trace: // Grey |
| 64 | color = FOREGROUND_INTENSITY; break; | 74 | color = FOREGROUND_INTENSITY; break; |
| 65 | case Level::Debug: // Cyan | 75 | case Level::Debug: // Cyan |
| @@ -76,9 +86,9 @@ static void ChangeConsoleColor(Level level) { | |||
| 76 | 86 | ||
| 77 | SetConsoleTextAttribute(console_handle, color); | 87 | SetConsoleTextAttribute(console_handle, color); |
| 78 | #else | 88 | #else |
| 79 | #define ESC "\x1b" | 89 | # define ESC "\x1b" |
| 80 | const char* color = ""; | 90 | const char* color = ""; |
| 81 | switch (level) { | 91 | switch (entry.log_level) { |
| 82 | case Level::Trace: // Grey | 92 | case Level::Trace: // Grey |
| 83 | color = ESC "[1;30m"; break; | 93 | color = ESC "[1;30m"; break; |
| 84 | case Level::Debug: // Cyan | 94 | case Level::Debug: // Cyan |
| @@ -92,18 +102,18 @@ static void ChangeConsoleColor(Level level) { | |||
| 92 | case Level::Critical: // Bright magenta | 102 | case Level::Critical: // Bright magenta |
| 93 | color = ESC "[1;35m"; break; | 103 | color = ESC "[1;35m"; break; |
| 94 | } | 104 | } |
| 95 | #undef ESC | ||
| 96 | 105 | ||
| 97 | fputs(color, stderr); | 106 | fputs(color, stderr); |
| 98 | #endif | 107 | #endif |
| 99 | } | ||
| 100 | 108 | ||
| 101 | void PrintMessage(const Entry& entry) { | 109 | PrintMessage(entry); |
| 102 | ChangeConsoleColor(entry.log_level); | 110 | |
| 103 | std::array<char, 4 * 1024> format_buffer; | 111 | #ifdef _WIN32 |
| 104 | FormatLogMessage(entry, format_buffer.data(), format_buffer.size()); | 112 | SetConsoleTextAttribute(console_handle, original_info.wAttributes); |
| 105 | fputs(format_buffer.data(), stderr); | 113 | #else |
| 106 | fputc('\n', stderr); | 114 | fputs(ESC "[0m", stderr); |
| 115 | # undef ESC | ||
| 116 | #endif | ||
| 107 | } | 117 | } |
| 108 | 118 | ||
| 109 | void TextLoggingLoop(std::shared_ptr<Logger> logger, const Filter* filter) { | 119 | void TextLoggingLoop(std::shared_ptr<Logger> logger, const Filter* filter) { |
| @@ -117,7 +127,7 @@ void TextLoggingLoop(std::shared_ptr<Logger> logger, const Filter* filter) { | |||
| 117 | for (size_t i = 0; i < num_entries; ++i) { | 127 | for (size_t i = 0; i < num_entries; ++i) { |
| 118 | const Entry& entry = entry_buffer[i]; | 128 | const Entry& entry = entry_buffer[i]; |
| 119 | if (filter->CheckMessage(entry.log_class, entry.log_level)) { | 129 | if (filter->CheckMessage(entry.log_class, entry.log_level)) { |
| 120 | PrintMessage(entry); | 130 | PrintColoredMessage(entry); |
| 121 | } | 131 | } |
| 122 | } | 132 | } |
| 123 | } | 133 | } |