add custom thread id type

pull/993/head
Alexey Ochapov 7 years ago
parent d235e7d46f
commit 450a11f3b8

@ -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).

@ -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<size_t>(::GetCurrentThreadId());
return static_cast<thread_t>(::GetCurrentThreadId());
#elif __linux__
#if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
#define SYS_gettid __NR_gettid
#endif
return static_cast<size_t>(syscall(SYS_gettid));
return static_cast<thread_t>(syscall(SYS_gettid));
#elif __FreeBSD__
long tid;
thr_self(&tid);
return static_cast<size_t>(tid);
return static_cast<thread_t>(tid);
#elif __APPLE__
uint64_t tid;
pthread_threadid_np(nullptr, &tid);
return static_cast<size_t>(tid);
return static_cast<thread_t>(tid);
#else // Default to standard C++11 (other Unix)
return static_cast<size_t>(std::hash<std::thread::id>()(std::this_thread::get_id()));
return static_cast<thread_t>(std::hash<std::thread::id>()(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
}

@ -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<used_thread_t, size_t>::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
}
};

@ -143,3 +143,21 @@
//
// #define SPDLOG_FUNCTION __PRETTY_FUNCTION__
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// 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
///////////////////////////////////////////////////////////////////////////////

Loading…
Cancel
Save