Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/issue-3101: fix the issue where mdc ignores SPDLOG_NO_TLS #3184

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
4 changes: 4 additions & 0 deletions include/spdlog/mdc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <map>
#include <string>

Expand Down
16 changes: 15 additions & 1 deletion include/spdlog/pattern_formatter-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
#include <spdlog/details/fmt_helper.h>
#include <spdlog/details/log_msg.h>
#include <spdlog/details/os.h>
#include <spdlog/mdc.h>

#ifndef SPDLOG_NO_TLS
#include <spdlog/mdc.h>
#endif

#include <spdlog/fmt/fmt.h>
#include <spdlog/formatter.h>

Expand Down Expand Up @@ -786,6 +790,7 @@ class elapsed_formatter final : public flag_formatter {

// 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 <typename ScopedPadder>
class mdc_formatter : public flag_formatter {
public:
Expand Down Expand Up @@ -824,6 +829,7 @@ class mdc_formatter : public flag_formatter {
}
}
};
#endif

// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
Expand Down Expand Up @@ -901,6 +907,7 @@ class full_formatter final : public flag_formatter {
dest.push_back(' ');
}

#ifndef SPDLOG_NO_TLS
// add mdc if present
auto &mdc_map = mdc::get_context();
if (!mdc_map.empty()) {
Expand All @@ -909,14 +916,19 @@ class full_formatter final : public flag_formatter {
dest.push_back(']');
dest.push_back(' ');
}
#endif
// fmt_helper::append_string_view(msg.msg(), dest);
fmt_helper::append_string_view(msg.payload, dest);
}

private:
std::chrono::seconds cache_timestamp_{0};
memory_buf_t cached_datetime_;

#ifndef SPDLOG_NO_TLS
mdc_formatter<null_scoped_padder> mdc_formatter_{padding_info{}};
#endif

};

} // namespace details
Expand Down Expand Up @@ -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<details::mdc_formatter<Padder>>(padding));
break;
#endif

default: // Unknown flag appears as is
auto unknown_flag = details::make_unique<details::aggregate_formatter>();
Expand Down
6 changes: 5 additions & 1 deletion tests/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions tests/test_pattern_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -628,3 +629,4 @@ TEST_CASE("mdc empty", "[pattern_formatter]") {

SECTION("Tear down") { spdlog::mdc::clear(); }
}
#endif