summaryrefslogtreecommitdiff
path: root/src/common/logging/text_formatter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/logging/text_formatter.cpp')
-rw-r--r--src/common/logging/text_formatter.cpp126
1 files changed, 126 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..3fe435346
--- /dev/null
+++ b/src/common/logging/text_formatter.cpp
@@ -0,0 +1,126 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2+
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
20namespace Log {
21
22// TODO(bunnei): This should be moved to a generic path manipulation library
23const 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
45void 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
57static void ChangeConsoleColor(Level level) {
58#ifdef _WIN32
59 static HANDLE console_handle = GetStdHandle(STD_ERROR_HANDLE);
60
61 WORD color = 0;
62 switch (level) {
63 case Level::Trace: // Grey
64 color = FOREGROUND_INTENSITY; break;
65 case Level::Debug: // Cyan
66 color = FOREGROUND_GREEN | FOREGROUND_BLUE; break;
67 case Level::Info: // Bright gray
68 color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
69 case Level::Warning: // Bright yellow
70 color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break;
71 case Level::Error: // Bright red
72 color = FOREGROUND_RED | FOREGROUND_INTENSITY; break;
73 case Level::Critical: // Bright magenta
74 color = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
75 }
76
77 SetConsoleTextAttribute(console_handle, color);
78#else
79#define ESC "\x1b"
80 const char* color = "";
81 switch (level) {
82 case Level::Trace: // Grey
83 color = ESC "[1;30m"; break;
84 case Level::Debug: // Cyan
85 color = ESC "[0;36m"; break;
86 case Level::Info: // Bright gray
87 color = ESC "[0;37m"; break;
88 case Level::Warning: // Bright yellow
89 color = ESC "[1;33m"; break;
90 case Level::Error: // Bright red
91 color = ESC "[1;31m"; break;
92 case Level::Critical: // Bright magenta
93 color = ESC "[1;35m"; break;
94 }
95#undef ESC
96
97 fputs(color, stderr);
98#endif
99}
100
101void PrintMessage(const Entry& entry) {
102 ChangeConsoleColor(entry.log_level);
103 std::array<char, 4 * 1024> format_buffer;
104 FormatLogMessage(entry, format_buffer.data(), format_buffer.size());
105 fputs(format_buffer.data(), stderr);
106 fputc('\n', stderr);
107}
108
109void TextLoggingLoop(std::shared_ptr<Logger> logger, const Filter* filter) {
110 std::array<Entry, 256> entry_buffer;
111
112 while (true) {
113 size_t num_entries = logger->GetEntries(entry_buffer.data(), entry_buffer.size());
114 if (num_entries == Logger::QUEUE_CLOSED) {
115 break;
116 }
117 for (size_t i = 0; i < num_entries; ++i) {
118 const Entry& entry = entry_buffer[i];
119 if (filter->CheckMessage(entry.log_class, entry.log_level)) {
120 PrintMessage(entry);
121 }
122 }
123 }
124}
125
126}