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

82 lines
2.3 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"
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-09 03:02:47 -05:00
/**
* Create instance
*/
const logger& logger::make(loglevel level) {
auto instance = factory_util::singleton<const logger>(level);
return static_cast<const logger&>(*instance);
}
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;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 15:22:45 -04:00
}
// clang-format on
2016-11-02 15:22:45 -04:00
}
/**
* Set output verbosity
*/
void logger::verbosity(loglevel level) {
#ifndef DEBUG
2016-11-25 07:55:15 -05:00
if (level == loglevel::TRACE) {
2016-11-02 15:22:45 -04:00
throw application_error("not a debug build: trace disabled...");
2016-11-25 07:55:15 -05:00
}
2016-11-02 15:22:45 -04:00
#endif
m_level = level;
}
/**
* Set output verbosity by loglevel name
*/
void logger::verbosity(string level) {
2016-11-25 07:55:15 -05:00
verbosity(parse_loglevel_name(move(level)));
2016-11-02 15:22:45 -04:00
}
/**
* Convert given loglevel name to its enum type counterpart
*/
2016-11-25 07:55:15 -05:00
loglevel parse_loglevel_name(const string& name) {
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-11-02 15:22:45 -04:00
return loglevel::NONE;
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