diff --git a/openVulkanoCpp/Base/Logger.cpp b/openVulkanoCpp/Base/Logger.cpp index de3ca6c..55641de 100644 --- a/openVulkanoCpp/Base/Logger.cpp +++ b/openVulkanoCpp/Base/Logger.cpp @@ -7,6 +7,7 @@ #include "Math/ByteSize.hpp" #include "Logger.hpp" #include "IO/AppFolders.hpp" +#include "Utils.hpp" #include #include #include @@ -137,15 +138,18 @@ namespace OpenVulkano if (reg) { auto logger = spdlog::get(name); if (logger) return logger; } auto logger = std::make_shared(name, SINKS.begin(), SINKS.end()); if (reg) spdlog::register_logger(logger); + auto formatter = std::make_unique(); + formatter->add_flag('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 CustomThreadNameFormatterFlag::clone() const + { + return spdlog::details::make_unique(); + } } diff --git a/openVulkanoCpp/Base/Logger.hpp b/openVulkanoCpp/Base/Logger.hpp index fb5b653..ef3b502 100644 --- a/openVulkanoCpp/Base/Logger.hpp +++ b/openVulkanoCpp/Base/Logger.hpp @@ -10,6 +10,8 @@ #define SPDLOG_TRACE_ON #include +#include +#include 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 clone() const override; + }; + } \ No newline at end of file diff --git a/openVulkanoCpp/Base/Utils.cpp b/openVulkanoCpp/Base/Utils.cpp index f67c51e..d452509 100644 --- a/openVulkanoCpp/Base/Utils.cpp +++ b/openVulkanoCpp/Base/Utils.cpp @@ -12,6 +12,9 @@ #include #endif #include +#include +#include +#include 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> 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 Utils::ReadFile(const std::string& filePath, bool emptyOnMissing) { std::ifstream file(filePath, std::ios::ate | std::ios::binary); diff --git a/openVulkanoCpp/Base/Utils.hpp b/openVulkanoCpp/Base/Utils.hpp index 23db9a2..9c803bd 100644 --- a/openVulkanoCpp/Base/Utils.hpp +++ b/openVulkanoCpp/Base/Utils.hpp @@ -19,6 +19,7 @@ namespace OpenVulkano { public: static void SetThreadName(const std::string& name); + static std::string GetThreadName(); static inline std::vector toCString(const std::vector& values) {