From 362214a349013fe779f919ac45529564f06ab88f Mon Sep 17 00:00:00 2001 From: allen_qfl <35458397+Allen-20180115@users.noreply.github.com> Date: Fri, 13 Sep 2024 03:27:25 +0800 Subject: [PATCH] fix/issue-3101: fix the issue where mdc ignores SPDLOG_NO_TLS (#3184) Co-authored-by: dyf --- example/example.cpp | 9 ++++++++- include/spdlog/mdc.h | 4 ++++ include/spdlog/pattern_formatter-inl.h | 16 +++++++++++++++- tests/includes.h | 6 +++++- tests/test_pattern_formatter.cpp | 2 ++ 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index 267e4215..de058d27 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -382,7 +382,9 @@ void replace_default_logger_example() { // Mapped Diagnostic Context (MDC) is a map that stores key-value pairs (string values) in thread local storage. // Each thread maintains its own MDC, which loggers use to append diagnostic information to log outputs. // Note: it is not supported in asynchronous mode due to its reliance on thread-local storage. -#include "spdlog/mdc.h" + +#ifndef SPDLOG_NO_TLS + #include "spdlog/mdc.h" void mdc_example() { spdlog::mdc::put("key1", "value1"); @@ -391,3 +393,8 @@ void mdc_example() spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [%&] %v"); spdlog::info("Some log message with context"); } +#else +void mdc_example() { + // if TLS feature is disabled +} +#endif diff --git a/include/spdlog/mdc.h b/include/spdlog/mdc.h index 41f0c1f3..5532de0d 100644 --- a/include/spdlog/mdc.h +++ b/include/spdlog/mdc.h @@ -3,6 +3,10 @@ #pragma once +#if defined(SPDLOG_NO_TLS) + #error "This header requires thread local storage support. Please do not define SPDLOG_NO_TLS." +#endif + #include #include diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 756e5941..cae7ddfd 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -10,7 +10,11 @@ #include #include #include -#include + +#ifndef SPDLOG_NO_TLS + #include +#endif + #include #include @@ -786,6 +790,7 @@ private: // 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 +#ifndef SPDLOG_NO_TLS template class mdc_formatter : public flag_formatter { public: @@ -824,6 +829,7 @@ public: } } }; +#endif // Full info formatter // pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v @@ -901,6 +907,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 +916,7 @@ 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,11 @@ 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 +1223,11 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i padding)); break; +#ifndef SPDLOG_NO_TLS // mdc formatter requires TLS support case ('&'): formatters_.push_back(details::make_unique>(padding)); break; +#endif default: // Unknown flag appears as is auto unknown_flag = details::make_unique(); diff --git a/tests/includes.h b/tests/includes.h index 14e988b9..5dbb2191 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -26,7 +26,11 @@ #include "spdlog/spdlog.h" #include "spdlog/async.h" #include "spdlog/details/fmt_helper.h" -#include "spdlog/mdc.h" + +#ifndef SPDLOG_NO_TLS + #include "spdlog/mdc.h" +#endif + #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/daily_file_sink.h" #include "spdlog/sinks/null_sink.h" diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index c0eaa111..d248e996 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -501,6 +501,7 @@ TEST_CASE("override need_localtime", "[pattern_formatter]") { } } +#ifndef SPDLOG_NO_TLS TEST_CASE("mdc formatter test-1", "[pattern_formatter]") { spdlog::mdc::put("mdc_key_1", "mdc_value_1"); spdlog::mdc::put("mdc_key_2", "mdc_value_2"); @@ -628,3 +629,4 @@ TEST_CASE("mdc empty", "[pattern_formatter]") { SECTION("Tear down") { spdlog::mdc::clear(); } } +#endif