diff --git a/src/mbgl/platform/log.cpp b/src/mbgl/platform/log.cpp index 648c3bd18..0f334ae3e 100644 --- a/src/mbgl/platform/log.cpp +++ b/src/mbgl/platform/log.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -49,18 +50,7 @@ void Log::record(EventSeverity severity, Event event, int64_t code, const std::s std::stringstream logStream; -#if defined(__APPLE__) - char name[32]; - pthread_getname_np(pthread_self(), name, sizeof(name)); - logStream << "{" << name << "}"; -#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ) -#if __GLIBC_PREREQ(2, 12) - char name[32]; - pthread_getname_np(pthread_self(), name, sizeof(name)); - logStream << "{" << name << "}"; -#endif -#endif - + logStream << "{" << util::getCurrentThreadName() << "}"; logStream << "[" << Enum::toString(event) << "]"; if (code >= 0) { diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp index c5f9d7338..0de897d72 100644 --- a/src/mbgl/util/thread.hpp +++ b/src/mbgl/util/thread.hpp @@ -87,6 +87,29 @@ private: RunLoop* loop = nullptr; }; +inline std::string getCurrentThreadName() { + char name[32] = "unknown"; +#if defined(__APPLE__) + pthread_getname_np(pthread_self(), name, sizeof(name)); +#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ) +#if __GLIBC_PREREQ(2, 12) + pthread_getname_np(pthread_self(), name, sizeof(name)); +#endif +#endif + return name; +} + +inline void setCurrentThreadName(const std::string& name) { +#if defined(__APPLE__) + pthread_setname_np(name.c_str()); +#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ) +#if __GLIBC_PREREQ(2, 12) + pthread_setname_np(pthread_self(), name.c_str()); +#endif +#endif + (void)name; +} + template template Thread::Thread(const ThreadContext& context, Args&&... args) { @@ -95,13 +118,7 @@ Thread::Thread(const ThreadContext& context, Args&&... args) { std::tuple params = std::forward_as_tuple(::std::forward(args)...); thread = std::thread([&] { -#if defined(__APPLE__) - pthread_setname_np(context.name.c_str()); -#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ) -#if __GLIBC_PREREQ(2, 12) - pthread_setname_np(pthread_self(), context.name.c_str()); -#endif -#endif + setCurrentThreadName(context.name); if (context.priority == ThreadPriority::Low) { platform::makeThreadLowPriority();