From bbd3d7ffe2e6178224454af44d130a9249951154 Mon Sep 17 00:00:00 2001 From: GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> Date: Thu, 12 Sep 2024 02:46:34 +0200 Subject: [PATCH] Disable MDC if `SPDLOG_NO_TLS` is defined --- include/spdlog/mdc.h | 4 ++++ include/spdlog/pattern_formatter-inl.h | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/spdlog/mdc.h b/include/spdlog/mdc.h index 41f0c1f3..ab660481 100644 --- a/include/spdlog/mdc.h +++ b/include/spdlog/mdc.h @@ -8,6 +8,10 @@ #include +#ifdef SPDLOG_NO_TLS + #error MDC is not supported with SPDLOG_NO_TLS. +#endif + // MDC is a simple map of key->string values stored in thread local storage whose content will be printed by the loggers. // Note: Not supported in async mode (thread local storage - so the async thread pool have different copy). // diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 756e5941..2f2aed50 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -10,10 +10,13 @@ #include #include #include -#include #include #include +#ifndef SPDLOG_NO_TLS + #include +#endif + #include #include #include @@ -784,6 +787,7 @@ private: log_clock::time_point last_message_time_; }; +#ifndef SPDLOG_NO_TLS // Class for formatting Mapped Diagnostic Context (MDC) in log messages. // Example: [logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message template @@ -824,6 +828,7 @@ public: } } }; +#endif // Full info formatter // pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v @@ -901,6 +906,7 @@ public: dest.push_back(' '); } +#ifndef SPDLOG_NO_TLS // add mdc if present auto &mdc_map = mdc::get_context(); if (!mdc_map.empty()) { @@ -909,6 +915,8 @@ public: dest.push_back(']'); dest.push_back(' '); } +#endif + // fmt_helper::append_string_view(msg.msg(), dest); fmt_helper::append_string_view(msg.payload, dest); } @@ -916,7 +924,9 @@ public: private: std::chrono::seconds cache_timestamp_{0}; memory_buf_t cached_datetime_; +#ifndef SPDLOG_NO_TLS mdc_formatter mdc_formatter_{padding_info{}}; +#endif }; } // namespace details @@ -1211,9 +1221,11 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i padding)); break; +#ifndef SPDLOG_NO_TLS case ('&'): formatters_.push_back(details::make_unique>(padding)); break; +#endif default: // Unknown flag appears as is auto unknown_flag = details::make_unique();