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>();
}
}

View File

@@ -10,6 +10,8 @@
#define SPDLOG_TRACE_ON
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/pattern_formatter.h>
namespace OpenVulkano
{
@@ -48,4 +50,12 @@ namespace OpenVulkano
static void RegisterSink(const spdlog::sink_ptr& newSink);
};
class CustomThreadNameFormatterFlag : public spdlog::custom_flag_formatter
{
public:
void format(const spdlog::details::log_msg &msg, const std::tm &tmTime, spdlog::memory_buf_t &dest) override;
std::unique_ptr<spdlog::custom_flag_formatter> clone() const override;
};
}

View File

@@ -12,6 +12,9 @@
#include <pthread.h>
#endif
#include <fstream>
#include <string>
#include <locale>
#include <codecvt>
namespace OpenVulkano
{
@@ -28,6 +31,26 @@ namespace OpenVulkano
#endif
}
std::string Utils::GetThreadName()
{
#ifdef _MSC_VER
auto thisThread = ::GetCurrentThread();
PWSTR threadName;
GetThreadDescription(thisThread, &threadName);
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::string str = converter.to_bytes(threadName);
LocalFree(threadName);
return str;
#else
// NOTE(vb): Accoring to linux man page size of this array must be _at least_ 16 bytes...
// But I don't know whether is it applicable to have the buffer of bigger size
char threadName[16];
pthread_getname_np(pthread_self(), threadName, sizeof(threadName));
std::string str = threadName;
return str;
#endif
}
Array<char> Utils::ReadFile(const std::string& filePath, bool emptyOnMissing)
{
std::ifstream file(filePath, std::ios::ate | std::ios::binary);

View File

@@ -19,6 +19,7 @@ namespace OpenVulkano
{
public:
static void SetThreadName(const std::string& name);
static std::string GetThreadName();
static inline std::vector<const char*> toCString(const std::vector<std::string>& values)
{