refactor(logger): Cleanup

This commit is contained in:
Michael Carlberg 2016-12-09 12:22:58 +01:00
parent 540bcb9fd0
commit 683ce7acc6
3 changed files with 18 additions and 23 deletions

View File

@ -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 <typename... Args>

View File

@ -11,8 +11,12 @@ POLYBAR_NS
* Create instance
*/
logger::make_type logger::make(loglevel level) {
auto instance = factory_util::singleton<const logger>(level);
return static_cast<const logger&>(*instance);
#ifndef DEBUG
if (level == loglevel::TRACE) {
throw application_error("not a debug build: trace disabled...");
}
#endif
return static_cast<const logger&>(*factory_util::singleton<const logger>(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<decltype(level)>(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;
}
}

View File

@ -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<class logger&>(logger::make(loglevel::WARNING))};
logger& logger{const_cast<decltype(logger)>(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")) {