mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 01:51:38 +08:00
Moved pattern formatter from spdlog/details to spdlog/
This commit is contained in:
parent
c6c517431f
commit
752d5685dc
@ -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 <my_formatter_flag> 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<custom_flag_formatter> clone() const override
|
||||
{
|
||||
return spdlog::details::make_unique<my_formatter_flag>();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void custom_flags_example()
|
||||
{
|
||||
|
||||
using spdlog::details::make_unique; //for pre c++14
|
||||
auto formatter = make_unique<spdlog::pattern_formatter>("[%+] [%*]");
|
||||
formatter->add_flag<my_formatter_flag>('*').recompile();
|
||||
spdlog::set_formatter(std::move(formatter));
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/details/periodic_worker.h>
|
||||
#include <spdlog/logger.h>
|
||||
#include <spdlog/details/pattern_formatter.h>
|
||||
#include <spdlog/pattern_formatter.h>
|
||||
|
||||
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
|
||||
// support for the default stdout color logger
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
#include <spdlog/sinks/sink.h>
|
||||
#include <spdlog/details/backtracer.h>
|
||||
#include <spdlog/details/pattern_formatter.h>
|
||||
#include <spdlog/pattern_formatter.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef SPDLOG_HEADER_ONLY
|
||||
#include <spdlog/details/pattern_formatter.h>
|
||||
#include <spdlog/pattern_formatter.h>
|
||||
#endif
|
||||
|
||||
#include <spdlog/details/fmt_helper.h>
|
@ -87,10 +87,10 @@ public:
|
||||
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)
|
||||
pattern_formatter &add_flag(char flag, const Args &... args)
|
||||
{
|
||||
custom_handlers_[flag] = details::make_unique<T>(args...);
|
||||
return *this;
|
||||
return *this;
|
||||
}
|
||||
void recompile();
|
||||
|
@ -8,7 +8,7 @@
|
||||
#endif
|
||||
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/details/pattern_formatter.h>
|
||||
#include <spdlog/pattern_formatter.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include "base_sink.h"
|
||||
#include <spdlog/details/log_msg.h>
|
||||
#include <spdlog/details/null_mutex.h>
|
||||
#include <spdlog/details/pattern_formatter.h>
|
||||
#include <spdlog/pattern_formatter.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
@ -8,7 +8,7 @@
|
||||
#endif
|
||||
|
||||
#include <spdlog/details/console_globals.h>
|
||||
#include <spdlog/details/pattern_formatter.h>
|
||||
#include <spdlog/pattern_formatter.h>
|
||||
#include <memory>
|
||||
|
||||
namespace spdlog {
|
||||
|
@ -8,7 +8,7 @@
|
||||
#endif
|
||||
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/details/pattern_formatter.h>
|
||||
#include <spdlog/pattern_formatter.h>
|
||||
|
||||
namespace spdlog {
|
||||
namespace sinks {
|
||||
|
@ -8,7 +8,7 @@
|
||||
#endif
|
||||
|
||||
#include <spdlog/common.h>
|
||||
#include <spdlog/details/pattern_formatter.h>
|
||||
#include <spdlog/pattern_formatter.h>
|
||||
|
||||
namespace spdlog {
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
#include <spdlog/details/backtracer-inl.h>
|
||||
#include <spdlog/details/registry-inl.h>
|
||||
#include <spdlog/details/os-inl.h>
|
||||
#include <spdlog/details/pattern_formatter-inl.h>
|
||||
#include <spdlog/pattern_formatter-inl.h>
|
||||
#include <spdlog/details/log_msg-inl.h>
|
||||
#include <spdlog/details/log_msg_buffer-inl.h>
|
||||
#include <spdlog/logger-inl.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"
|
||||
#include "spdlog/pattern_formatter.h"
|
@ -330,7 +330,7 @@ public:
|
||||
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();
|
||||
formatter_1->add_flag<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");
|
||||
@ -402,7 +402,7 @@ TEST_CASE("full filename formatter", "[pattern_formatter]")
|
||||
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();
|
||||
formatter->add_flag<custom_test_flag>('t', "custom1").add_flag<custom_test_flag>('u', "custom2").recompile();
|
||||
|
||||
memory_buf_t formatted;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user