Commit ed8d0996 authored by gabime's avatar gabime

Fixed #1197

parent b693d0cd
......@@ -83,3 +83,10 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
SPDLOG_LOGGER_CATCH()
}
}
SPDLOG_INLINE std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
{
auto cloned = std::make_shared<spdlog::async_logger>(*this);
cloned->name_ = std::move(new_name);
return cloned;
}
......@@ -49,6 +49,8 @@ public:
async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp,
async_overflow_policy overflow_policy = async_overflow_policy::block);
std::shared_ptr<logger> clone(std::string new_name) override;
protected:
void sink_it_(const details::log_msg &msg) override;
void flush_() override;
......
......@@ -122,7 +122,6 @@ struct formatter<spdlog::details::bytes_range<T>>
auto inserter = ctx.out();
#endif
for (auto &item : the_range)
{
auto ch = static_cast<unsigned char>(item);
......
......@@ -162,6 +162,14 @@ SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
custom_err_handler_ = handler;
}
// create new logger with same sinks and configuration.
SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
{
auto cloned = std::make_shared<logger>(*this);
cloned->name_ = std::move(logger_name);
return cloned;
}
// protected methods
SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
{
......
......@@ -356,6 +356,9 @@ public:
// error handler
void set_error_handler(err_handler);
// create new logger with same sinks and configuration.
virtual std::shared_ptr<logger> clone(std::string logger_name);
protected:
std::string name_;
std::vector<sink_ptr> sinks_;
......
#include "includes.h"
#include "test_sink.h"
#include "spdlog/fmt/bin_to_hex.h"
template<class T>
std::string log_info(const T &what, spdlog::level::level_enum logger_level = spdlog::level::info)
......@@ -92,7 +94,54 @@ TEST_CASE("periodic flush", "[periodic_flush]")
spdlog::drop_all();
}
#include "spdlog/fmt/bin_to_hex.h"
TEST_CASE("clone-logger", "[clone]")
{
using namespace spdlog;
auto test_sink = std::make_shared<sinks::test_sink_mt>();
auto logger = std::make_shared<spdlog::logger>("orig", test_sink);
logger->set_pattern("%v");
auto cloned = logger->clone("clone");
REQUIRE(cloned->name() == "clone");
REQUIRE(logger->sinks() == cloned->sinks());
REQUIRE(logger->level() == cloned->level());
REQUIRE(logger->flush_level() == cloned->flush_level());
logger->info("Some message 1");
cloned->info("Some message 2");
REQUIRE(test_sink->lines().size()==2);
REQUIRE(test_sink->lines()[0] == "Some message 1");
REQUIRE(test_sink->lines()[1] == "Some message 2");
spdlog::drop_all();
}
TEST_CASE("clone async", "[clone]")
{
using namespace spdlog;
spdlog::init_thread_pool(4, 1);
auto test_sink = std::make_shared<sinks::test_sink_st >();
auto logger = std::make_shared<spdlog::async_logger>("orig", test_sink, spdlog::thread_pool());
logger->set_pattern("%v");
auto cloned = logger->clone("clone");
REQUIRE(cloned->name() == "clone");
REQUIRE(logger->sinks() == cloned->sinks());
REQUIRE(logger->level() == cloned->level());
REQUIRE(logger->flush_level() == cloned->flush_level());
logger->info("Some message 1");
cloned->info("Some message 2");
spdlog::details::os::sleep_for_millis(10);
REQUIRE(test_sink->lines().size()==2);
REQUIRE(test_sink->lines()[0] == "Some message 1");
REQUIRE(test_sink->lines()[1] == "Some message 2");
spdlog::drop_all();
}
TEST_CASE("to_hex", "[to_hex]")
{
......
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