1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/src/components/logger.cpp

90 lines
2.4 KiB
C++
Raw Normal View History

2016-11-20 17:04:31 -05:00
#include <unistd.h>
2016-11-02 15:22:45 -04:00
#include "components/logger.hpp"
2016-11-25 07:55:15 -05:00
#include "errors.hpp"
2017-01-18 23:38:42 -05:00
#include "settings.hpp"
2016-12-22 23:18:58 -05:00
#include "utils/concurrency.hpp"
2016-12-09 03:02:47 -05:00
#include "utils/factory.hpp"
2016-11-02 15:22:45 -04:00
#include "utils/string.hpp"
2016-11-19 00:22:44 -05:00
POLYBAR_NS
2016-11-02 15:22:45 -04:00
2016-12-22 23:18:58 -05:00
/**
* Convert string
*/
2016-12-30 22:32:11 -05:00
const char* logger::convert(string arg) const { // NOLINT
2016-12-22 23:18:58 -05: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 03:02:47 -05:00
/**
* Create instance
*/
2016-12-09 03:40:46 -05:00
logger::make_type logger::make(loglevel level) {
return *factory_util::singleton<std::remove_reference_t<logger::make_type>>(level);
2016-12-09 03:02:47 -05:00
}
2016-11-02 15:22:45 -04:00
/**
* Construct logger
*/
logger::logger(loglevel level) : m_level(level) {
// clang-format off
2016-11-02 15:22:45 -04:00
if (isatty(m_fd)) {
m_prefixes[loglevel::TRACE] = "\r\033[0;32m- \033[0m";
2016-11-02 15:22:45 -04:00
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-13 23:42:46 -05: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 15:22:45 -04:00
}
// clang-format on
2016-11-02 15:22:45 -04:00
}
/**
* Set output verbosity
*/
2016-12-09 06:22:58 -05:00
void logger::verbosity(loglevel&& level) {
2017-01-18 23:38:42 -05:00
#ifndef DEBUG_LOGGER
2016-11-25 07:55:15 -05:00
if (level == loglevel::TRACE) {
2017-01-18 23:38:42 -05:00
throw application_error("Trace logging is not enabled...");
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
#endif
2016-12-09 06:22:58 -05:00
m_level = forward<decltype(level)>(level);
2016-11-02 15:22:45 -04:00
}
/**
* Convert given loglevel name to its enum type counterpart
*/
2016-12-09 06:22:58 -05:00
loglevel logger::parse_verbosity(const string& name, loglevel fallback) {
2016-11-25 07:55:15 -05:00
if (string_util::compare(name, "error")) {
2016-11-02 15:22:45 -04:00
return loglevel::ERROR;
2016-11-25 07:55:15 -05:00
} else if (string_util::compare(name, "warning")) {
2016-11-02 15:22:45 -04:00
return loglevel::WARNING;
2016-11-25 07:55:15 -05:00
} else if (string_util::compare(name, "info")) {
2016-11-02 15:22:45 -04:00
return loglevel::INFO;
2016-11-25 07:55:15 -05:00
} else if (string_util::compare(name, "trace")) {
2016-11-02 15:22:45 -04:00
return loglevel::TRACE;
2016-11-25 07:55:15 -05:00
} else {
2016-12-09 06:22:58 -05:00
return fallback;
2016-11-25 07:55:15 -05:00
}
2016-11-03 13:19:47 -04:00
}
2016-11-02 15:22:45 -04:00
2016-11-19 00:22:44 -05:00
POLYBAR_NS_END