Custom formatter for flag 't', new Utils::GetThreadName()

This commit is contained in:
Vladyslav Baranovskyi
2024-05-30 16:30:09 +03:00
parent 2df5706df4
commit 3127af5bc7
4 changed files with 53 additions and 3 deletions

View File

@@ -7,6 +7,7 @@
#include "Math/ByteSize.hpp"
#include "Logger.hpp"
#include "IO/AppFolders.hpp"
#include "Utils.hpp"
#include <iostream>
#include <filesystem>
#include <csignal>
@@ -137,15 +138,18 @@ namespace OpenVulkano
if (reg) { auto logger = spdlog::get(name); if (logger) return logger; }
auto logger = std::make_shared<spdlog::logger>(name, SINKS.begin(), SINKS.end());
if (reg) spdlog::register_logger(logger);
auto formatter = std::make_unique<spdlog::pattern_formatter>();
formatter->add_flag<CustomThreadNameFormatterFlag>('t'); // overloading t
#ifdef LOG_DATE
logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [T%t] [%^%l%$] [%n]: %v");
formatter->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [T:%t] [%^%l%$] [%n]: %v");
#else
logger->set_pattern("[%H:%M:%S.%e] [T%t] [%^%l%$] [%n]: %v");
formatter->set_pattern("[%H:%M:%S.%e] [T:%t] [%^%l%$] [%n]: %v");
#endif
#ifdef DEBUG
logger->set_level(spdlog::level::debug);
#endif
logger->set_formatter(std::move(formatter));
logger->flush_on(spdlog::level::err); // Flush on error
LOGGERS.push_back(logger);
@@ -174,4 +178,16 @@ namespace OpenVulkano
}
SINKS.push_back(newSink);
}
void CustomThreadNameFormatterFlag::format(const spdlog::details::log_msg &msg, const std::tm &tmTime, spdlog::memory_buf_t &dest)
{
std::string threadName = OpenVulkano::Utils::GetThreadName();
// NOTE(vb): What should be displayed if the thread name wasn't specified?
dest.append(threadName.c_str(), threadName.c_str() + threadName.length());
}
std::unique_ptr<spdlog::custom_flag_formatter> CustomThreadNameFormatterFlag::clone() const
{
return spdlog::details::make_unique<CustomThreadNameFormatterFlag>();
}
}