From 332eaaf91685a891197726b96ba97443c3bdb936 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 21 Mar 2020 15:54:01 +0200 Subject: [PATCH] Update readme with custom flags example --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 130d0802..4daad2e5 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,39 @@ void user_defined_example() } ``` + +--- +#### User defined flags in the log pattern +```c++ +// Log patterns can contain custom flags. +// the following example will add new flag '%*' - which will be bound 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('*').set_pattern("[%n] [%*] [%^%l%$] %v"); + spdlog::set_formatter(std::move(formatter)); +} + +``` + --- #### Custom error handler ```c++ @@ -295,6 +328,8 @@ void err_handler_example() } ``` + + --- #### syslog ```c++