mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-27 16:09:05 +08:00
Remove mdc
This commit is contained in:
parent
14a202580b
commit
eacd4d6b2a
@ -130,8 +130,8 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
||||
set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
||||
|
||||
set(CMAKE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
|
||||
|
||||
# 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 $<$<AND:$<CXX_COMPILER_ID:MSVC>,$<NOT:$<COMPILE_LANGUAGE:CUDA>>>:/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()
|
||||
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -1,34 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <spdlog/common.h>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace spdlog {
|
||||
class SPDLOG_API mdc {
|
||||
public:
|
||||
using mdc_map_t = std::map<std::string, std::string>;
|
||||
|
||||
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
|
@ -3,8 +3,6 @@
|
||||
|
||||
#include "spdlog/pattern_formatter.h"
|
||||
|
||||
#include <spdlog/mdc.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
@ -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 <typename ScopedPadder>
|
||||
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<null_scoped_padder> 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<details::elapsed_formatter<Padder, std::chrono::seconds>>(padding));
|
||||
break;
|
||||
|
||||
case ('&'):
|
||||
formatters_.push_back(std::make_unique<details::mdc_formatter<Padder>>(padding));
|
||||
break;
|
||||
|
||||
default: // Unknown flag appears as is
|
||||
auto unknown_flag = std::make_unique<details::aggregate_formatter>();
|
||||
|
||||
|
@ -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<spdlog::pattern_formatter>();
|
||||
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<spdlog::pattern_formatter>();
|
||||
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<spdlog::pattern_formatter>();
|
||||
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<spdlog::pattern_formatter>();
|
||||
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<spdlog::pattern_formatter>();
|
||||
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(); }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user