Commit 29952e3a authored by Ingo Heimbach's avatar Ingo Heimbach

Merge branch 'develop'

parents 86418230 3f44723b
Pipeline #1128 skipped
......@@ -4,18 +4,8 @@ namespace logger {
const LogLevel Logger::default_log_level_ = LogLevel::DEFAULT_LOG_LEVEL;
const LogLevel Logger::default_message_log_level_ = LogLevel::DEBUG;
const Logger::ColorCodes Logger::color_codes_ = {"\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m",
"\033[35m", "\033[36m", "\033[37m", "\033[90m", "\033[91m",
"\033[92m", "\033[93m", "\033[94m", "\033[95m", "\033[96m",
"\033[97m", "\033[5m", "\033[0m"};
const std::string Logger::log_level_to_string_[] = {
std::string(Logger::color_codes_.white) + "NONE" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_red) + Logger::color_codes_.blink + "CRITICAL" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_red) + "ERROR" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_yellow) + "WARNING" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_blue) + "INFO" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_green) + "DEBUG" + Logger::color_codes_.reset
};
Logger::ColorCodes Logger::color_codes_;
std::string Logger::log_level_to_string_[log_level_count];
std::ofstream Logger::devnull_stream_("/dev/null");
#ifndef NLOG
......
......@@ -9,6 +9,7 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <unistd.h>
namespace logger {
......@@ -16,6 +17,7 @@ namespace logger {
enum class LogLevel : std::int8_t {
NONE, CRITICAL, ERROR, WARNING, INFO, DEBUG
};
const unsigned int log_level_count = static_cast<unsigned int>(LogLevel::DEBUG) + 1;
class Logger {
struct ColorCodes {
......@@ -25,7 +27,9 @@ class Logger {
public:
Logger(LogLevel log_level=default_log_level_) :
log_level_(log_level), message_log_level_(default_message_log_level_) {}
log_level_(log_level), message_log_level_(default_message_log_level_) {
init_color_codes();
}
virtual ~Logger() = default;
Logger operator()(LogLevel message_log_level, std::string file_name="", unsigned int line_number=0) const {
......@@ -86,6 +90,31 @@ class Logger {
}
}
protected:
void init_color_codes() {
// file descriptor 1 is always stderr
if(isatty(1)) {
Logger::ColorCodes color_codes = {"\033[30m", "\033[31m", "\033[32m", "\033[33m", "\033[34m",
"\033[35m", "\033[36m", "\033[37m", "\033[90m", "\033[91m",
"\033[92m", "\033[93m", "\033[94m", "\033[95m", "\033[96m",
"\033[97m", "\033[5m", "\033[0m"};
color_codes_ = color_codes;
}
else {
Logger::ColorCodes color_codes = {"", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", ""};
color_codes_ = color_codes;
}
std::string log_level_to_string[] = {
std::string(Logger::color_codes_.white) + "NONE" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_red) + Logger::color_codes_.blink + "CRITICAL" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_red) + "ERROR" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_yellow) + "WARNING" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_blue) + "INFO" + Logger::color_codes_.reset,
std::string(Logger::color_codes_.light_green) + "DEBUG" + Logger::color_codes_.reset
};
std::move(log_level_to_string, log_level_to_string + log_level_count, log_level_to_string_);
}
private:
LogLevel log_level_;
......@@ -93,11 +122,10 @@ class Logger {
std::string file_name_;
unsigned int line_number_;
// TODO: Default log level should be configurable with a macro! (default: WARNING with NDEBUG set else DEBUG)
static const LogLevel default_log_level_;
static const LogLevel default_message_log_level_;
static const ColorCodes color_codes_;
static const std::string log_level_to_string_[];
static ColorCodes color_codes_;
static std::string log_level_to_string_[log_level_count];
static std::ofstream devnull_stream_;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment