Commit c6c51743 authored by Gabi Melman's avatar Gabi Melman

wip custom formatter flags

parent ee54f54c
...@@ -974,11 +974,13 @@ private: ...@@ -974,11 +974,13 @@ private:
} // namespace details } // namespace details
SPDLOG_INLINE pattern_formatter::pattern_formatter(std::string pattern, pattern_time_type time_type, std::string eol) SPDLOG_INLINE pattern_formatter::pattern_formatter(
std::string pattern, pattern_time_type time_type, std::string eol, custom_flags custom_user_flags)
: pattern_(std::move(pattern)) : pattern_(std::move(pattern))
, eol_(std::move(eol)) , eol_(std::move(eol))
, pattern_time_type_(time_type) , pattern_time_type_(time_type)
, last_log_secs_(0) , last_log_secs_(0)
, custom_handlers_(std::move(custom_user_flags))
{ {
std::memset(&cached_tm_, 0, sizeof(cached_tm_)); std::memset(&cached_tm_, 0, sizeof(cached_tm_));
compile_pattern_(pattern_); compile_pattern_(pattern_);
...@@ -997,7 +999,12 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type, ...@@ -997,7 +999,12 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type,
SPDLOG_INLINE std::unique_ptr<formatter> pattern_formatter::clone() const SPDLOG_INLINE std::unique_ptr<formatter> pattern_formatter::clone() const
{ {
return details::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_); custom_flags cloned_custom_formatters;
for (auto &it : custom_handlers_)
{
cloned_custom_formatters[it.first] = it.second->clone();
}
return details::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters));
} }
SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest)
...@@ -1017,6 +1024,11 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory ...@@ -1017,6 +1024,11 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory
details::fmt_helper::append_string_view(eol_, dest); details::fmt_helper::append_string_view(eol_, dest);
} }
SPDLOG_INLINE void pattern_formatter::recompile()
{
compile_pattern_(pattern_);
}
SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg) SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg)
{ {
if (pattern_time_type_ == pattern_time_type::local) if (pattern_time_type_ == pattern_time_type::local)
...@@ -1029,6 +1041,12 @@ SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg) ...@@ -1029,6 +1041,12 @@ SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg)
template<typename Padder> template<typename Padder>
SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_info padding) SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_info padding)
{ {
auto it = custom_handlers_.find(flag);
if (it != custom_handlers_.end())
{
formatters_.push_back(it->second->clone());
return;
}
switch (flag) switch (flag)
{ {
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map>
namespace spdlog { namespace spdlog {
namespace details { namespace details {
...@@ -46,7 +47,7 @@ struct padding_info ...@@ -46,7 +47,7 @@ struct padding_info
bool enabled_ = false; bool enabled_ = false;
}; };
class flag_formatter class SPDLOG_API flag_formatter
{ {
public: public:
explicit flag_formatter(padding_info padinfo) explicit flag_formatter(padding_info padinfo)
...@@ -62,11 +63,19 @@ protected: ...@@ -62,11 +63,19 @@ protected:
} // namespace details } // namespace details
class SPDLOG_API custom_flag_formatter : public details::flag_formatter
{
public:
virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
};
class SPDLOG_API pattern_formatter final : public formatter class SPDLOG_API pattern_formatter final : public formatter
{ {
public: public:
explicit pattern_formatter( using custom_flags = std::unordered_map<char, std::unique_ptr<custom_flag_formatter>>;
std::string pattern, pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
explicit pattern_formatter(std::string pattern, pattern_time_type time_type = pattern_time_type::local,
std::string eol = spdlog::details::os::default_eol, custom_flags custom_user_flags = {});
// use default pattern is not given // use default pattern is not given
explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol); explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
...@@ -77,6 +86,14 @@ public: ...@@ -77,6 +86,14 @@ public:
std::unique_ptr<formatter> clone() const override; std::unique_ptr<formatter> clone() const override;
void format(const details::log_msg &msg, memory_buf_t &dest) override; void format(const details::log_msg &msg, memory_buf_t &dest) override;
template<typename T, typename... Args>
pattern_formatter &add_flag_handler(char flag, const Args &... args)
{
custom_handlers_[flag] = details::make_unique<T>(args...);
return *this;
}
void recompile();
private: private:
std::string pattern_; std::string pattern_;
std::string eol_; std::string eol_;
...@@ -84,6 +101,7 @@ private: ...@@ -84,6 +101,7 @@ private:
std::tm cached_tm_; std::tm cached_tm_;
std::chrono::seconds last_log_secs_; std::chrono::seconds last_log_secs_;
std::vector<std::unique_ptr<details::flag_formatter>> formatters_; std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
custom_flags custom_handlers_;
std::tm get_time_(const details::log_msg &msg); std::tm get_time_(const details::log_msg &msg);
template<typename Padder> template<typename Padder>
......
...@@ -246,6 +246,8 @@ TEST_CASE("paddinng_truncate_funcname", "[pattern_formatter]") ...@@ -246,6 +246,8 @@ TEST_CASE("paddinng_truncate_funcname", "[pattern_formatter]")
REQUIRE(lines[1] == "message [funct]"); REQUIRE(lines[1] == "message [funct]");
} }
TEST_CASE("clone-default-formatter", "[pattern_formatter]") TEST_CASE("clone-default-formatter", "[pattern_formatter]")
{ {
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>(); auto formatter_1 = std::make_shared<spdlog::pattern_formatter>();
...@@ -305,6 +307,43 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]") ...@@ -305,6 +307,43 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]")
REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2)); REQUIRE(fmt::to_string(formatted_1) == fmt::to_string(formatted_2));
} }
class custom_test_flag : public spdlog::custom_flag_formatter
{
public:
class custom_test_flag (std::string txt) : some_txt{std::move(txt)}
{}
void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
{
dest.append(some_txt.data(), some_txt.data() + some_txt.size());
}
std::string some_txt;
std::unique_ptr<custom_flag_formatter> clone() const override
{
return spdlog::details::make_unique<custom_test_flag>(some_txt);
}
};
// test clone with custom flag formatters
TEST_CASE("clone-custom_formatter", "[pattern_formatter]")
{
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>("[%n] [%t] %v", spdlog::pattern_time_type::utc, "");
formatter_1->add_flag_handler<custom_test_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");
memory_buf_t formatted_1;
memory_buf_t formatted_2;
formatter_1->format(msg, formatted_1);
formatter_2->format(msg, formatted_2);
REQUIRE(fmt::to_string(formatted_1) == "[logger-name] [custom_output] some message");
REQUIRE(fmt::to_string(formatted_2) == "[logger-name] [custom_output] some message");
}
// //
// Test source location formatting // Test source location formatting
// //
...@@ -358,3 +397,16 @@ TEST_CASE("full filename formatter", "[pattern_formatter]") ...@@ -358,3 +397,16 @@ TEST_CASE("full filename formatter", "[pattern_formatter]")
formatter.format(msg, formatted); formatter.format(msg, formatted);
REQUIRE(fmt::to_string(formatted) == test_path); REQUIRE(fmt::to_string(formatted) == test_path);
} }
TEST_CASE("custom flags", "[pattern_formatter]")
{
auto formatter = std::make_shared<spdlog::pattern_formatter>("[%n] [%t] [%u] %v", spdlog::pattern_time_type::utc, "");
formatter->add_flag_handler<custom_test_flag>('t', "custom1").add_flag_handler<custom_test_flag>('u', "custom2").recompile();
memory_buf_t formatted;
spdlog::details::log_msg msg(spdlog::source_loc{}, "logger-name", spdlog::level::info, "some message");
formatter->format(msg, formatted);
REQUIRE(fmt::to_string(formatted) == "[logger-name] [custom1] [custom2] some message");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment