Commit bf5a619b authored by Adrien Oliva's avatar Adrien Oliva

feat(color): Add color

- Implement ANSI color codes and print them according to configuration.

GTLAB #7
parent a1484bbe
Pipeline #243 passed with stages
in 1 minute and 21 seconds
#ifndef COLOR_H_Z8GUW2TY
#define COLOR_H_Z8GUW2TY
#define ANSI_COLOR_FATAL "\x1b[101;97;1m"
#define ANSI_COLOR_ALERT "\x1b[105;97;1m"
#define ANSI_COLOR_CRITIC "\x1b[101;1m"
#define ANSI_COLOR_ERROR "\x1b[91;1m"
#define ANSI_COLOR_WARNING "\x1b[93;1m"
#define ANSI_COLOR_NOTICE "\x1b[94;1m"
#define ANSI_COLOR_INFORMATION "\x1b[92;1m"
#define ANSI_COLOR_DEBUG "\x1b[97;1m"
#define ANSI_COLOR_TRACE "\x1b[1m"
#define ANSI_COLOR_RESET "\x1b[0m"
#endif /* end of include guard: COLOR_H_Z8GUW2TY */
......@@ -4,6 +4,8 @@
#include <cstring>
#include <fstream>
#include "color.h"
using namespace logger;
char *InternalLog::s_destination = NULL;
......@@ -58,10 +60,13 @@ char InternalLog::char_from_level(enum log_level l)
void InternalLog::print_header()
{
(*m_output) << "[" << char_from_level(m_level) << "] "
bool color = getColor();
(*m_output) << color_start(color)
<< "[" << char_from_level(m_level) << "] "
<< m_location.m_file << ":"
<< m_location.m_line << "("
<< m_location.m_function << ") ";
<< m_location.m_function << ")"
<< color_end(color) << " ";
}
std::ostream *InternalLog::getOstream(const char *destination)
......@@ -171,3 +176,55 @@ void InternalLog::unsetColorVariable()
free(static_cast<void *>(s_color));
s_color = NULL;
}
bool InternalLog::getColor()
{
const char *color = std::getenv(getColorVariable());
if (color == NULL) {
return false;
} else {
try {
int value = std::stoi(color);
return (value == 1);
} catch (std::exception &) {
return false;
}
}
}
const char *InternalLog::color_start(bool color)
{
if (!color)
return "";
switch (m_level) {
case fatal:
return ANSI_COLOR_FATAL;
case alert:
return ANSI_COLOR_ALERT;
case crit:
return ANSI_COLOR_CRITIC;
case error:
return ANSI_COLOR_ERROR;
case warn:
return ANSI_COLOR_WARNING;
case notice:
return ANSI_COLOR_NOTICE;
case info:
return ANSI_COLOR_INFORMATION;
case debug:
return ANSI_COLOR_DEBUG;
case trace:
return ANSI_COLOR_TRACE;
default:
return "";
}
}
const char *InternalLog::color_end(bool color)
{
if (color)
return ANSI_COLOR_RESET;
else
return "";
}
......@@ -77,6 +77,24 @@ namespace logger {
* - function is the function name where log append.
*/
void print_header();
/**
* @brief Depending on color configuration flag, get ANSI escape
* code of current log level.
*
* @param color Color configuration flag.
*
* @return ANSI escape sequence.
*/
const char *color_start(bool color);
/**
* @brief Depending on color configuration flag, get ANSI escape
* code to reset color.
*
* @param color Color configuration flag.
*
* @return ANSI escape reset sequence.
*/
const char *color_end(bool color);
protected:
/**
......@@ -88,6 +106,12 @@ namespace logger {
* environment variable given by `destination`.
*/
static std::ostream *getOstream(const char *destination);
/**
* @brief Get configured color use.
*
* @return Flag to know if color needs to be used.
*/
static bool getColor();
public:
/**
......
......@@ -615,15 +615,15 @@ TEST(colorconf, alllogs_color_on_settings)
givelog(log_level::info) << "Info";
givelog(log_level::debug) << "Debug";
givelog(log_level::trace) << "Trace";
std::string expected("[F] file2:38(function2) Fatal\n"
"[A] file2:38(function2) Alert\n"
"[C] file2:38(function2) Crit\n"
"[E] file2:38(function2) Error\n"
"[W] file2:38(function2) Warning\n"
"[N] file2:38(function2) Notice\n"
"[I] file2:38(function2) Info\n"
"[D] file2:38(function2) Debug\n"
"[T] file2:38(function2) Trace\n"
std::string expected("\x1b[101;97;1m[F] file2:38(function2)\x1b[0m Fatal\n"
"\x1b[105;97;1m[A] file2:38(function2)\x1b[0m Alert\n"
"\x1b[101;1m[C] file2:38(function2)\x1b[0m Crit\n"
"\x1b[91;1m[E] file2:38(function2)\x1b[0m Error\n"
"\x1b[93;1m[W] file2:38(function2)\x1b[0m Warning\n"
"\x1b[94;1m[N] file2:38(function2)\x1b[0m Notice\n"
"\x1b[92;1m[I] file2:38(function2)\x1b[0m Info\n"
"\x1b[97;1m[D] file2:38(function2)\x1b[0m Debug\n"
"\x1b[1m[T] file2:38(function2)\x1b[0m Trace\n"
);
check_file_content("color_output.log", expected);
};
......
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