mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 08:25:43 +08:00
wip custom formatter flags
This commit is contained in:
parent
ee54f54ced
commit
c6c517431f
@ -974,11 +974,13 @@ private:
|
||||
|
||||
} // 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))
|
||||
, eol_(std::move(eol))
|
||||
, pattern_time_type_(time_type)
|
||||
, last_log_secs_(0)
|
||||
, custom_handlers_(std::move(custom_user_flags))
|
||||
{
|
||||
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
||||
compile_pattern_(pattern_);
|
||||
@ -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
|
||||
{
|
||||
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)
|
||||
@ -1017,6 +1024,11 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory
|
||||
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)
|
||||
{
|
||||
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)
|
||||
template<typename Padder>
|
||||
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)
|
||||
{
|
||||
|
||||
|
@ -14,6 +14,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace spdlog {
|
||||
namespace details {
|
||||
@ -46,7 +47,7 @@ struct padding_info
|
||||
bool enabled_ = false;
|
||||
};
|
||||
|
||||
class flag_formatter
|
||||
class SPDLOG_API flag_formatter
|
||||
{
|
||||
public:
|
||||
explicit flag_formatter(padding_info padinfo)
|
||||
@ -62,11 +63,19 @@ protected:
|
||||
|
||||
} // 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
|
||||
{
|
||||
public:
|
||||
explicit pattern_formatter(
|
||||
std::string pattern, pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
|
||||
using custom_flags = std::unordered_map<char, std::unique_ptr<custom_flag_formatter>>;
|
||||
|
||||
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
|
||||
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:
|
||||
std::unique_ptr<formatter> clone() const 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:
|
||||
std::string pattern_;
|
||||
std::string eol_;
|
||||
@ -84,6 +101,7 @@ private:
|
||||
std::tm cached_tm_;
|
||||
std::chrono::seconds last_log_secs_;
|
||||
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
|
||||
custom_flags custom_handlers_;
|
||||
|
||||
std::tm get_time_(const details::log_msg &msg);
|
||||
template<typename Padder>
|
||||
|
@ -246,6 +246,8 @@ TEST_CASE("paddinng_truncate_funcname", "[pattern_formatter]")
|
||||
REQUIRE(lines[1] == "message [funct]");
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST_CASE("clone-default-formatter", "[pattern_formatter]")
|
||||
{
|
||||
auto formatter_1 = std::make_shared<spdlog::pattern_formatter>();
|
||||
@ -305,6 +307,43 @@ TEST_CASE("clone-formatter-2", "[pattern_formatter]")
|
||||
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
|
||||
//
|
||||
@ -358,3 +397,16 @@ TEST_CASE("full filename formatter", "[pattern_formatter]")
|
||||
formatter.format(msg, formatted);
|
||||
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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user