Optmize set_formatter to avoid redundant clone

This commit is contained in:
gabime 2019-06-06 18:19:36 +03:00
parent 03f0e2196e
commit e03c160e27

View File

@ -64,13 +64,21 @@ 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. redundant
// 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 &sink : sinks_) for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
{ {
sink->set_formatter(f->clone()); if (std::next(it) == sinks_.end())
{
// last element - we can be move it.
(*it)->set_formatter(std::move(f));
}
else
{
(*it)->set_formatter(f->clone());
}
} }
} }