diff options
| author | 2014-12-29 19:47:41 -0800 | |
|---|---|---|
| committer | 2014-12-29 19:47:41 -0800 | |
| commit | 8ba9ac0f74abb0408a26207a76a0c1808bad8de0 (patch) | |
| tree | f1c7c3393fa726435b5b90bf335567c93e528ef1 /src/common/logging/text_formatter.cpp | |
| parent | Add comment regarding __WIN32__ in SkyEye code (diff) | |
| parent | Merge pull request #367 from bunnei/usat_ssat (diff) | |
| download | yuzu-8ba9ac0f74abb0408a26207a76a0c1808bad8de0.tar.gz yuzu-8ba9ac0f74abb0408a26207a76a0c1808bad8de0.tar.xz yuzu-8ba9ac0f74abb0408a26207a76a0c1808bad8de0.zip | |
Fix merge conflicts
Diffstat (limited to 'src/common/logging/text_formatter.cpp')
| -rw-r--r-- | src/common/logging/text_formatter.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp new file mode 100644 index 000000000..ef5739d84 --- /dev/null +++ b/src/common/logging/text_formatter.cpp | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | // Copyright 2014 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <cstdio> | ||
| 7 | |||
| 8 | #ifdef _WIN32 | ||
| 9 | # define WIN32_LEAN_AND_MEAN | ||
| 10 | # include <Windows.h> | ||
| 11 | #endif | ||
| 12 | |||
| 13 | #include "common/logging/backend.h" | ||
| 14 | #include "common/logging/filter.h" | ||
| 15 | #include "common/logging/log.h" | ||
| 16 | #include "common/logging/text_formatter.h" | ||
| 17 | |||
| 18 | #include "common/string_util.h" | ||
| 19 | |||
| 20 | namespace Log { | ||
| 21 | |||
| 22 | // TODO(bunnei): This should be moved to a generic path manipulation library | ||
| 23 | const char* TrimSourcePath(const char* path, const char* root) { | ||
| 24 | const char* p = path; | ||
| 25 | |||
| 26 | while (*p != '\0') { | ||
| 27 | const char* next_slash = p; | ||
| 28 | while (*next_slash != '\0' && *next_slash != '/' && *next_slash != '\\') { | ||
| 29 | ++next_slash; | ||
| 30 | } | ||
| 31 | |||
| 32 | bool is_src = Common::ComparePartialString(p, next_slash, root); | ||
| 33 | p = next_slash; | ||
| 34 | |||
| 35 | if (*p != '\0') { | ||
| 36 | ++p; | ||
| 37 | } | ||
| 38 | if (is_src) { | ||
| 39 | path = p; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | return path; | ||
| 43 | } | ||
| 44 | |||
| 45 | void FormatLogMessage(const Entry& entry, char* out_text, size_t text_len) { | ||
| 46 | unsigned int time_seconds = static_cast<unsigned int>(entry.timestamp.count() / 1000000); | ||
| 47 | unsigned int time_fractional = static_cast<unsigned int>(entry.timestamp.count() % 1000000); | ||
| 48 | |||
| 49 | const char* class_name = Logger::GetLogClassName(entry.log_class); | ||
| 50 | const char* level_name = Logger::GetLevelName(entry.log_level); | ||
| 51 | |||
| 52 | snprintf(out_text, text_len, "[%4u.%06u] %s <%s> %s: %s", | ||
| 53 | time_seconds, time_fractional, class_name, level_name, | ||
| 54 | TrimSourcePath(entry.location.c_str()), entry.message.c_str()); | ||
| 55 | } | ||
| 56 | |||
| 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) { | ||
| 65 | #ifdef _WIN32 | ||
| 66 | static HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE); | ||
| 67 | |||
| 68 | CONSOLE_SCREEN_BUFFER_INFO original_info = {0}; | ||
| 69 | GetConsoleScreenBufferInfo(console_handle, &original_info); | ||
| 70 | |||
| 71 | WORD color = 0; | ||
| 72 | switch (entry.log_level) { | ||
| 73 | case Level::Trace: // Grey | ||
| 74 | color = FOREGROUND_INTENSITY; break; | ||
| 75 | case Level::Debug: // Cyan | ||
| 76 | color = FOREGROUND_GREEN | FOREGROUND_BLUE; break; | ||
| 77 | case Level::Info: // Bright gray | ||
| 78 | color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break; | ||
| 79 | case Level::Warning: // Bright yellow | ||
| 80 | color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; | ||
| 81 | case Level::Error: // Bright red | ||
| 82 | color = FOREGROUND_RED | FOREGROUND_INTENSITY; break; | ||
| 83 | case Level::Critical: // Bright magenta | ||
| 84 | color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; | ||
| 85 | } | ||
| 86 | |||
| 87 | SetConsoleTextAttribute(console_handle, color); | ||
| 88 | #else | ||
| 89 | # define ESC "\x1b" | ||
| 90 | const char* color = ""; | ||
| 91 | switch (entry.log_level) { | ||
| 92 | case Level::Trace: // Grey | ||
| 93 | color = ESC "[1;30m"; break; | ||
| 94 | case Level::Debug: // Cyan | ||
| 95 | color = ESC "[0;36m"; break; | ||
| 96 | case Level::Info: // Bright gray | ||
| 97 | color = ESC "[0;37m"; break; | ||
| 98 | case Level::Warning: // Bright yellow | ||
| 99 | color = ESC "[1;33m"; break; | ||
| 100 | case Level::Error: // Bright red | ||
| 101 | color = ESC "[1;31m"; break; | ||
| 102 | case Level::Critical: // Bright magenta | ||
| 103 | color = ESC "[1;35m"; break; | ||
| 104 | } | ||
| 105 | |||
| 106 | fputs(color, stderr); | ||
| 107 | #endif | ||
| 108 | |||
| 109 | PrintMessage(entry); | ||
| 110 | |||
| 111 | #ifdef _WIN32 | ||
| 112 | SetConsoleTextAttribute(console_handle, original_info.wAttributes); | ||
| 113 | #else | ||
| 114 | fputs(ESC "[0m", stderr); | ||
| 115 | # undef ESC | ||
| 116 | #endif | ||
| 117 | } | ||
| 118 | |||
| 119 | void TextLoggingLoop(std::shared_ptr<Logger> logger, const Filter* filter) { | ||
| 120 | std::array<Entry, 256> entry_buffer; | ||
| 121 | |||
| 122 | while (true) { | ||
| 123 | size_t num_entries = logger->GetEntries(entry_buffer.data(), entry_buffer.size()); | ||
| 124 | if (num_entries == Logger::QUEUE_CLOSED) { | ||
| 125 | break; | ||
| 126 | } | ||
| 127 | for (size_t i = 0; i < num_entries; ++i) { | ||
| 128 | const Entry& entry = entry_buffer[i]; | ||
| 129 | if (filter->CheckMessage(entry.log_class, entry.log_level)) { | ||
| 130 | PrintColoredMessage(entry); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | } | ||