impr: Fallback to old thread name API when new one isn't available

This commit is contained in:
WerWolv 2024-01-09 13:43:34 +01:00
parent 179a65ed8b
commit 929b5176ce
2 changed files with 32 additions and 3 deletions

View File

@ -367,8 +367,37 @@ namespace hex {
void TaskManager::setCurrentThreadName(const std::string &name) {
#if defined(OS_WINDOWS)
auto longName = hex::utf8ToUtf16(name);
::SetThreadDescription(::GetCurrentThread(), longName.c_str());
using SetThreadDescriptionFunc = HRESULT(WINAPI*)(HANDLE hThread, PCWSTR lpThreadDescription);
static auto setThreadDescription = reinterpret_cast<SetThreadDescriptionFunc>(
reinterpret_cast<uintptr_t>(
::GetProcAddress(
::GetModuleHandle("Kernel32.dll"),
"SetThreadDescription"
)
)
);
if (setThreadDescription != nullptr) {
const auto longName = hex::utf8ToUtf16(name);
setThreadDescription(::GetCurrentThread(), longName.c_str());
} else {
struct THREADNAME_INFO {
DWORD dwType;
LPCSTR szName;
DWORD dwThreadID;
DWORD dwFlags;
};
THREADNAME_INFO info = { };
info.dwType = 0x1000;
info.szName = name.c_str();
info.dwThreadID = ::GetCurrentThreadId();
info.dwFlags = 0;
constexpr static DWORD MS_VC_EXCEPTION = 0x406D1388;
RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), reinterpret_cast<ULONG_PTR*>(&info));
}
#elif defined(OS_LINUX)
pthread_setname_np(pthread_self(), name.c_str());
#elif defined(OS_WEB)

View File

@ -75,7 +75,7 @@ namespace hex::log::impl {
std::string projectThreadTag = projectName;
if (auto threadName = TaskManager::getCurrentThreadName(); !threadName.empty())
projectThreadTag += fmt::format("|{0}", threadName);
projectThreadTag += fmt::format(" | {0}", threadName);
constexpr static auto MaxTagLength = 25;
if (projectThreadTag.length() > MaxTagLength)