diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/logging/text_formatter.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp index 01c355bb6..b603ead13 100644 --- a/src/common/logging/text_formatter.cpp +++ b/src/common/logging/text_formatter.cpp | |||
| @@ -5,6 +5,11 @@ | |||
| 5 | #include <array> | 5 | #include <array> |
| 6 | #include <cstdio> | 6 | #include <cstdio> |
| 7 | 7 | ||
| 8 | #ifdef _WIN32 | ||
| 9 | # define WIN32_LEAN_AND_MEAN | ||
| 10 | # include <Windows.h> | ||
| 11 | #endif | ||
| 12 | |||
| 8 | #include "common/logging/backend.h" | 13 | #include "common/logging/backend.h" |
| 9 | #include "common/logging/log.h" | 14 | #include "common/logging/log.h" |
| 10 | #include "common/logging/text_formatter.h" | 15 | #include "common/logging/text_formatter.h" |
| @@ -23,7 +28,52 @@ void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len) { | |||
| 23 | entry.location.c_str(), entry.message.c_str()); | 28 | entry.location.c_str(), entry.message.c_str()); |
| 24 | } | 29 | } |
| 25 | 30 | ||
| 31 | static void ChangeConsoleColor(Level level) { | ||
| 32 | #ifdef _WIN32 | ||
| 33 | static HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE); | ||
| 34 | |||
| 35 | WORD color = 0; | ||
| 36 | switch (level) { | ||
| 37 | case Level::Trace: // Grey | ||
| 38 | color = FOREGROUND_INTENSITY; break; | ||
| 39 | case Level::Debug: // Cyan | ||
| 40 | color = FOREGROUND_GREEN | FOREGROUND_BLUE; break; | ||
| 41 | case Level::Info: // Bright gray | ||
| 42 | color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break; | ||
| 43 | case Level::Warning: // Bright yellow | ||
| 44 | color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; | ||
| 45 | case Level::Error: // Bright red | ||
| 46 | color = FOREGROUND_RED | FOREGROUND_INTENSITY; break; | ||
| 47 | case Level::Critical: // Bright magenta | ||
| 48 | color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; | ||
| 49 | } | ||
| 50 | |||
| 51 | SetConsoleTextAttribute(console_handle, color); | ||
| 52 | #else | ||
| 53 | #define ESC "\x1b" | ||
| 54 | const char* color = ""; | ||
| 55 | switch (level) { | ||
| 56 | case Level::Trace: // Grey | ||
| 57 | color = ESC "[1;30m"; break; | ||
| 58 | case Level::Debug: // Cyan | ||
| 59 | color = ESC "[0;36m"; break; | ||
| 60 | case Level::Info: // Bright gray | ||
| 61 | color = ESC "[0;37m"; break; | ||
| 62 | case Level::Warning: // Bright yellow | ||
| 63 | color = ESC "[1;33m"; break; | ||
| 64 | case Level::Error: // Bright red | ||
| 65 | color = ESC "[1;31m"; break; | ||
| 66 | case Level::Critical: // Bright magenta | ||
| 67 | color = ESC "[1;35m"; break; | ||
| 68 | } | ||
| 69 | #undef ESC | ||
| 70 | |||
| 71 | fputs(color, stderr); | ||
| 72 | #endif | ||
| 73 | } | ||
| 74 | |||
| 26 | void PrintMessage(const Entry& entry) { | 75 | void PrintMessage(const Entry& entry) { |
| 76 | ChangeConsoleColor(entry.log_level); | ||
| 27 | std::array<char, 4 * 1024> format_buffer; | 77 | std::array<char, 4 * 1024> format_buffer; |
| 28 | FormatLogMessage(entry, format_buffer.data(), format_buffer.size()); | 78 | FormatLogMessage(entry, format_buffer.data(), format_buffer.size()); |
| 29 | fputs(format_buffer.data(), stderr); | 79 | fputs(format_buffer.data(), stderr); |