From 450a11f3b88cb96a214781b0c78d27936c92734c Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Sun, 10 Feb 2019 04:13:18 +0300 Subject: [PATCH] add custom thread id type --- include/spdlog/details/log_msg.h | 2 +- include/spdlog/details/os.h | 31 +++++++++++++++------- include/spdlog/details/pattern_formatter.h | 8 ++++++ include/spdlog/tweakme.h | 20 +++++++++++++- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h index 69422ba3..3c7d9a23 100644 --- a/include/spdlog/details/log_msg.h +++ b/include/spdlog/details/log_msg.h @@ -41,7 +41,7 @@ struct log_msg const std::string *logger_name{nullptr}; level::level_enum level{level::off}; log_clock::time_point time; - size_t thread_id{0}; + details::os::thread_t thread_id{}; size_t msg_id{0}; // wrapping the formatted text with color (updated by pattern_formatter). diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 646805e6..8ad1f674 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -310,38 +310,49 @@ inline int utc_minutes_offset(const std::tm &tm = details::os::localtime()) #endif } -// Return current thread id as size_t +#if defined(SPDLOG_CUSTOM_THREAD_T) +using thread_t = SPDLOG_CUSTOM_THREAD_T; +#else +using thread_t = size_t; +#endif + +// Return current thread id as thread_t // It exists because the std::this_thread::get_id() is much slower(especially // under VS 2013) -inline size_t _thread_id() SPDLOG_NOEXCEPT +inline thread_t _thread_id() SPDLOG_NOEXCEPT { +#if defined(SPDLOG_CUSTOM_THREAD_ID_GETTER) + return SPDLOG_CUSTOM_THREAD_ID_GETTER; +#else #ifdef _WIN32 - return static_cast(::GetCurrentThreadId()); + return static_cast(::GetCurrentThreadId()); #elif __linux__ + #if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21) #define SYS_gettid __NR_gettid #endif - return static_cast(syscall(SYS_gettid)); + return static_cast(syscall(SYS_gettid)); #elif __FreeBSD__ long tid; thr_self(&tid); - return static_cast(tid); + return static_cast(tid); #elif __APPLE__ uint64_t tid; pthread_threadid_np(nullptr, &tid); - return static_cast(tid); + return static_cast(tid); #else // Default to standard C++11 (other Unix) - return static_cast(std::hash()(std::this_thread::get_id())); + return static_cast(std::hash()(std::this_thread::get_id())); +#endif #endif } -// Return current thread id as size_t (from thread local storage) -inline size_t thread_id() SPDLOG_NOEXCEPT +// Return current thread id as thread_t (from thread local storage) +inline thread_t thread_id() SPDLOG_NOEXCEPT { #if defined(SPDLOG_NO_TLS) return _thread_id(); #else // cache thread id in tls - static thread_local const size_t tid = _thread_id(); + static thread_local const thread_t tid = _thread_id(); return tid; #endif } diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index c0ad86e8..fe8f2804 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -687,6 +687,13 @@ public: void format(const details::log_msg &msg, const std::tm &, fmt::memory_buffer &dest) override { +#if defined(SPDLOG_CUSTOM_THREAD_ID_PRINTER) + SPDLOG_CUSTOM_THREAD_ID_PRINTER +#else + using used_thread_t = decltype(msg.thread_id); + static_assert(std::is_convertible::value, + "custom thread_t is not convertible to size_t, use SPDLOG_CUSTOM_THREAD_ID_PRINTER macro"); + if (padinfo_.enabled()) { const auto field_size = fmt_helper::count_digits(msg.thread_id); @@ -697,6 +704,7 @@ public: { fmt_helper::append_int(msg.thread_id, dest); } +#endif } }; diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index b3b71e4f..f9041555 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -142,4 +142,22 @@ // Defaults to __FUNCTION__ (should work on all compilers) if not defined. // // #define SPDLOG_FUNCTION __PRETTY_FUNCTION__ -/////////////////////////////////////////////////////////////////////////////// \ No newline at end of file +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to use custom thread identifier type. +// +// WARNING: Custom thread id getter should be used when thread_t is not +// convertible to size_t (which is default type) or values should be received +// from another source, e.g. pthread identifiers under Linux. +// +// WARNING: If custom thread_t is not convertible to size_t and custom thread +// id printer code is not set then compile-time assert will fail. Use +// SPDLOG_CUSTOM_THREAD_ID_PRINTER macro to provide corresponding code to print +// thread id. This code will be placed inside t_formatter (for '%t') in +// details/pattern_formatter.h. +// +// #define SPDLOG_CUSTOM_THREAD_T custom_type +// #define SPDLOG_CUSTOM_THREAD_ID_GETTER custom_type_get_func() +// #define SPDLOG_CUSTOM_THREAD_ID_PRINTER { } // see t_formatter::format +///////////////////////////////////////////////////////////////////////////////