CoriEngine
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1#include "Logger.hpp"
2#include <spdlog/sinks/stdout_color_sinks.h>
3
4#ifdef PLATFORM_WINDOWS
5#include <windows.h>
6#endif
7
8namespace {
9 std::mutex s_CoreTagMutex;
10 std::mutex s_ClientTagMutex;
11}
12
13namespace Cori {
14
15 std::shared_ptr<spdlog::logger> Logger::s_CoreLogger;
16 std::shared_ptr<spdlog::logger> Logger::s_ClientLogger;
17
18 std::unordered_set<std::string, Logger::StringHash, std::equal_to<>> Logger::s_CoreInactiveTags;
19 std::unordered_set<std::string, Logger::StringHash, std::equal_to<>> Logger::s_ClientInactiveTags;
20
21 bool Logger::s_Initialized = false;
22
24#ifdef PLATFORM_WINDOWS
25 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
26 if (hOut == INVALID_HANDLE_VALUE) {
27 return;
28 }
29
30 DWORD dwMode = 0;
31 if (!GetConsoleMode(hOut, &dwMode)) {
32 return;
33 }
34
35 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
36
37 if (!SetConsoleMode(hOut, dwMode)) {
38 return;
39 }
40#endif
41 }
42
43 void Logger::Init(const bool async, const bool fileWrite) {
44 if (async) {
45 spdlog::init_thread_pool(8192, 1);
46 }
47
48 int32_t maxSize = 1048576 * 20;
49 int32_t maxFiles = 5;
50 const auto fileSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("logs/cori_log.txt", maxSize, maxFiles);
51 fileSink->set_pattern("%^[%Y-%m-%d %H:%M:%S.%e] [Thread %t] [%-6n] [%-8l]: %v%$ %@");
52 std::vector<spdlog::sink_ptr> coreSinks;
53 std::vector<spdlog::sink_ptr> clientSinks;
54
55 if (fileWrite) {
56 coreSinks.push_back(fileSink);
57
58 clientSinks.push_back(fileSink);
59 }
60
61#ifdef DEBUG_BUILD
62 const auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
63 consoleSink->set_pattern("%^[%Y-%m-%d %H:%M:%S.%e] [Thread %t] [%-6n] [%-8l]: %v%$ %@");
64
65 coreSinks.push_back(consoleSink);
66 clientSinks.push_back(consoleSink);
67#endif
68
69 if (async) {
70 s_CoreLogger = std::make_shared<spdlog::async_logger>("ENGINE", coreSinks.begin(), coreSinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
71 s_ClientLogger = std::make_shared<spdlog::async_logger>("APP", clientSinks.begin(), clientSinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
72 }
73 else {
74 s_CoreLogger = std::make_shared<spdlog::logger>("ENGINE", coreSinks.begin(), coreSinks.end());
75 s_ClientLogger = std::make_shared<spdlog::logger>("APP", clientSinks.begin(), clientSinks.end());
76 }
77
78 spdlog::register_logger(s_CoreLogger);
79 s_CoreLogger->set_level(spdlog::level::trace);
80 s_CoreLogger->flush_on(spdlog::level::warn);
81
82 spdlog::register_logger(s_ClientLogger);
83 s_ClientLogger->set_level(spdlog::level::trace);
84 s_ClientLogger->flush_on(spdlog::level::warn);
85
86 s_Initialized = true;
87
88 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "------------- NEW LOG SESSION -------------");
89 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "| Logger initialized. Mode: {} |", async ? "Asynchronous" : "Synchronous ");
90 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "-------------------------------------------");
91 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "| File logging is: {} |", fileWrite ? "Enabled " : "Disabled");
92 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "-------------------------------------------");
93 }
94
96 return s_Initialized;
97 }
98
100 switch (level) {
102 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Client log level is set to TRACE");
103 s_ClientLogger->set_level(spdlog::level::trace);
104 break;
106 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Client log level is set to DEBUG");
107 s_ClientLogger->set_level(spdlog::level::debug);
108 break;
110 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Client log level is set to INFO");
111 s_ClientLogger->set_level(spdlog::level::info);
112 break;
114 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Client log level is set to WARN");
115 s_ClientLogger->set_level(spdlog::level::warn);
116 break;
118 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Client log level is set to ERROR");
119 s_ClientLogger->set_level(spdlog::level::err);
120 break;
122 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Client log level is set to FATAL");
123 s_ClientLogger->set_level(spdlog::level::critical);
124 break;
125 }
126 }
127
129 switch (level) {
131 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Core log level is set to TRACE");
132 s_CoreLogger->set_level(spdlog::level::trace);
133 break;
135 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Core log level is set to DEBUG");
136 s_CoreLogger->set_level(spdlog::level::debug);
137 break;
139 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Core log level is set to INFO");
140 s_CoreLogger->set_level(spdlog::level::info);
141 break;
143 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Core log level is set to WARN");
144 s_CoreLogger->set_level(spdlog::level::warn);
145 break;
147 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Core log level is set to ERROR");
148 s_CoreLogger->set_level(spdlog::level::err);
149 break;
151 CORI_CORE_INFO_TAGGED({ Tags::Core::Self, Tags::Core::Logger }, "Core log level is set to FATAL");
152 s_CoreLogger->set_level(spdlog::level::critical);
153 break;
154 }
155 }
156
157 void Logger::EnableCoreTag(const char* tag) {
158 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
159 s_CoreInactiveTags.erase(std::string(tag));
160 }
161
162 void Logger::EnableCoreTags(const std::initializer_list<const char*> tags) {
163 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
164 for (const char* tag : tags) {
165 s_CoreInactiveTags.erase(tag);
166 }
167 }
168
169 void Logger::DisableCoreTag(const char* tag) {
170 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
171 s_CoreInactiveTags.insert(std::string(tag));
172 }
173
174 void Logger::DisableCoreTags(const std::initializer_list<const char*> tags) {
175 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
176 for (const char* tag : tags) {
177 s_CoreInactiveTags.insert(tag);
178 }
179 }
180
181 bool Logger::IsCoreTagDisabled(const char* tag) {
182 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
183 return s_CoreInactiveTags.contains(tag);
184 }
185
187 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
188 s_CoreInactiveTags.clear();
189 }
190
191
192 std::vector<std::string> Logger::GetCoreInactiveTags() {
193 std::vector<std::string> result;
194 result.reserve(s_CoreInactiveTags.size());
195 result.insert(result.end(), s_CoreInactiveTags.begin(), s_CoreInactiveTags.end());
196 return result;
197 }
198
199 void Logger::EnableClientTag(const char* tag) {
200 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
201 s_ClientInactiveTags.erase(std::string(tag));
202 }
203
204 void Logger::EnableClientTags(const std::initializer_list<const char*> tags) {
205 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
206 for (const char* tag : tags) {
207 s_ClientInactiveTags.erase(tag);
208 }
209 }
210
211 void Logger::DisableClientTag(const char* tag) {
212 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
213 s_ClientInactiveTags.insert(std::string(tag));
214 }
215
216 void Logger::DisableClientTags(const std::initializer_list<const char*> tags) {
217 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
218 if (!s_ClientInactiveTags.empty()) {
219 for (const char* tag : tags) {
220 s_ClientInactiveTags.insert(tag);
221 }
222 }
223 }
224
225 bool Logger::IsClientTagDisabled(const char* tag) {
226 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
227 return s_ClientInactiveTags.contains(tag);
228 }
229
231 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
232 s_ClientInactiveTags.clear();
233 }
234
235
236 std::vector<std::string> Logger::GetClientInactiveTags() {
237 std::vector<std::string> result;
238 result.reserve(s_ClientInactiveTags.size());
239 result.insert(result.end(), s_ClientInactiveTags.begin(), s_ClientInactiveTags.end());
240 return result;
241 }
242
243 bool Logger::ShouldCoreLog(const std::initializer_list<const char*> tags) {
244 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
245 if (s_CoreInactiveTags.empty()) {
246 return true;
247 }
248 for (const char* tag : tags) {
249 if (s_CoreInactiveTags.contains(tag)) {
250 return false;
251 }
252 }
253 return true;
254 }
255
256 bool Logger::ShouldClientLog(const std::initializer_list<const char*> tags) {
257 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
258 if (s_ClientInactiveTags.empty()) {
259 return true;
260 }
261 for (const char* tag : tags) {
262 if (s_ClientInactiveTags.contains(tag)) {
263 return false;
264 }
265 }
266 return true;
267 }
268
269}
#define CORI_CORE_INFO_TAGGED(...)
Definition Logger.hpp:1027
static void EnableClientTag(const char *tag)
Enables the logging of a specific client tag.
Definition Logger.cpp:199
static void EnableClientTags(const std::initializer_list< const char * > tags)
Same as EnableClientTag but enables multiple tags at once.
Definition Logger.cpp:204
static void ClearCoreTagFilter()
Clears the core tag filter, all tags that were disabled become enabled again as a result.
Definition Logger.cpp:186
static bool IsClientTagDisabled(const char *tag)
Checks if a specific client tag is enabled.
Definition Logger.cpp:225
static void SetClientLogLevel(LogLevel level)
Definition Logger.cpp:99
static void EnableCoreTags(const std::initializer_list< const char * > tags)
Same as EnableCoreTag but enables multiple tags at once.
Definition Logger.cpp:162
static void DisableCoreTags(const std::initializer_list< const char * > tags)
Same as DisableCoreTag but disables multiple tags at once.
Definition Logger.cpp:174
static void DisableCoreTag(const char *tag)
Disables the logging of a specific core tag.
Definition Logger.cpp:169
static bool GetStatus()
Definition Logger.cpp:95
static std::vector< std::string > GetClientInactiveTags()
Gies a list of all currently disabled client tags.
Definition Logger.cpp:236
static void SetCoreLogLevel(LogLevel level)
Definition Logger.cpp:128
static std::vector< std::string > GetCoreInactiveTags()
Gies a list of all currently disabled core tags.
Definition Logger.cpp:192
static void ClearClientTagFilter()
Clears the client tag filter, all tags that were disabled become enabled again as a result.
Definition Logger.cpp:230
static void EnableVirtualTerminalProcessing()
Definition Logger.cpp:23
static void Init(bool async, bool fileWrite)
Definition Logger.cpp:43
static void DisableClientTag(const char *tag)
Disables the logging of a specific core tag.
Definition Logger.cpp:211
static void EnableCoreTag(const char *tag)
Enables the logging of a specific core tag.
Definition Logger.cpp:157
static bool IsCoreTagDisabled(const char *tag)
Checks if a specific core tag is enabled.
Definition Logger.cpp:181
static void DisableClientTags(const std::initializer_list< const char * > tags)
Same as DisableClientTag but disables multiple tags at once.
Definition Logger.cpp:216
Global engine namespace.
static constexpr char Self[]
Definition Logger.hpp:83
static constexpr char Logger[]
Definition Logger.hpp:93