2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdio>
|
2016-12-05 19:41:00 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2016-12-23 04:18:58 +00:00
|
|
|
#include <thread>
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
#include "common.hpp"
|
2017-01-20 01:25:54 +00:00
|
|
|
#include "settings.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2017-01-11 04:10:51 +00:00
|
|
|
#ifndef STDOUT_FILENO
|
|
|
|
#define STDOUT_FILENO 1
|
|
|
|
#endif
|
|
|
|
#ifndef STDERR_FILENO
|
|
|
|
#define STDERR_FILENO 2
|
|
|
|
#endif
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2017-01-19 10:11:28 +00:00
|
|
|
enum class loglevel {
|
2016-06-15 03:32:35 +00:00
|
|
|
NONE = 0,
|
|
|
|
ERROR,
|
|
|
|
WARNING,
|
|
|
|
INFO,
|
|
|
|
TRACE,
|
|
|
|
};
|
|
|
|
|
|
|
|
class logger {
|
|
|
|
public:
|
2016-12-09 08:40:46 +00:00
|
|
|
using make_type = const logger&;
|
|
|
|
static make_type make(loglevel level = loglevel::NONE);
|
2016-12-09 08:02:47 +00:00
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
explicit logger(loglevel level);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-09 11:22:58 +00:00
|
|
|
static loglevel parse_verbosity(const string& name, loglevel fallback = loglevel::NONE);
|
|
|
|
|
|
|
|
void verbosity(loglevel&& level);
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-05 19:41:00 +00:00
|
|
|
#ifdef DEBUG_LOGGER // {{{
|
2016-06-15 03:32:35 +00:00
|
|
|
template <typename... Args>
|
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...);
|
|
|
|
}
|
2017-01-19 04:38:42 +00:00
|
|
|
#ifdef DEBUG_LOGGER_VERBOSE
|
2016-10-25 11:10:34 +00:00
|
|
|
template <typename... Args>
|
|
|
|
void trace_x(string message, Args... args) const {
|
|
|
|
output(loglevel::TRACE, message, args...);
|
|
|
|
}
|
|
|
|
#else
|
2016-12-05 19:41:00 +00:00
|
|
|
template <typename... Args>
|
|
|
|
void trace_x(Args...) const {}
|
2016-10-25 11:10:34 +00:00
|
|
|
#endif
|
2016-12-05 19:41:00 +00:00
|
|
|
#else
|
|
|
|
template <typename... Args>
|
|
|
|
void trace(Args...) const {}
|
|
|
|
template <typename... Args>
|
|
|
|
void trace_x(Args...) const {}
|
|
|
|
#endif // }}}
|
2016-10-25 11:10:34 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2016-12-23 04:18:58 +00:00
|
|
|
* Convert string
|
2016-06-15 03:32:35 +00:00
|
|
|
*/
|
2016-12-23 14:54:06 +00:00
|
|
|
const char* convert(string arg) const; // NOLINT
|
2016-12-23 04:18:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert thread id
|
|
|
|
*/
|
|
|
|
size_t convert(const std::thread::id arg) const;
|
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 {
|
2016-12-05 19:41:00 +00:00
|
|
|
if (level > m_level) {
|
2016-06-15 03:32:35 +00:00
|
|
|
return;
|
2016-12-05 19:41:00 +00:00
|
|
|
}
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-05 19:41:00 +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-12-05 19:41:00 +00:00
|
|
|
#endif // }}}
|
|
|
|
|
|
|
|
dprintf(m_fd, (m_prefixes.at(level) + format + m_suffixes.at(level) + "\n").c_str(), convert(values)...);
|
|
|
|
|
|
|
|
#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-12-05 19:41:00 +00:00
|
|
|
#endif // }}}
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Logger verbosity level
|
|
|
|
*/
|
2016-12-05 19:41:00 +00:00
|
|
|
loglevel m_level{loglevel::TRACE};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* File descriptor used when writing the log messages
|
|
|
|
*/
|
2016-12-05 19:41:00 +00:00
|
|
|
int m_fd{STDERR_FILENO};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loglevel specific prefixes
|
|
|
|
*/
|
2016-12-05 19:41:00 +00:00
|
|
|
std::map<loglevel, string> m_prefixes;
|
2016-06-15 03:32:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loglevel specific suffixes
|
|
|
|
*/
|
2016-12-05 19:41:00 +00:00
|
|
|
std::map<loglevel, string> m_suffixes;
|
2016-06-15 03:32:35 +00:00
|
|
|
};
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|