1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-03 04:33:30 -05:00
polybar/include/components/logger.hpp

138 lines
2.8 KiB
C++
Raw Normal View History

2016-06-14 23:32:35 -04:00
#pragma once
#include <cstdio>
#include <map>
#include <string>
2016-06-14 23:32:35 -04:00
#include "common.hpp"
2016-11-19 00:22:44 -05:00
POLYBAR_NS
2016-06-14 23:32:35 -04:00
enum class loglevel : uint8_t {
2016-06-14 23:32:35 -04:00
NONE = 0,
ERROR,
WARNING,
INFO,
TRACE,
};
class logger {
public:
2016-12-09 03:40:46 -05:00
using make_type = const logger&;
static make_type make(loglevel level = loglevel::NONE);
2016-12-09 03:02:47 -05:00
2016-11-02 15:22:45 -04:00
explicit logger(loglevel level);
2016-06-14 23:32:35 -04:00
2016-12-09 06:22:58 -05:00
static loglevel parse_verbosity(const string& name, loglevel fallback = loglevel::NONE);
void verbosity(loglevel&& level);
2016-06-14 23:32:35 -04:00
#ifdef DEBUG_LOGGER // {{{
2016-06-14 23:32:35 -04:00
template <typename... Args>
void trace(string message, Args... args) const {
2016-06-14 23:32:35 -04:00
output(loglevel::TRACE, message, args...);
}
2016-11-02 15:22:45 -04:00
#ifdef VERBOSE_TRACELOG
template <typename... Args>
void trace_x(string message, Args... args) const {
output(loglevel::TRACE, message, args...);
}
#else
template <typename... Args>
void trace_x(Args...) const {}
#endif
#else
template <typename... Args>
void trace(Args...) const {}
template <typename... Args>
void trace_x(Args...) const {}
#endif // }}}
2016-06-14 23:32:35 -04:00
/**
* Output an info message
*/
template <typename... Args>
void info(string message, Args... args) const {
output(loglevel::INFO, message, args...);
}
/**
* Output a warning message
*/
template <typename... Args>
void warn(string message, Args... args) const {
output(loglevel::WARNING, message, args...);
}
/**
* Output an error message
*/
template <typename... Args>
void err(string message, Args... args) const {
output(loglevel::ERROR, message, args...);
}
protected:
template <typename T>
decltype(auto) convert(T&& arg) const {
return forward<T>(arg);
}
2016-06-14 23:32:35 -04:00
/**
* Convert string to const char*
*/
const char* convert(string arg) const {
return arg.c_str();
}
2016-06-14 23:32:35 -04:00
/**
* Write the log message to the output channel
* if the defined verbosity level allows it
*/
template <typename... Args>
void output(loglevel level, string format, Args... values) const {
if (level > m_level) {
2016-06-14 23:32:35 -04:00
return;
}
2016-06-14 23:32:35 -04:00
#if defined(__clang__) // {{{
2016-06-14 23:32:35 -04:00
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-security"
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
#endif // }}}
dprintf(m_fd, (m_prefixes.at(level) + format + m_suffixes.at(level) + "\n").c_str(), convert(values)...);
#if defined(__clang__) // {{{
2016-06-14 23:32:35 -04:00
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif // }}}
2016-06-14 23:32:35 -04:00
}
private:
/**
* Logger verbosity level
*/
loglevel m_level{loglevel::TRACE};
2016-06-14 23:32:35 -04:00
/**
* File descriptor used when writing the log messages
*/
int m_fd{STDERR_FILENO};
2016-06-14 23:32:35 -04:00
/**
* Loglevel specific prefixes
*/
std::map<loglevel, string> m_prefixes;
2016-06-14 23:32:35 -04:00
/**
* Loglevel specific suffixes
*/
std::map<loglevel, string> m_suffixes;
2016-06-14 23:32:35 -04:00
};
2016-11-19 00:22:44 -05:00
POLYBAR_NS_END