Not tls option

This commit is contained in:
Gabi Melman 2024-12-05 06:52:23 +02:00
parent 0bf2e7811a
commit b03c45ebaa
3 changed files with 19 additions and 7 deletions

View File

@ -86,6 +86,7 @@ endif()
option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" OFF)
option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" OFF)
option(SPDLOG_DISABLE_DEFAULT_LOGGER "Disable default logger creation" OFF)
option(SPDLOG_NO_TLS "Disable thread local storage" OFF)
# clang-tidy
option(SPDLOG_TIDY "run clang-tidy" OFF)
@ -307,8 +308,13 @@ endif()
# ---------------------------------------------------------------------------------------
# spdlog private defines according to the options
# ---------------------------------------------------------------------------------------
foreach(SPDLOG_OPTION SPDLOG_CLOCK_COARSE SPDLOG_PREVENT_CHILD_FD SPDLOG_NO_THREAD_ID SPDLOG_DISABLE_DEFAULT_LOGGER
SPDLOG_FWRITE_UNLOCKED)
foreach(SPDLOG_OPTION
SPDLOG_CLOCK_COARSE
SPDLOG_PREVENT_CHILD_FD
SPDLOG_NO_THREAD_ID
SPDLOG_DISABLE_DEFAULT_LOGGER
SPDLOG_NO_TLS
SPDLOG_FWRITE_UNLOCKED)
if(${SPDLOG_OPTION})
target_compile_definitions(spdlog PRIVATE ${SPDLOG_OPTION})
endif()

View File

@ -232,9 +232,12 @@ size_t _thread_id() noexcept {
// Return current thread id as size_t (from thread local storage)
size_t thread_id() noexcept {
// cache thread id in tls
#if defined(SPDLOG_NO_TLS)
return _thread_id();
#else // cache thread id in tls
static thread_local const size_t tid = _thread_id();
return tid;
#endif
}
void sleep_for_millis(unsigned int milliseconds) noexcept {

View File

@ -157,25 +157,28 @@ size_t _thread_id() noexcept { return static_cast<size_t>(::GetCurrentThreadId()
// Return current thread id as size_t (from thread local storage)
size_t thread_id() noexcept {
// cache thread id in tls
#if defined(SPDLOG_NO_TLS)
return _thread_id();
#else // cache thread id in tls
static thread_local const size_t tid = _thread_id();
return tid;
#endif
}
// This is avoid msvc issue in sleep_for that happens if the clock changes.
// See https://github.com/gabime/spdlog/issues/609
void sleep_for_millis(unsigned int milliseconds) noexcept { ::Sleep(milliseconds); }
// Try tp convert wstring filename to string. Return "??" if failed
// Try tp convert wstring filename to string. Return "???" if failed
std::string filename_to_str(const filename_t &filename) {
static_assert(std::is_same_v<filename_t::value_type, wchar_t>, "filename_t type must be wchar_t");
try {
try {
memory_buf_t buf;
wstr_to_utf8buf(filename.wstring(), buf);
return std::string(buf.data(), buf.size());
}
catch (...) {
return "??";
return "???";
}
}