From 683ce7acc6bc2adcc05572fab0a58c5e3abb99b3 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 9 Dec 2016 12:22:58 +0100 Subject: [PATCH] refactor(logger): Cleanup --- include/components/logger.hpp | 8 +++----- src/components/logger.cpp | 25 +++++++++++-------------- src/main.cpp | 8 ++++---- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/include/components/logger.hpp b/include/components/logger.hpp index d712f680..d787f6fe 100644 --- a/include/components/logger.hpp +++ b/include/components/logger.hpp @@ -16,18 +16,16 @@ enum class loglevel : uint8_t { TRACE, }; -loglevel parse_loglevel_name(const string& name); - class logger { public: using make_type = const logger&; static make_type make(loglevel level = loglevel::NONE); explicit logger(loglevel level); - explicit logger(string level_name) : logger(parse_loglevel_name(level_name)) {} - void verbosity(loglevel level); - void verbosity(string level); + static loglevel parse_verbosity(const string& name, loglevel fallback = loglevel::NONE); + + void verbosity(loglevel&& level); #ifdef DEBUG_LOGGER // {{{ template diff --git a/src/components/logger.cpp b/src/components/logger.cpp index 80989fa5..f331b14e 100644 --- a/src/components/logger.cpp +++ b/src/components/logger.cpp @@ -11,8 +11,12 @@ POLYBAR_NS * Create instance */ logger::make_type logger::make(loglevel level) { - auto instance = factory_util::singleton(level); - return static_cast(*instance); +#ifndef DEBUG + if (level == loglevel::TRACE) { + throw application_error("not a debug build: trace disabled..."); + } +#endif + return static_cast(*factory_util::singleton(level)); } /** @@ -45,26 +49,19 @@ logger::logger(loglevel level) : m_level(level) { /** * Set output verbosity */ -void logger::verbosity(loglevel level) { +void logger::verbosity(loglevel&& level) { #ifndef DEBUG if (level == loglevel::TRACE) { - throw application_error("not a debug build: trace disabled..."); + throw application_error("Not a debug build; trace disabled..."); } #endif - m_level = level; -} - -/** - * Set output verbosity by loglevel name - */ -void logger::verbosity(string level) { - verbosity(parse_loglevel_name(move(level))); + m_level = forward(level); } /** * Convert given loglevel name to its enum type counterpart */ -loglevel parse_loglevel_name(const string& name) { +loglevel logger::parse_verbosity(const string& name, loglevel fallback) { if (string_util::compare(name, "error")) { return loglevel::ERROR; } else if (string_util::compare(name, "warning")) { @@ -74,7 +71,7 @@ loglevel parse_loglevel_name(const string& name) { } else if (string_util::compare(name, "trace")) { return loglevel::TRACE; } else { - return loglevel::NONE; + return fallback; } } diff --git a/src/main.cpp b/src/main.cpp index 0a7af4b9..77d7d0e8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,9 +22,9 @@ struct exit_failure {}; int main(int argc, char** argv) { // clang-format off const command_line::options opts{ - command_line::option{"-h", "--help", "Show help options"}, + command_line::option{"-h", "--help", "Show help options"}, command_line::option{"-v", "--version", "Print version information"}, - command_line::option{"-l", "--log", "Set the logging verbosity (default: WARNING)", "LEVEL", {"warning", "info", "trace"}}, + command_line::option{"-l", "--log", "Set the logging verbosity (default: WARNING)", "LEVEL", {"error", "warning", "info", "trace"}}, command_line::option{"-q", "--quiet", "Be quiet (will override -l)"}, command_line::option{"-c", "--config", "Path to the configuration file", "FILE"}, command_line::option{"-r", "--reload", "Reload when the configuration has been modified"}, @@ -37,7 +37,7 @@ int main(int argc, char** argv) { uint8_t exit_code{EXIT_SUCCESS}; bool reload{false}; - logger& logger{const_cast(logger::make(loglevel::WARNING))}; + logger& logger{const_cast(logger::make(loglevel::WARNING))}; try { //================================================== @@ -77,7 +77,7 @@ int main(int argc, char** argv) { if (cli->has("quiet")) { logger.verbosity(loglevel::ERROR); } else if (cli->has("log")) { - logger.verbosity(cli->get("log")); + logger.verbosity(logger::parse_verbosity(cli->get("log"))); } if (cli->has("help")) {