cleanup(logger): use universal references to avoid useless copies

This commit is contained in:
Jérôme BOULMIER 2019-05-15 20:42:29 -04:00 committed by Patrick Ziegler
parent 982a22cd62
commit 51f9f35599
3 changed files with 20 additions and 19 deletions

View File

@ -4,6 +4,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <thread> #include <thread>
#include <utility>
#include "common.hpp" #include "common.hpp"
#include "settings.hpp" #include "settings.hpp"
@ -34,51 +35,51 @@ class logger {
static loglevel parse_verbosity(const string& name, loglevel fallback = loglevel::NONE); static loglevel parse_verbosity(const string& name, loglevel fallback = loglevel::NONE);
void verbosity(loglevel&& level); void verbosity(loglevel level);
#ifdef DEBUG_LOGGER // {{{ #ifdef DEBUG_LOGGER // {{{
template <typename... Args> template <typename... Args>
void trace(string message, Args... args) const { void trace(const string& message, Args&&... args) const {
output(loglevel::TRACE, message, args...); output(loglevel::TRACE, message, std::forward<Args>(args)...);
} }
#ifdef DEBUG_LOGGER_VERBOSE #ifdef DEBUG_LOGGER_VERBOSE
template <typename... Args> template <typename... Args>
void trace_x(string message, Args... args) const { void trace_x(const string& message, Args&&... args) const {
output(loglevel::TRACE, message, args...); output(loglevel::TRACE, message, std::forward<Args>(args)...);
} }
#else #else
template <typename... Args> template <typename... Args>
void trace_x(Args...) const {} void trace_x(Args&&...) const {}
#endif #endif
#else #else
template <typename... Args> template <typename... Args>
void trace(Args...) const {} void trace(Args&&...) const {}
template <typename... Args> template <typename... Args>
void trace_x(Args...) const {} void trace_x(Args&&...) const {}
#endif // }}} #endif // }}}
/** /**
* Output an info message * Output an info message
*/ */
template <typename... Args> template <typename... Args>
void info(string message, Args... args) const { void info(const string& message, Args&&... args) const {
output(loglevel::INFO, message, args...); output(loglevel::INFO, message, std::forward<Args>(args)...);
} }
/** /**
* Output a warning message * Output a warning message
*/ */
template <typename... Args> template <typename... Args>
void warn(string message, Args... args) const { void warn(const string& message, Args&&... args) const {
output(loglevel::WARNING, message, args...); output(loglevel::WARNING, message, std::forward<Args>(args)...);
} }
/** /**
* Output an error message * Output an error message
*/ */
template <typename... Args> template <typename... Args>
void err(string message, Args... args) const { void err(const string& message, Args&&... args) const {
output(loglevel::ERROR, message, args...); output(loglevel::ERROR, message, std::forward<Args>(args)...);
} }
protected: protected:
@ -95,14 +96,14 @@ class logger {
/** /**
* Convert thread id * Convert thread id
*/ */
size_t convert(const std::thread::id arg) const; size_t convert(std::thread::id arg) const;
/** /**
* Write the log message to the output channel * Write the log message to the output channel
* if the defined verbosity level allows it * if the defined verbosity level allows it
*/ */
template <typename... Args> template <typename... Args>
void output(loglevel level, string format, Args... values) const { void output(loglevel level, const string& format, Args&&... values) const {
if (level > m_level) { if (level > m_level) {
return; return;
} }

View File

@ -60,13 +60,13 @@ logger::logger(loglevel level) : m_level(level) {
/** /**
* Set output verbosity * Set output verbosity
*/ */
void logger::verbosity(loglevel&& level) { void logger::verbosity(loglevel level) {
#ifndef DEBUG_LOGGER #ifndef DEBUG_LOGGER
if (level == loglevel::TRACE) { if (level == loglevel::TRACE) {
throw application_error("Trace logging is not enabled..."); throw application_error("Trace logging is not enabled...");
} }
#endif #endif
m_level = forward<decltype(level)>(level); m_level = level;
} }
/** /**

View File

@ -56,7 +56,7 @@ namespace modules {
// Warn about "unreachable" format tag // Warn about "unreachable" format tag
if (m_formatter->has(TAG_LABEL_UNMOUNTED) && m_remove_unmounted) { if (m_formatter->has(TAG_LABEL_UNMOUNTED) && m_remove_unmounted) {
m_log.warn("%s: Defined format tag \"%s\" will never be used (reason: `remove-unmounted = true`)", name(), m_log.warn("%s: Defined format tag \"%s\" will never be used (reason: `remove-unmounted = true`)", name(),
TAG_LABEL_UNMOUNTED); string{TAG_LABEL_UNMOUNTED});
} }
} }