2#include <spdlog/sinks/stdout_color_sinks.h>
9 std::mutex s_CoreTagMutex;
10 std::mutex s_ClientTagMutex;
15 std::shared_ptr<spdlog::logger> Logger::s_CoreLogger;
16 std::shared_ptr<spdlog::logger> Logger::s_ClientLogger;
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;
21 bool Logger::s_Initialized =
false;
24#ifdef PLATFORM_WINDOWS
25 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
26 if (hOut == INVALID_HANDLE_VALUE) {
31 if (!GetConsoleMode(hOut, &dwMode)) {
35 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
37 if (!SetConsoleMode(hOut, dwMode)) {
45 spdlog::init_thread_pool(8192, 1);
48 int32_t maxSize = 1048576 * 20;
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;
56 coreSinks.push_back(fileSink);
58 clientSinks.push_back(fileSink);
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%$ %@");
65 coreSinks.push_back(consoleSink);
66 clientSinks.push_back(consoleSink);
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);
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());
78 spdlog::register_logger(s_CoreLogger);
79 s_CoreLogger->set_level(spdlog::level::trace);
80 s_CoreLogger->flush_on(spdlog::level::warn);
82 spdlog::register_logger(s_ClientLogger);
83 s_ClientLogger->set_level(spdlog::level::trace);
84 s_ClientLogger->flush_on(spdlog::level::warn);
103 s_ClientLogger->set_level(spdlog::level::trace);
107 s_ClientLogger->set_level(spdlog::level::debug);
111 s_ClientLogger->set_level(spdlog::level::info);
115 s_ClientLogger->set_level(spdlog::level::warn);
119 s_ClientLogger->set_level(spdlog::level::err);
123 s_ClientLogger->set_level(spdlog::level::critical);
132 s_CoreLogger->set_level(spdlog::level::trace);
136 s_CoreLogger->set_level(spdlog::level::debug);
140 s_CoreLogger->set_level(spdlog::level::info);
144 s_CoreLogger->set_level(spdlog::level::warn);
148 s_CoreLogger->set_level(spdlog::level::err);
152 s_CoreLogger->set_level(spdlog::level::critical);
158 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
159 s_CoreInactiveTags.erase(std::string(tag));
163 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
164 for (
const char* tag : tags) {
165 s_CoreInactiveTags.erase(tag);
170 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
171 s_CoreInactiveTags.insert(std::string(tag));
175 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
176 for (
const char* tag : tags) {
177 s_CoreInactiveTags.insert(tag);
182 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
183 return s_CoreInactiveTags.contains(tag);
187 std::lock_guard<std::mutex> lock(s_CoreTagMutex);
188 s_CoreInactiveTags.clear();
193 std::vector<std::string> result;
194 result.reserve(s_CoreInactiveTags.size());
195 result.insert(result.end(), s_CoreInactiveTags.begin(), s_CoreInactiveTags.end());
200 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
201 s_ClientInactiveTags.erase(std::string(tag));
205 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
206 for (
const char* tag : tags) {
207 s_ClientInactiveTags.erase(tag);
212 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
213 s_ClientInactiveTags.insert(std::string(tag));
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);
226 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
227 return s_ClientInactiveTags.contains(tag);
231 std::lock_guard<std::mutex> lock(s_ClientTagMutex);
232 s_ClientInactiveTags.clear();
237 std::vector<std::string> result;
238 result.reserve(s_ClientInactiveTags.size());
239 result.insert(result.end(), s_ClientInactiveTags.begin(), s_ClientInactiveTags.end());
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()) {
248 for (
const char* tag : tags) {
249 if (s_CoreInactiveTags.contains(tag)) {
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()) {
261 for (
const char* tag : tags) {
262 if (s_ClientInactiveTags.contains(tag)) {
#define CORI_CORE_INFO_TAGGED(...)
static void EnableClientTag(const char *tag)
Enables the logging of a specific client tag.
static void EnableClientTags(const std::initializer_list< const char * > tags)
Same as EnableClientTag but enables multiple tags at once.
static void ClearCoreTagFilter()
Clears the core tag filter, all tags that were disabled become enabled again as a result.
static bool IsClientTagDisabled(const char *tag)
Checks if a specific client tag is enabled.
static void SetClientLogLevel(LogLevel level)
static void EnableCoreTags(const std::initializer_list< const char * > tags)
Same as EnableCoreTag but enables multiple tags at once.
static void DisableCoreTags(const std::initializer_list< const char * > tags)
Same as DisableCoreTag but disables multiple tags at once.
static void DisableCoreTag(const char *tag)
Disables the logging of a specific core tag.
static std::vector< std::string > GetClientInactiveTags()
Gies a list of all currently disabled client tags.
static void SetCoreLogLevel(LogLevel level)
static std::vector< std::string > GetCoreInactiveTags()
Gies a list of all currently disabled core tags.
static void ClearClientTagFilter()
Clears the client tag filter, all tags that were disabled become enabled again as a result.
static void EnableVirtualTerminalProcessing()
static void Init(bool async, bool fileWrite)
static void DisableClientTag(const char *tag)
Disables the logging of a specific core tag.
static void EnableCoreTag(const char *tag)
Enables the logging of a specific core tag.
static bool IsCoreTagDisabled(const char *tag)
Checks if a specific core tag is enabled.
static void DisableClientTags(const std::initializer_list< const char * > tags)
Same as DisableClientTag but disables multiple tags at once.