diff --git a/example/example.cpp b/example/example.cpp index 7411fbd3..14e751d8 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -18,6 +18,7 @@ void multi_sink_example(); void user_defined_example(); void err_handler_example(); void syslog_example(); +void custom_flags_example(); #include "spdlog/spdlog.h" #include "spdlog/cfg/env.h" // for loading levels from the environment variable @@ -70,6 +71,7 @@ int main(int, char *[]) user_defined_example(); err_handler_example(); trace_example(); + custom_flags_example(); // Flush all *registered* loggers using a worker thread every 3 seconds. // note: registered loggers *must* be thread safe for this to work correctly! @@ -250,5 +252,34 @@ void android_example() auto android_logger = spdlog::android_logger_mt("android", tag); android_logger->critical("Use \"adb shell logcat\" to view this message."); } - #endif + + +// log patterns can now contain custom flags! +// add custom flag '%*' which will be cound to a instance + +#include "spdlog/pattern_formatter.h" +class my_formatter_flag : public spdlog::custom_flag_formatter +{ +public: + void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override + { + std::string some_txt = "custom-flag"; + dest.append(some_txt.data(), some_txt.data() + some_txt.size()); + } + + std::unique_ptr clone() const override + { + return spdlog::details::make_unique(); + } +}; + + +void custom_flags_example() +{ + + using spdlog::details::make_unique; //for pre c++14 + auto formatter = make_unique("[%+] [%*]"); + formatter->add_flag('*').recompile(); + spdlog::set_formatter(std::move(formatter)); +} diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index ff6e9773..b42383b7 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #ifndef SPDLOG_DISABLE_DEFAULT_LOGGER // support for the default stdout color logger diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index d9d58c29..69d6a1de 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -9,7 +9,7 @@ #include #include -#include +#include #include diff --git a/include/spdlog/details/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h similarity index 99% rename from include/spdlog/details/pattern_formatter-inl.h rename to include/spdlog/pattern_formatter-inl.h index f4efcc23..6c3e0b79 100644 --- a/include/spdlog/details/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -4,7 +4,7 @@ #pragma once #ifndef SPDLOG_HEADER_ONLY -#include +#include #endif #include diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/pattern_formatter.h similarity index 96% rename from include/spdlog/details/pattern_formatter.h rename to include/spdlog/pattern_formatter.h index 574523c5..33987e4e 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -87,10 +87,10 @@ public: void format(const details::log_msg &msg, memory_buf_t &dest) override; template - pattern_formatter &add_flag_handler(char flag, const Args &... args) + pattern_formatter &add_flag(char flag, const Args &... args) { custom_handlers_[flag] = details::make_unique(args...); - return *this; + return *this; } void recompile(); diff --git a/include/spdlog/sinks/base_sink-inl.h b/include/spdlog/sinks/base_sink-inl.h index 2883c058..b15fb0e6 100644 --- a/include/spdlog/sinks/base_sink-inl.h +++ b/include/spdlog/sinks/base_sink-inl.h @@ -8,7 +8,7 @@ #endif #include -#include +#include #include diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index ae98fee4..8fccb4ee 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -6,7 +6,7 @@ #include "base_sink.h" #include #include -#include +#include #include #include diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h index adbcb632..20f2114f 100644 --- a/include/spdlog/sinks/stdout_sinks-inl.h +++ b/include/spdlog/sinks/stdout_sinks-inl.h @@ -8,7 +8,7 @@ #endif #include -#include +#include #include namespace spdlog { diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 9001c14e..04b74942 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -8,7 +8,7 @@ #endif #include -#include +#include namespace spdlog { namespace sinks { diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h index 43aa49ef..02e818a4 100644 --- a/include/spdlog/spdlog-inl.h +++ b/include/spdlog/spdlog-inl.h @@ -8,7 +8,7 @@ #endif #include -#include +#include namespace spdlog { diff --git a/src/spdlog.cpp b/src/spdlog.cpp index 38047398..c0904e6c 100644 --- a/src/spdlog.cpp +++ b/src/spdlog.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/tests/includes.h b/tests/includes.h index 98734b18..7285873a 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -23,4 +23,4 @@ #include "spdlog/sinks/ostream_sink.h" #include "spdlog/sinks/rotating_file_sink.h" #include "spdlog/sinks/stdout_color_sinks.h" -#include "spdlog/details/pattern_formatter.h" \ No newline at end of file +#include "spdlog/pattern_formatter.h" \ No newline at end of file diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index 04247bc5..7315a1fb 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -330,7 +330,7 @@ public: TEST_CASE("clone-custom_formatter", "[pattern_formatter]") { auto formatter_1 = std::make_shared("[%n] [%t] %v", spdlog::pattern_time_type::utc, ""); - formatter_1->add_flag_handler('t', "custom_output").recompile(); + formatter_1->add_flag('t', "custom_output").recompile(); auto formatter_2 = formatter_1->clone(); std::string logger_name = "logger-name"; spdlog::details::log_msg msg(logger_name, spdlog::level::info, "some message"); @@ -402,7 +402,7 @@ TEST_CASE("full filename formatter", "[pattern_formatter]") TEST_CASE("custom flags", "[pattern_formatter]") { auto formatter = std::make_shared("[%n] [%t] [%u] %v", spdlog::pattern_time_type::utc, ""); - formatter->add_flag_handler('t', "custom1").add_flag_handler('u', "custom2").recompile(); + formatter->add_flag('t', "custom1").add_flag('u', "custom2").recompile(); memory_buf_t formatted;