Fix Visual Studio poor std::this_thread::get_id() performance by using GetCurrentThreadId() (and pthread_self() under linux)

This commit is contained in:
gabime 2015-04-07 20:26:42 +03:00
parent 67e0957e67
commit a09107927b
5 changed files with 22 additions and 7 deletions

View File

@ -60,7 +60,7 @@ class async_log_helper
level::level_enum level; level::level_enum level;
log_clock::time_point time; log_clock::time_point time;
std::string txt; std::string txt;
std::thread::id thread_id; uint64_t thread_id;
async_msg() = default; async_msg() = default;
~async_msg() = default; ~async_msg() = default;

View File

@ -64,7 +64,9 @@ public:
{ {
_log_msg.logger_name = _callback_logger->name(); _log_msg.logger_name = _callback_logger->name();
_log_msg.time = os::now(); _log_msg.time = os::now();
_log_msg.thread_id = std::this_thread::get_id(); _log_msg.thread_id = os::thread_id();
_callback_logger->_log_msg(_log_msg); _callback_logger->_log_msg(_log_msg);
} }
} }

View File

@ -46,7 +46,7 @@ struct log_msg
log_msg(const log_msg& other) : log_msg(const log_msg& other) :
logger_name(other.logger_name), logger_name(other.logger_name),
level(other.level), level(other.level),
time(other.time), time(other.time),
thread_id(other.thread_id) thread_id(other.thread_id)
{ {
@ -84,7 +84,7 @@ struct log_msg
void clear() void clear()
{ {
level = level::off; level = level::off;
raw.clear(); raw.clear();
formatted.clear(); formatted.clear();
} }
@ -92,7 +92,7 @@ struct log_msg
std::string logger_name; std::string logger_name;
level::level_enum level; level::level_enum level;
log_clock::time_point time; log_clock::time_point time;
std::thread::id thread_id; uint64_t thread_id;
fmt::MemoryWriter raw; fmt::MemoryWriter raw;
fmt::MemoryWriter formatted; fmt::MemoryWriter formatted;
}; };

View File

@ -32,6 +32,8 @@
# define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN
# endif # endif
# include <Windows.h> # include <Windows.h>
#else
#include <pthread.h>
#endif #endif
#include "../common.h" #include "../common.h"
@ -167,6 +169,17 @@ inline int utc_minutes_offset(const std::tm& tm = details::os::localtime())
} }
inline uint64_t thread_id()
{
#ifdef _WIN32
return ::GetCurrentThreadId();
#else
return (uint64_t) pthread_self();
#endif
}
} //os } //os
} //details } //details
} //spdlog } //spdlog

View File

@ -354,7 +354,7 @@ class t_formatter :public flag_formatter
{ {
void format(details::log_msg& msg, const std::tm&) override void format(details::log_msg& msg, const std::tm&) override
{ {
msg.formatted << std::hash<std::thread::id>()(msg.thread_id); msg.formatted << msg.thread_id;
} }
}; };
@ -478,7 +478,7 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
{ {
switch (flag) switch (flag)
{ {
// logger name // logger name
case 'n': case 'n':
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::name_formatter())); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::name_formatter()));
break; break;