2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
#include "common.hpp"
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "utils/factory.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
enum class loglevel {
|
|
|
|
NONE = 0,
|
|
|
|
ERROR,
|
|
|
|
WARNING,
|
|
|
|
INFO,
|
|
|
|
TRACE,
|
|
|
|
};
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
loglevel parse_loglevel_name(const string& name);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
class logger {
|
|
|
|
public:
|
2016-11-02 19:22:45 +00:00
|
|
|
explicit logger(loglevel level);
|
2016-06-15 03:32:35 +00:00
|
|
|
explicit logger(string level_name) : logger(parse_loglevel_name(level_name)) {}
|
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
void verbosity(loglevel level);
|
|
|
|
void verbosity(string level);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output a trace message
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
2016-11-02 19:22:45 +00:00
|
|
|
#ifdef DEBUG_LOGGER
|
2016-10-24 23:51:55 +00:00
|
|
|
void trace(string message, Args... args) const {
|
2016-06-15 03:32:35 +00:00
|
|
|
output(loglevel::TRACE, message, args...);
|
|
|
|
}
|
2016-10-24 23:51:55 +00:00
|
|
|
#else
|
2016-11-02 19:22:45 +00:00
|
|
|
#ifdef VERBOSE_TRACELOG
|
|
|
|
#undef VERBOSE_TRACELOG
|
|
|
|
#endif
|
|
|
|
void trace(string, Args...) const {
|
|
|
|
}
|
2016-10-24 23:51:55 +00:00
|
|
|
#endif
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-10-25 11:10:34 +00:00
|
|
|
/**
|
|
|
|
* Output extra verbose trace message
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
2016-11-02 19:22:45 +00:00
|
|
|
#ifdef VERBOSE_TRACELOG
|
2016-10-25 11:10:34 +00:00
|
|
|
void trace_x(string message, Args... args) const {
|
|
|
|
output(loglevel::TRACE, message, args...);
|
|
|
|
}
|
|
|
|
#else
|
2016-11-02 19:22:45 +00:00
|
|
|
void trace_x(string, Args...) const {
|
|
|
|
}
|
2016-10-25 11:10:34 +00:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 03:32:35 +00: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-10-25 18:14:44 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert string to const char*
|
|
|
|
*/
|
|
|
|
const char* convert(string arg) const {
|
|
|
|
return arg.c_str();
|
2016-10-25 18:14:44 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00: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)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto prefix = m_prefixes.find(level)->second;
|
|
|
|
auto suffix = m_suffixes.find(level)->second;
|
|
|
|
|
|
|
|
// silence the compiler
|
2016-10-25 18:14:44 +00:00
|
|
|
#if defined(__clang__)
|
2016-06-15 03:32:35 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wformat-security"
|
2016-10-25 20:47:21 +00:00
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-security"
|
2016-10-25 18:14:44 +00:00
|
|
|
#endif
|
2016-06-15 03:32:35 +00:00
|
|
|
dprintf(m_fd, (prefix + format + suffix + "\n").c_str(), convert(values)...);
|
2016-10-25 18:14:44 +00:00
|
|
|
#if defined(__clang__)
|
2016-06-15 03:32:35 +00:00
|
|
|
#pragma clang diagnostic pop
|
2016-10-25 20:47:21 +00:00
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic pop
|
2016-10-25 18:14:44 +00:00
|
|
|
#endif
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Logger verbosity level
|
|
|
|
*/
|
|
|
|
loglevel m_level = loglevel::TRACE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File descriptor used when writing the log messages
|
|
|
|
*/
|
2016-10-11 06:34:34 +00:00
|
|
|
int m_fd = STDERR_FILENO;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loglevel specific prefixes
|
|
|
|
*/
|
|
|
|
// clang-format off
|
|
|
|
map<loglevel, string> m_prefixes {
|
2016-11-19 05:22:44 +00:00
|
|
|
{loglevel::TRACE, "polybar|trace "},
|
|
|
|
{loglevel::INFO, "polybar|info "},
|
|
|
|
{loglevel::WARNING, "polybar|warn "},
|
|
|
|
{loglevel::ERROR, "polybar|error "},
|
2016-06-15 03:32:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loglevel specific suffixes
|
|
|
|
*/
|
|
|
|
map<loglevel, string> m_suffixes {
|
|
|
|
{loglevel::TRACE, ""},
|
|
|
|
{loglevel::INFO, ""},
|
|
|
|
{loglevel::WARNING, ""},
|
|
|
|
{loglevel::ERROR, ""},
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
};
|
|
|
|
|
2016-10-24 23:55:59 +00:00
|
|
|
namespace {
|
|
|
|
/**
|
|
|
|
* Configure injection module
|
|
|
|
*/
|
|
|
|
template <typename T = const logger&>
|
|
|
|
di::injector<T> configure_logger(loglevel level = loglevel::NONE) {
|
2016-11-20 22:04:31 +00:00
|
|
|
auto instance = factory_util::generic_singleton<logger>(level);
|
2016-10-24 23:55:59 +00:00
|
|
|
return di::make_injector(di::bind<>().to(instance));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|