diff --git a/myframe/common.cpp b/myframe/common.cpp index 27267bc..42081b7 100644 --- a/myframe/common.cpp +++ b/myframe/common.cpp @@ -194,9 +194,16 @@ int Common::SetSelfThreadAffinity(int cpu_core) { int Common::SetThreadName(std::thread* t, const std::string& name) { #if defined(MYFRAME_OS_WINDOWS) + int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, nullptr, 0); + if (len <= 0) { + return -1; + } + wchar_t* wide_name = new wchar_t[len]; + MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, wide_name, len); auto handle = t->native_handle(); - auto res = SetThreadDescription(handle, name.c_str()); - if (res != 0) { + auto res = SetThreadDescription(handle, wide_name); + delete[] wide_name; + if (FAILED(res)) { return -1; } return 0; @@ -211,9 +218,16 @@ int Common::SetThreadName(std::thread* t, const std::string& name) { int Common::SetSelfThreadName(const std::string& name) { #if defined(MYFRAME_OS_WINDOWS) + int len = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, nullptr, 0); + if (len <= 0) { + return -1; + } + wchar_t* wide_name = new wchar_t[len]; + MultiByteToWideChar(CP_UTF8, 0, name.c_str(), -1, wide_name, len); auto handle = GetCurrentThread(); - auto res = SetThreadDescription(handle, name.c_str()); - if (res != 0) { + auto res = SetThreadDescription(handle, wide_name); + delete[] wide_name; + if (FAILED(res)) { return -1; } return 0; diff --git a/myframe/worker_context.cpp b/myframe/worker_context.cpp index 5b80155..acfd177 100644 --- a/myframe/worker_context.cpp +++ b/myframe/worker_context.cpp @@ -78,7 +78,11 @@ void WorkerContext::Initialize() { std::string th_name = mailbox_.Addr(); th_name = th_name.size() >= 16 ? th_name.substr(0, 15) : th_name; if (Common::SetSelfThreadName(th_name)) { - LOG(WARNING) << "set thread name " << th_name << " failed"; + LOG(WARNING) << "set " << mailbox_.Addr() + << " thread name " << th_name << " failed"; + } else { + LOG(INFO) << "set " << mailbox_.Addr() + << " thread name " << th_name; } worker_->Init(); }