diff --git a/CMakeLists.txt b/CMakeLists.txt index 5cbb53be..d0305001 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/src/details/os_unix.cpp b/src/details/os_unix.cpp index 8319f633..80ac731f 100644 --- a/src/details/os_unix.cpp +++ b/src/details/os_unix.cpp @@ -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 { diff --git a/src/details/os_windows.cpp b/src/details/os_windows.cpp index d14a3e58..5c5c2c5b 100644 --- a/src/details/os_windows.cpp +++ b/src/details/os_windows.cpp @@ -157,25 +157,28 @@ size_t _thread_id() noexcept { return static_cast(::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 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 "???"; } }