Added copy ctor and assignment to logger

This commit is contained in:
gabime 2019-06-15 19:46:41 +03:00
parent 9d3aa5a253
commit 09cb45001b
2 changed files with 221 additions and 167 deletions

View File

@ -11,8 +11,58 @@
#include "spdlog/details/pattern_formatter.h"
namespace spdlog {
// public methods
SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, const char *msg)
SPDLOG_INLINE logger::logger(const logger &other):
name_(other.name_),
sinks_(other.sinks_),
level_(other.level_.load(std::memory_order_relaxed)),
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
custom_err_handler_(other.custom_err_handler_)
{
}
SPDLOG_INLINE logger::logger(logger &&other):
name_(std::move(other.name_)),
sinks_(std::move(other.sinks_)),
level_(other.level_.load(std::memory_order_relaxed)),
flush_level_(other.flush_level_.load(std::memory_order_relaxed)),
custom_err_handler_(std::move(other.custom_err_handler_))
{
}
SPDLOG_INLINE logger &logger::operator=(logger other)
{
this->swap(other);
return *this;
}
SPDLOG_INLINE void logger::swap(spdlog::logger &other)
{
name_.swap(other.name_);
sinks_.swap(other.sinks_);
//swap level_
auto tmp = other.level_.load();
tmp = level_.exchange(tmp);
other.level_.store(tmp);
//swap flush level_
tmp = other.flush_level_.load();
tmp = flush_level_.exchange(tmp);
other.flush_level_.store(tmp);
custom_err_handler_.swap(other.custom_err_handler_);
}
SPDLOG_INLINE void swap(logger &a, logger &b)
{
a.swap(b);
}
void logger::log(source_loc loc, level::level_enum lvl, const char *msg)
{
if (!should_log(lvl))
{
@ -135,10 +185,8 @@ SPDLOG_INLINE void logger::set_error_handler(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>(std::move(logger_name), sinks_.begin(), sinks_.end());
cloned->set_level(this->level());
cloned->flush_on(this->flush_level());
cloned->set_error_handler(this->custom_err_handler_);
auto cloned = std::make_shared<logger>(*this);
cloned->name_ = std::move(logger_name);
return cloned;
}

View File

@ -56,8 +56,11 @@ public:
virtual ~logger() = default;
logger(const logger &) = delete;
logger &operator=(const logger &) = delete;
logger(const logger &other);
logger(logger &&other);
logger &operator=(logger other);
void swap(spdlog::logger &other);
template<typename... Args>
void log(source_loc loc, level::level_enum lvl, const char *fmt, const Args &... args)
@ -328,23 +331,26 @@ public:
virtual std::shared_ptr<logger> clone(std::string logger_name);
protected:
virtual void sink_it_(details::log_msg &msg);
std::string name_;
std::vector<sink_ptr> sinks_;
spdlog::level_t level_{spdlog::logger::default_level()};
spdlog::level_t flush_level_{level::off};
err_handler custom_err_handler_{nullptr};
virtual void sink_it_(details::log_msg &msg);
virtual void flush_();
bool should_flush_(const details::log_msg &msg);
// default error handler.
// print the error to stderr with the max rate of 1 message/minute.
void err_handler_(const std::string &msg);
const std::string name_;
std::vector<sink_ptr> sinks_;
spdlog::level_t level_{spdlog::logger::default_level()};
spdlog::level_t flush_level_{level::off};
err_handler custom_err_handler_{nullptr};
};
void swap(logger& a, logger& b);
} // namespace spdlog
#ifdef SPDLOG_HEADER_ONLY
#include "logger-inl.h"
#endif