diff --git a/CMakeLists.txt b/CMakeLists.txt index db662a20..784f382a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,8 +130,8 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$) - set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$) - + set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$) + # make sure __cplusplus is defined add_compile_options(/Zc:__cplusplus) # enable parallel build for the solution @@ -218,10 +218,9 @@ set(SPDLOG_HEADERS "include/spdlog/sinks/systemd_sink.h" "include/spdlog/sinks/tcp_sink.h" "include/spdlog/sinks/udp_sink.h" - "include/spdlog/mdc.h" ) -set(SPDLOG_SRCS +set(SPDLOG_SRCS "src/async_logger.cpp" "src/common.cpp" "src/logger.cpp" @@ -233,7 +232,7 @@ set(SPDLOG_SRCS "src/details/log_msg_buffer.cpp" "src/details/periodic_worker.cpp" "src/details/registry.cpp" - "src/details/thread_pool.cpp" + "src/details/thread_pool.cpp" "src/sinks/ansicolor_sink.cpp" "src/sinks/base_sink.cpp" "src/sinks/basic_file_sink.cpp" @@ -267,14 +266,14 @@ list(APPEND SPDLOG_HEADERS ${OUT_CONFIG_FILE}) if(BUILD_SHARED_LIBS) if(WIN32) set(VERSION_RC ${CMAKE_CURRENT_BINARY_DIR}/version.rc) - configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY) endif() add_library(spdlog SHARED ${SPDLOG_SRCS} ${SPDLOG_HEADERS} ${VERSION_RC}) target_compile_definitions(spdlog PUBLIC SPDLOG_SHARED_LIB) if(MSVC) # disable dlls related warnings on msvc target_compile_options(spdlog PUBLIC $<$,$>>:/wd4251 - /wd4275>) + /wd4275>) endif() else() add_library(spdlog STATIC ${SPDLOG_SRCS} ${SPDLOG_HEADERS}) @@ -303,7 +302,7 @@ set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX d) # set source groups for visual studio if(CMAKE_GENERATOR MATCHES "Visual Studio") source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/include PREFIX include FILES ${SPDLOG_HEADERS}) - source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX sources FILES ${SPDLOG_SRCS}) + source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX sources FILES ${SPDLOG_SRCS}) source_group(sources FILES ${VERSION_RC}) endif() diff --git a/example/example.cpp b/example/example.cpp index 0d8bdcd1..b70cc153 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -26,7 +26,6 @@ void udp_example(); void custom_flags_example(); void file_events_example(); void replace_default_logger_example(); -void mdc_example(); #include "spdlog/cfg/env.h" // support for loading levels from the environment variable #include "spdlog/spdlog.h" @@ -72,7 +71,6 @@ int main(int, char *[]) { custom_flags_example(); file_events_example(); replace_default_logger_example(); - mdc_example(); // Flush all *registered* loggers using a worker thread every 3 seconds. // note: registered loggers *must* be thread safe for this to work correctly! @@ -345,15 +343,5 @@ void replace_default_logger_example() { spdlog::debug("This message should not be displayed!"); spdlog::set_level(spdlog::level::trace); spdlog::debug("This message should be displayed.."); - spdlog::set_default_logger(old_logger); } - -#include "spdlog/mdc.h" -void mdc_example() { - spdlog::mdc::put("key1", "value1"); - spdlog::mdc::put("key2", "value2"); - // use the %& formatter flag to print all MDC values - spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [%&] %v"); - spdlog::info("Some log message with context"); -} diff --git a/include/spdlog/mdc.h b/include/spdlog/mdc.h deleted file mode 100644 index f673482c..00000000 --- a/include/spdlog/mdc.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include - -#include -#include - -namespace spdlog { -class SPDLOG_API mdc { -public: - using mdc_map_t = std::map; - - static void put(const std::string &key, const std::string &value) { get_context()[key] = value; } - - static std::string get(const std::string &key) { - auto &context = get_context(); - auto it = context.find(key); - if (it != context.end()) { - return it->second; - } - return ""; - } - - static void remove(const std::string &key) { get_context().erase(key); } - - static void clear() { get_context().clear(); } - - static mdc_map_t &get_context() { - static thread_local mdc_map_t context; - return context; - } -}; - -} // namespace spdlog diff --git a/src/pattern_formatter.cpp b/src/pattern_formatter.cpp index 1d3dbd9f..1dbbf318 100644 --- a/src/pattern_formatter.cpp +++ b/src/pattern_formatter.cpp @@ -3,8 +3,6 @@ #include "spdlog/pattern_formatter.h" -#include - #include #include #include @@ -739,47 +737,6 @@ private: log_clock::time_point last_message_time_; }; -// 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 -class mdc_formatter final : public flag_formatter { -public: - explicit mdc_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { - auto &mdc_map = mdc::get_context(); - if (mdc_map.empty()) { - ScopedPadder p(0, padinfo_, dest); - return; - } else { - format_mdc(mdc_map, dest); - } - } - - void format_mdc(const mdc::mdc_map_t &mdc_map, memory_buf_t &dest) { - auto last_element = --mdc_map.end(); - for (auto it = mdc_map.begin(); it != mdc_map.end(); ++it) { - auto &pair = *it; - const auto &key = pair.first; - const auto &value = pair.second; - size_t content_size = key.size() + value.size() + 1; // 1 for ':' - - if (it != last_element) { - content_size++; // 1 for ' ' - } - - ScopedPadder p(content_size, padinfo_, dest); - fmt_helper::append_string_view(key, dest); - fmt_helper::append_string_view(":", dest); - fmt_helper::append_string_view(value, dest); - if (it != last_element) { - fmt_helper::append_string_view(" ", dest); - } - } - } -}; - // Full info formatter // pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v class full_formatter final : public flag_formatter { @@ -853,22 +810,12 @@ public: dest.push_back(']'); dest.push_back(' '); } - - // add mdc if present - auto &mdc_map = mdc::get_context(); - if (!mdc_map.empty()) { - dest.push_back('['); - mdc_formatter_.format_mdc(mdc_map, dest); - dest.push_back(']'); - dest.push_back(' '); - } fmt_helper::append_string_view(msg.payload, dest); } private: std::chrono::seconds cache_timestamp_{0}; memory_buf_t cached_datetime_; - mdc_formatter mdc_formatter_{padding_info{}}; }; } // namespace details @@ -1146,10 +1093,6 @@ void pattern_formatter::handle_flag_(char flag, details::padding_info padding) { formatters_.push_back(std::make_unique>(padding)); break; - case ('&'): - formatters_.push_back(std::make_unique>(padding)); - break; - default: // Unknown flag appears as is auto unknown_flag = std::make_unique(); diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 64a8de6b..1da38ed3 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -1,5 +1,4 @@ #include "includes.h" -#include "spdlog/mdc.h" #include "spdlog/sinks/ostream_sink.h" #include "test_sink.h" @@ -457,120 +456,3 @@ TEST_CASE("override need_localtime", "[pattern_formatter]") { REQUIRE(to_string_view(formatted) == oss.str()); } } - -// Test Mapped Diagnostic Context (mdc) -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"); - - auto formatter = std::make_shared(); - formatter->set_pattern("[%n] [%l] [%&] %v"); - - memory_buf_t formatted; - spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message"); - formatter->format(msg, formatted); - - auto expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message{}", - spdlog::details::os::default_eol); - REQUIRE(to_string_view(formatted) == expected); - - SECTION("Tear down") { spdlog::mdc::clear(); } -} - -TEST_CASE("mdc formatter value update", "[pattern_formatter]") { - spdlog::mdc::put("mdc_key_1", "mdc_value_1"); - spdlog::mdc::put("mdc_key_2", "mdc_value_2"); - - auto formatter = std::make_shared(); - formatter->set_pattern("[%n] [%l] [%&] %v"); - - memory_buf_t formatted_1; - spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message"); - formatter->format(msg, formatted_1); - - auto expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message{}", - spdlog::details::os::default_eol); - - REQUIRE(to_string_view(formatted_1) == expected); - - spdlog::mdc::put("mdc_key_1", "new_mdc_value_1"); - memory_buf_t formatted_2; - formatter->format(msg, formatted_2); - expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_1:new_mdc_value_1 mdc_key_2:mdc_value_2] some message{}", - spdlog::details::os::default_eol); - - REQUIRE(to_string_view(formatted_2) == expected); - - SECTION("Tear down") { spdlog::mdc::clear(); } -} - -TEST_CASE("mdc different threads", "[pattern_formatter]") { - auto formatter = std::make_shared(); - formatter->set_pattern("[%n] [%l] [%&] %v"); - spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message"); - - memory_buf_t formatted_2; - - auto lambda_1 = [formatter, msg]() { - spdlog::mdc::put("mdc_key", "thread_1_id"); - memory_buf_t formatted; - formatter->format(msg, formatted); - - auto expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key:thread_1_id] some message{}", - spdlog::details::os::default_eol); - - REQUIRE(to_string_view(formatted) == expected); - }; - - auto lambda_2 = [formatter, msg]() { - spdlog::mdc::put("mdc_key", "thread_2_id"); - memory_buf_t formatted; - formatter->format(msg, formatted); - - auto expected = spdlog::fmt_lib::format("[logger-name] [info] [mdc_key:thread_2_id] some message{}", - spdlog::details::os::default_eol); - - REQUIRE(to_string_view(formatted) == expected); - }; - - std::thread thread_1(lambda_1); - std::thread thread_2(lambda_2); - - thread_1.join(); - thread_2.join(); - - SECTION("Tear down") { spdlog::mdc::clear(); } -} - -TEST_CASE("mdc remove key", "[pattern_formatter]") { - spdlog::mdc::put("mdc_key_1", "mdc_value_1"); - spdlog::mdc::put("mdc_key_2", "mdc_value_2"); - spdlog::mdc::remove("mdc_key_1"); - - auto formatter = std::make_shared(); - formatter->set_pattern("[%n] [%l] [%&] %v"); - - memory_buf_t formatted; - spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message"); - formatter->format(msg, formatted); - - auto expected = - spdlog::fmt_lib::format("[logger-name] [info] [mdc_key_2:mdc_value_2] some message{}", spdlog::details::os::default_eol); - REQUIRE(to_string_view(formatted) == expected); - - SECTION("Tear down") { spdlog::mdc::clear(); } -} - -TEST_CASE("mdc empty", "[pattern_formatter]") { - auto formatter = std::make_shared(); - formatter->set_pattern("[%n] [%l] [%&] %v"); - - memory_buf_t formatted; - spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message"); - formatter->format(msg, formatted); - - auto expected = spdlog::fmt_lib::format("[logger-name] [info] [] some message{}", spdlog::details::os::default_eol); - REQUIRE(to_string_view(formatted) == expected); - - SECTION("Tear down") { spdlog::mdc::clear(); } -}