polybar-dwm/src/components/logger.cpp

74 lines
2.2 KiB
C++
Raw Normal View History

2016-11-20 22:04:31 +00:00
#include <unistd.h>
2016-11-02 19:22:45 +00:00
#include "components/logger.hpp"
2016-11-25 12:55:15 +00:00
#include "errors.hpp"
2016-12-09 08:02:47 +00:00
#include "utils/factory.hpp"
2016-11-02 19:22:45 +00:00
#include "utils/string.hpp"
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
2016-12-09 08:02:47 +00:00
/**
* Create instance
*/
2016-12-09 08:40:46 +00:00
logger::make_type logger::make(loglevel level) {
2016-12-09 11:22:58 +00:00
return static_cast<const logger&>(*factory_util::singleton<const logger>(level));
2016-12-09 08:02:47 +00:00
}
2016-11-02 19:22:45 +00:00
/**
* Construct logger
*/
logger::logger(loglevel level) : m_level(level) {
// clang-format off
2016-11-02 19:22:45 +00:00
if (isatty(m_fd)) {
m_prefixes[loglevel::TRACE] = "\r\033[0;90m- ";
m_prefixes[loglevel::INFO] = "\r\033[1;32m* \033[0m";
m_prefixes[loglevel::WARNING] = "\r\033[1;33mwarn: \033[0m";
m_prefixes[loglevel::ERROR] = "\r\033[1;31merror: \033[0m";
m_suffixes[loglevel::TRACE] = "\033[0m";
m_suffixes[loglevel::INFO] = "\033[0m";
m_suffixes[loglevel::WARNING] = "\033[0m";
m_suffixes[loglevel::ERROR] = "\033[0m";
} else {
m_prefixes.emplace(make_pair(loglevel::TRACE, "polybar|trace "));
m_prefixes.emplace(make_pair(loglevel::INFO, "polybar|infoe "));
m_prefixes.emplace(make_pair(loglevel::WARNING, "polybar|warne "));
m_prefixes.emplace(make_pair(loglevel::ERROR, "polybar|error "));
m_suffixes.emplace(make_pair(loglevel::TRACE, ""));
m_suffixes.emplace(make_pair(loglevel::INFO, ""));
m_suffixes.emplace(make_pair(loglevel::WARNING, ""));
m_suffixes.emplace(make_pair(loglevel::ERROR, ""));
2016-11-02 19:22:45 +00:00
}
// clang-format on
2016-11-02 19:22:45 +00:00
}
/**
* Set output verbosity
*/
2016-12-09 11:22:58 +00:00
void logger::verbosity(loglevel&& level) {
2016-11-02 19:22:45 +00:00
#ifndef DEBUG
2016-11-25 12:55:15 +00:00
if (level == loglevel::TRACE) {
2016-12-09 11:43:31 +00:00
throw application_error("Trace logging is only enabled for debug builds...");
2016-11-25 12:55:15 +00:00
}
2016-11-02 19:22:45 +00:00
#endif
2016-12-09 11:22:58 +00:00
m_level = forward<decltype(level)>(level);
2016-11-02 19:22:45 +00:00
}
/**
* Convert given loglevel name to its enum type counterpart
*/
2016-12-09 11:22:58 +00:00
loglevel logger::parse_verbosity(const string& name, loglevel fallback) {
2016-11-25 12:55:15 +00:00
if (string_util::compare(name, "error")) {
2016-11-02 19:22:45 +00:00
return loglevel::ERROR;
2016-11-25 12:55:15 +00:00
} else if (string_util::compare(name, "warning")) {
2016-11-02 19:22:45 +00:00
return loglevel::WARNING;
2016-11-25 12:55:15 +00:00
} else if (string_util::compare(name, "info")) {
2016-11-02 19:22:45 +00:00
return loglevel::INFO;
2016-11-25 12:55:15 +00:00
} else if (string_util::compare(name, "trace")) {
2016-11-02 19:22:45 +00:00
return loglevel::TRACE;
2016-11-25 12:55:15 +00:00
} else {
2016-12-09 11:22:58 +00:00
return fallback;
2016-11-25 12:55:15 +00:00
}
2016-11-03 17:19:47 +00:00
}
2016-11-02 19:22:45 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END