Commit a1484bbe authored by Adrien Oliva's avatar Adrien Oliva

Merge branch '6-create-configuration-for-color' into 'master'

Resolve "Create configuration for color"

Closes #6

See merge request !6
parents 43451624 6a84af94
Pipeline #242 passed with stages
in 1 minute and 23 seconds
11 Adrien Oliva <olivaa+gitlab@yapbreak.fr>
14 Adrien Oliva <olivaa+gitlab@yapbreak.fr>
......@@ -4,6 +4,7 @@ __attribute__((destructor)) static void yaplog_static_deinit()
{
logger::unsetDestinationVariable();
logger::unsetLevelVariable();
logger::unsetColorVariable();
}
......@@ -17,6 +18,11 @@ void logger::setLevelVariable(const char *level)
InternalLog::setLevelVariable(level);
}
void logger::setColorVariable(const char *color)
{
InternalLog::setColorVariable(color);
}
void logger::unsetDestinationVariable()
{
InternalLog::unsetDestinationVariable();
......@@ -26,3 +32,8 @@ void logger::unsetLevelVariable()
{
InternalLog::unsetLevelVariable();
}
void logger::unsetColorVariable()
{
InternalLog::unsetColorVariable();
}
......@@ -96,8 +96,10 @@ namespace logger {
*/
void setDestinationVariable(const char *dest);
void setLevelVariable(const char *level);
void setColorVariable(const char *color);
void unsetDestinationVariable();
void unsetLevelVariable();
void unsetColorVariable();
};
#endif /* end of include guard: YAPLOG_H_UGV90DZC */
......@@ -8,6 +8,7 @@ using namespace logger;
char *InternalLog::s_destination = NULL;
char *InternalLog::s_level = NULL;
char *InternalLog::s_color = NULL;
InternalLog::InternalLog(log_level level, const log_location &loc,
std::ostream *destination,
......@@ -127,6 +128,11 @@ const char *InternalLog::getLevelVariable()
return s_level ? s_level : YAPLOG_LEVEL_DEFAULT;
}
const char *InternalLog::getColorVariable()
{
return s_color ? s_color : YAPLOG_COLOR_DEFAULT;
}
void InternalLog::setDestinationVariable(const char *dest)
{
unsetDestinationVariable();
......@@ -139,6 +145,12 @@ void InternalLog::setLevelVariable(const char *level)
s_level = strdup(level);
}
void InternalLog::setColorVariable(const char *color)
{
unsetColorVariable();
s_color = strdup(color);
}
void InternalLog::unsetDestinationVariable()
{
if (s_destination != NULL)
......@@ -152,3 +164,10 @@ void InternalLog::unsetLevelVariable()
free(static_cast<void *>(s_level));
s_level = NULL;
}
void InternalLog::unsetColorVariable()
{
if (s_color != NULL)
free(static_cast<void *>(s_color));
s_color = NULL;
}
......@@ -8,6 +8,7 @@
#define YAPLOG_LEVEL_DEFAULT "LOGLEVEL"
#define YAPLOG_DEST_DEFAULT "LOGDESTINATION"
#define YAPLOG_COLOR_DEFAULT "LOGCOLOR"
namespace logger {
......@@ -158,6 +159,12 @@ namespace logger {
* @return Level environment variable
*/
static const char *getLevelVariable();
/**
* @brief Get environment variable to check for log color to show.
*
* @return Color environment variable
*/
static const char *getColorVariable();
/**
* @brief Environment variable name of log destination
......@@ -168,6 +175,10 @@ namespace logger {
* @brief Environment variable name of log level configuration.
*/
static char *s_level;
/**
* @brief Environment variable name of log color configuration.
*/
static char *s_color;
public:
/**
......@@ -183,6 +194,12 @@ namespace logger {
*/
static void setLevelVariable(const char *level);
/**
* @brief Set environment variable to check for log color.
*
* @param color New color environement variable
*/
static void setColorVariable(const char *color);
/**
* @brief Unset environment variable to check for log file destination.
*/
static void unsetDestinationVariable();
......@@ -190,6 +207,10 @@ namespace logger {
* @brief Unset environment variable to check for log level.
*/
static void unsetLevelVariable();
/**
* @brief Unset environment variable to check for log color.
*/
static void unsetColorVariable();
};
/**
......
......@@ -490,12 +490,16 @@ TEST_GROUP(customconfvariable)
logger::setLevelVariable("MYCUSTOM_LEVEL");
setenv("MYCUSTOM_DEST", "custom_output.log", 1);
setenv("MYCUSTOM_LEVEL", "9", 1);
setenv("LOGLEVEL", "0", 1);
setenv("LOGDESTINATION", "stdout", 1);
}
void teardown()
{
unsetenv("LOGDESTINATION");
unsetenv("MYCUSTOM_DEST");
unsetenv("MYCUSTOM_LEVEL");
unsetenv("LOGLEVEL");
unsetenv("LOGDESTINATION");
logger::unsetDestinationVariable();
logger::unsetLevelVariable();
unlink("custom_output.log");
......@@ -503,6 +507,54 @@ TEST_GROUP(customconfvariable)
};
TEST(customconfvariable, alllogs)
{
givelog(log_level::fatal) << "Fatal";
givelog(log_level::alert) << "Alert";
givelog(log_level::crit) << "Crit";
givelog(log_level::error) << "Error";
logger::unsetLevelVariable();
givelog(log_level::warn) << "Warning";
givelog(log_level::notice) << "Notice";
logger::setLevelVariable("MYCUSTOM_LEVEL");
givelog(log_level::info) << "Info";
logger::unsetDestinationVariable();
givelog(log_level::debug) << "Debug";
logger::setDestinationVariable("MYCUSTOM_DEST");
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"
"[I] file2:38(function2) Info\n"
"[T] file2:38(function2) Trace\n"
);
check_file_content("custom_output.log", expected);
};
TEST_GROUP(colorconf)
{
void setup()
{
logger::setColorVariable("MYCUSTOM_COLOR");
setenv("LOGDESTINATION", "color_output.log", 1);
setenv("LOGLEVEL", "9", 1);
}
void teardown()
{
unsetenv("MYCUSTOM_COLOR");
logger::unsetColorVariable();
unlink("color_output.log");
}
void enablecolor(const char *value)
{
setenv("MYCUSTOM_COLOR", value, 1);
}
};
TEST(colorconf, alllogs_defaultcolor_settings)
{
givelog(log_level::fatal) << "Fatal";
givelog(log_level::alert) << "Alert";
......@@ -523,8 +575,85 @@ TEST(customconfvariable, alllogs)
"[D] file2:38(function2) Debug\n"
"[T] file2:38(function2) Trace\n"
);
check_file_content("custom_output.log", expected);
check_file_content("color_output.log", expected);
};
TEST(colorconf, alllogs_color_off_settings)
{
enablecolor("0");
givelog(log_level::fatal) << "Fatal";
givelog(log_level::alert) << "Alert";
givelog(log_level::crit) << "Crit";
givelog(log_level::error) << "Error";
givelog(log_level::warn) << "Warning";
givelog(log_level::notice) << "Notice";
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"
);
check_file_content("color_output.log", expected);
};
TEST(colorconf, alllogs_color_on_settings)
{
enablecolor("1");
givelog(log_level::fatal) << "Fatal";
givelog(log_level::alert) << "Alert";
givelog(log_level::crit) << "Crit";
givelog(log_level::error) << "Error";
givelog(log_level::warn) << "Warning";
givelog(log_level::notice) << "Notice";
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"
);
check_file_content("color_output.log", expected);
};
TEST(colorconf, alllogs_color_unauth_values_settings)
{
enablecolor("2");
givelog(log_level::fatal) << "Fatal";
enablecolor("-1");
givelog(log_level::alert) << "Alert";
enablecolor("foo");
givelog(log_level::crit) << "Crit";
enablecolor("bar");
givelog(log_level::error) << "Error";
givelog(log_level::warn) << "Warning";
givelog(log_level::notice) << "Notice";
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"
);
check_file_content("color_output.log", expected);
};
#endif /* end of include guard: UT_LIBYAPLOG_H_SHF9IC0C */
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