Merge pull request 'Custom formatter for flag 't', new Utils::GetThreadName()' (#26) from thread_names_instead_of_ids into master

Reviewed-on: https://git.madvoxel.net/OpenVulkano/OpenVulkano/pulls/26
Reviewed-by: Georg Hagen <georg.hagen@madvoxel.com>
This commit is contained in:
Vladyslav_Baranovskyi_EXT
2024-05-30 19:19:51 +02:00
4 changed files with 72 additions and 3 deletions

View File

@@ -7,9 +7,11 @@
#include "Math/ByteSize.hpp"
#include "Logger.hpp"
#include "IO/AppFolders.hpp"
#include "Utils.hpp"
#include <iostream>
#include <filesystem>
#include <csignal>
#include <inttypes.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/null_sink.h>
#ifndef NO_CONSOLE_LOG
@@ -137,15 +139,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 +179,25 @@ 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();
if(threadName.length())
{
dest.append(threadName.c_str(), threadName.c_str() + threadName.length());
}
else
{
uint64_t threadId = OpenVulkano::Utils::GetThreadId();
char buffer[64];
snprintf(buffer, sizeof(buffer), "%" PRIu64, threadId);
dest.append(buffer, buffer + strlen(buffer));
}
}
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,34 @@ 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...
char threadName[64];
pthread_getname_np(pthread_self(), threadName, sizeof(threadName));
std::string str = threadName;
return str;
#endif
}
uint64_t Utils::GetThreadId()
{
#ifdef _MSC_VER
return (uint64_t)::GetThreadId(::GetCurrentThread());
#else
return (uint64_t)pthread_self();
#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,8 @@ namespace OpenVulkano
{
public:
static void SetThreadName(const std::string& name);
static std::string GetThreadName();
static uint64_t GetThreadId();
static inline std::vector<const char*> toCString(const std::vector<std::string>& values)
{