polybar-dwm/src/components/logger.cpp

90 lines
2.4 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"
2017-01-19 04:38:42 +00:00
#include "settings.hpp"
2016-12-23 04:18:58 +00:00
#include "utils/concurrency.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-23 04:18:58 +00:00
/**
* Convert string
*/
2016-12-31 03:32:11 +00:00
const char* logger::convert(string arg) const { // NOLINT
2016-12-23 04:18:58 +00:00
return arg.c_str();
}
/**
* Convert thread id
*/
size_t logger::convert(const std::thread::id arg) const {
return concurrency_util::thread_id(arg);
}
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) {
return *factory_util::singleton<std::remove_reference_t<logger::make_type>>(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 {
2016-12-14 04:42:46 +00:00
m_prefixes.emplace(make_pair(loglevel::TRACE, "polybar|trace: "));
m_prefixes.emplace(make_pair(loglevel::INFO, "polybar|info: "));
m_prefixes.emplace(make_pair(loglevel::WARNING, "polybar|warn: "));
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) {
2017-01-19 04:38:42 +00:00
#ifndef DEBUG_LOGGER
2016-11-25 12:55:15 +00:00
if (level == loglevel::TRACE) {
2017-01-19 04:38:42 +00:00
throw application_error("Trace logging is not enabled...");
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