mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-26 02:21:34 +08:00
Added copy ctor and assignment to logger
This commit is contained in:
parent
9d3aa5a253
commit
09cb45001b
@ -11,9 +11,59 @@
|
|||||||
#include "spdlog/details/pattern_formatter.h"
|
#include "spdlog/details/pattern_formatter.h"
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
// public methods
|
|
||||||
SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, const char *msg)
|
// public methods
|
||||||
{
|
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))
|
if (!should_log(lvl))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@ -32,42 +82,42 @@ SPDLOG_INLINE void logger::log(source_loc loc, level::level_enum lvl, const char
|
|||||||
{
|
{
|
||||||
err_handler_("Unknown exception in logger");
|
err_handler_("Unknown exception in logger");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void logger::log(level::level_enum lvl, const char *msg)
|
SPDLOG_INLINE void logger::log(level::level_enum lvl, const char *msg)
|
||||||
{
|
{
|
||||||
log(source_loc{}, lvl, msg);
|
log(source_loc{}, lvl, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const
|
SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const
|
||||||
{
|
{
|
||||||
return msg_level >= level_.load(std::memory_order_relaxed);
|
return msg_level >= level_.load(std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
|
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
|
||||||
{
|
{
|
||||||
level_.store(log_level);
|
level_.store(log_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE level::level_enum logger::default_level()
|
SPDLOG_INLINE level::level_enum logger::default_level()
|
||||||
{
|
{
|
||||||
return static_cast<level::level_enum>(SPDLOG_ACTIVE_LEVEL);
|
return static_cast<level::level_enum>(SPDLOG_ACTIVE_LEVEL);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE level::level_enum logger::level() const
|
SPDLOG_INLINE level::level_enum logger::level() const
|
||||||
{
|
{
|
||||||
return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
|
return static_cast<level::level_enum>(level_.load(std::memory_order_relaxed));
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE const std::string &logger::name() const
|
SPDLOG_INLINE const std::string &logger::name() const
|
||||||
{
|
{
|
||||||
return name_;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set formatting for the sinks in this logger.
|
// set formatting for the sinks in this logger.
|
||||||
// each sink will get a seperate instance of the formatter object.
|
// each sink will get a seperate instance of the formatter object.
|
||||||
SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
|
SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
|
||||||
{
|
{
|
||||||
for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
|
for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
|
||||||
{
|
{
|
||||||
if (std::next(it) == sinks_.end())
|
if (std::next(it) == sinks_.end())
|
||||||
@ -80,17 +130,17 @@ SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
|
|||||||
(*it)->set_formatter(f->clone());
|
(*it)->set_formatter(f->clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
|
SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type)
|
||||||
{
|
{
|
||||||
auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
|
auto new_formatter = details::make_unique<pattern_formatter>(std::move(pattern), time_type);
|
||||||
set_formatter(std::move(new_formatter));
|
set_formatter(std::move(new_formatter));
|
||||||
}
|
}
|
||||||
|
|
||||||
// flush functions
|
// flush functions
|
||||||
SPDLOG_INLINE void logger::flush()
|
SPDLOG_INLINE void logger::flush()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
flush_();
|
flush_();
|
||||||
@ -103,48 +153,46 @@ SPDLOG_INLINE void logger::flush()
|
|||||||
{
|
{
|
||||||
err_handler_("Unknown exception in logger");
|
err_handler_("Unknown exception in logger");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void logger::flush_on(level::level_enum log_level)
|
SPDLOG_INLINE void logger::flush_on(level::level_enum log_level)
|
||||||
{
|
{
|
||||||
flush_level_.store(log_level);
|
flush_level_.store(log_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE level::level_enum logger::flush_level() const
|
SPDLOG_INLINE level::level_enum logger::flush_level() const
|
||||||
{
|
{
|
||||||
return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
|
return static_cast<level::level_enum>(flush_level_.load(std::memory_order_relaxed));
|
||||||
}
|
}
|
||||||
|
|
||||||
// sinks
|
// sinks
|
||||||
SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const
|
SPDLOG_INLINE const std::vector<sink_ptr> &logger::sinks() const
|
||||||
{
|
{
|
||||||
return sinks_;
|
return sinks_;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
|
SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
|
||||||
{
|
{
|
||||||
return sinks_;
|
return sinks_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// error handler
|
// error handler
|
||||||
SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
|
SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
|
||||||
{
|
{
|
||||||
custom_err_handler_ = handler;
|
custom_err_handler_ = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new logger with same sinks and configuration.
|
// create new logger with same sinks and configuration.
|
||||||
SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
|
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());
|
auto cloned = std::make_shared<logger>(*this);
|
||||||
cloned->set_level(this->level());
|
cloned->name_ = std::move(logger_name);
|
||||||
cloned->flush_on(this->flush_level());
|
|
||||||
cloned->set_error_handler(this->custom_err_handler_);
|
|
||||||
return cloned;
|
return cloned;
|
||||||
}
|
}
|
||||||
|
|
||||||
// protected methods
|
// protected methods
|
||||||
SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg)
|
SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg)
|
||||||
{
|
{
|
||||||
for (auto &sink : sinks_)
|
for (auto &sink : sinks_)
|
||||||
{
|
{
|
||||||
if (sink->should_log(msg.level))
|
if (sink->should_log(msg.level))
|
||||||
@ -157,24 +205,24 @@ SPDLOG_INLINE void logger::sink_it_(details::log_msg &msg)
|
|||||||
{
|
{
|
||||||
flush_();
|
flush_();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void logger::flush_()
|
SPDLOG_INLINE void logger::flush_()
|
||||||
{
|
{
|
||||||
for (auto &sink : sinks_)
|
for (auto &sink : sinks_)
|
||||||
{
|
{
|
||||||
sink->flush();
|
sink->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
|
SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
|
||||||
{
|
{
|
||||||
auto flush_level = flush_level_.load(std::memory_order_relaxed);
|
auto flush_level = flush_level_.load(std::memory_order_relaxed);
|
||||||
return (msg.level >= flush_level) && (msg.level != level::off);
|
return (msg.level >= flush_level) && (msg.level != level::off);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
||||||
{
|
{
|
||||||
if (custom_err_handler_)
|
if (custom_err_handler_)
|
||||||
{
|
{
|
||||||
custom_err_handler_(msg);
|
custom_err_handler_(msg);
|
||||||
@ -186,5 +234,5 @@ SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
|||||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
||||||
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
|
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
@ -56,8 +56,11 @@ public:
|
|||||||
|
|
||||||
virtual ~logger() = default;
|
virtual ~logger() = default;
|
||||||
|
|
||||||
logger(const logger &) = delete;
|
logger(const logger &other);
|
||||||
logger &operator=(const logger &) = delete;
|
logger(logger &&other);
|
||||||
|
logger &operator=(logger other);
|
||||||
|
|
||||||
|
void swap(spdlog::logger &other);
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void log(source_loc loc, level::level_enum lvl, const char *fmt, const Args &... 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);
|
virtual std::shared_ptr<logger> clone(std::string logger_name);
|
||||||
|
|
||||||
protected:
|
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_();
|
virtual void flush_();
|
||||||
bool should_flush_(const details::log_msg &msg);
|
bool should_flush_(const details::log_msg &msg);
|
||||||
|
|
||||||
// default error handler.
|
// default error handler.
|
||||||
// print the error to stderr with the max rate of 1 message/minute.
|
// print the error to stderr with the max rate of 1 message/minute.
|
||||||
void err_handler_(const std::string &msg);
|
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
|
} // namespace spdlog
|
||||||
|
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "logger-inl.h"
|
#include "logger-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user