2016-05-31 03:58:58 +00:00
|
|
|
#pragma once
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <mutex>
|
2016-05-31 03:58:58 +00:00
|
|
|
#include <unistd.h>
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
#include "exception.hpp"
|
|
|
|
#include "utils/streams.hpp"
|
|
|
|
|
|
|
|
#define LOGGER_FD STDERR_FILENO
|
|
|
|
|
|
|
|
#define LOGGER_TAG_FATAL isatty(LOGGER_FD) ? "\033[1;31mfatal\033[37m ** \033[0m" : "fatal ** "
|
|
|
|
#define LOGGER_TAG_ERR isatty(LOGGER_FD) ? "\033[1;31m err\033[37m ** \033[0m" : "error ** "
|
|
|
|
#define LOGGER_TAG_WARN isatty(LOGGER_FD) ? "\033[1;33m warn\033[37m ** \033[0m" : "warn ** "
|
|
|
|
#define LOGGER_TAG_INFO isatty(LOGGER_FD) ? "\033[1;36m info\033[37m ** \033[0m" : "info ** "
|
|
|
|
#define LOGGER_TAG_DEBUG isatty(LOGGER_FD) ? "\033[1;35mdebug\033[37m ** \033[0m" : "debug ** "
|
|
|
|
#define LOGGER_TAG_TRACE isatty(LOGGER_FD) ? "\033[1;32mtrace\033[37m ** \033[0m" : "trace ** "
|
|
|
|
|
|
|
|
#define log_error(s) get_logger()->error(s)
|
|
|
|
#define log_warning(s) get_logger()->warning(s)
|
|
|
|
#define log_info(s) get_logger()->info(s)
|
|
|
|
#define log_debug(s) get_logger()->debug(s)
|
2016-06-21 02:29:33 +00:00
|
|
|
#ifdef DEBUG
|
2016-05-19 14:41:06 +00:00
|
|
|
#define log_trace(s) get_logger()->trace(__FILE__, __FUNCTION__, __LINE__, s)
|
2016-06-28 02:58:32 +00:00
|
|
|
#define log_trace2(logger, s) logger->trace(__FILE__, __FUNCTION__, __LINE__, s)
|
2016-06-21 02:29:33 +00:00
|
|
|
#else
|
|
|
|
#define log_trace(s) if (0) {}
|
2016-06-28 02:58:32 +00:00
|
|
|
#define log_trace2(logger, s) if (0) {}
|
2016-06-21 02:29:33 +00:00
|
|
|
#endif
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
enum LogLevel
|
|
|
|
{
|
|
|
|
LEVEL_NONE = 1 << 0,
|
|
|
|
LEVEL_ERROR = 1 << 1,
|
|
|
|
LEVEL_WARNING = 1 << 2,
|
|
|
|
LEVEL_INFO = 1 << 4,
|
|
|
|
LEVEL_DEBUG = 1 << 8,
|
|
|
|
LEVEL_TRACE = 1 << 16,
|
2016-06-21 02:29:33 +00:00
|
|
|
#ifdef DEBUG
|
2016-05-19 14:41:06 +00:00
|
|
|
LEVEL_ALL = LEVEL_ERROR | LEVEL_WARNING | LEVEL_INFO | LEVEL_DEBUG | LEVEL_TRACE
|
2016-06-21 02:29:33 +00:00
|
|
|
#else
|
|
|
|
LEVEL_ALL = LEVEL_ERROR | LEVEL_WARNING | LEVEL_INFO | LEVEL_DEBUG
|
|
|
|
#endif
|
2016-05-19 14:41:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Logger
|
|
|
|
{
|
|
|
|
std::mutex mtx;
|
|
|
|
|
2016-06-20 12:01:20 +00:00
|
|
|
int level = LogLevel::LEVEL_ERROR | LogLevel::LEVEL_WARNING | LogLevel::LEVEL_INFO;
|
2016-05-19 14:41:06 +00:00
|
|
|
int fd = LOGGER_FD;
|
|
|
|
|
2016-06-21 02:29:33 +00:00
|
|
|
void output(std::string tag, std::string msg);
|
|
|
|
|
2016-05-19 14:41:06 +00:00
|
|
|
public:
|
|
|
|
Logger();
|
|
|
|
|
2016-06-21 02:29:33 +00:00
|
|
|
void set_level(int level);
|
2016-05-19 14:41:06 +00:00
|
|
|
void add_level(int level);
|
|
|
|
|
2016-06-21 02:29:33 +00:00
|
|
|
void fatal(std::string msg);
|
|
|
|
void error(std::string msg);
|
|
|
|
void warning(std::string msg);
|
|
|
|
void info(std::string msg);
|
|
|
|
void debug(std::string msg);
|
2016-05-19 14:41:06 +00:00
|
|
|
|
|
|
|
void fatal(int msg) { fatal(std::to_string(msg)); }
|
|
|
|
void error(int msg) { error(std::to_string(msg)); }
|
|
|
|
void warning(int msg) { warning(std::to_string(msg)); }
|
|
|
|
void info(int msg) { info(std::to_string(msg)); }
|
|
|
|
void debug(int msg) { debug(std::to_string(msg)); }
|
2016-06-21 02:29:33 +00:00
|
|
|
|
|
|
|
void trace(const char *file, const char *fn, int lineno, std::string msg);
|
2016-05-19 14:41:06 +00:00
|
|
|
void trace(const char *file, const char *fn, int lineno, int msg) {
|
|
|
|
trace(file, fn, lineno, std::to_string(msg));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-28 02:58:32 +00:00
|
|
|
std::shared_ptr<Logger> get_logger();
|