Commit 7f0dbf48 authored by Adrien Oliva's avatar Adrien Oliva

Merge branch '8-hide-code-location-information' into 'master'

Resolve "Hide code location information"

Closes #8

See merge request !8
parents 37a7aaca 13f7e677
Pipeline #252 passed with stages
in 1 minute and 20 seconds
......@@ -5,6 +5,7 @@ __attribute__((destructor)) static void yaplog_static_deinit()
logger::unsetDestinationVariable();
logger::unsetLevelVariable();
logger::unsetColorVariable();
logger::unsetInfoVariable();
}
......@@ -23,6 +24,11 @@ void logger::setColorVariable(const char *color)
InternalLog::setColorVariable(color);
}
void logger::setInfoVariable(const char *info)
{
InternalLog::setInfoVariable(info);
}
void logger::unsetDestinationVariable()
{
InternalLog::unsetDestinationVariable();
......@@ -37,3 +43,9 @@ void logger::unsetColorVariable()
{
InternalLog::unsetColorVariable();
}
void logger::unsetInfoVariable()
{
InternalLog::unsetInfoVariable();
}
......@@ -97,9 +97,11 @@ namespace logger {
void setDestinationVariable(const char *dest);
void setLevelVariable(const char *level);
void setColorVariable(const char *color);
void setInfoVariable(const char *info);
void unsetDestinationVariable();
void unsetLevelVariable();
void unsetColorVariable();
void unsetInfoVariable();
};
#endif /* end of include guard: YAPLOG_H_UGV90DZC */
......@@ -11,6 +11,7 @@ using namespace logger;
char *InternalLog::s_destination = NULL;
char *InternalLog::s_level = NULL;
char *InternalLog::s_color = NULL;
char *InternalLog::s_info = NULL;
InternalLog::InternalLog(log_level level, const log_location &loc,
std::ostream *destination,
......@@ -62,11 +63,13 @@ void InternalLog::print_header()
{
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 << ")"
<< color_end(color) << " ";
<< "[" << char_from_level(m_level) << "]";
if (getInfo()) {
(*m_output) << " " << m_location.m_file << ":"
<< m_location.m_line << "("
<< m_location.m_function << ")";
}
(*m_output) << color_end(color) << " ";
}
std::ostream *InternalLog::getOstream(const char *destination)
......@@ -138,6 +141,11 @@ const char *InternalLog::getColorVariable()
return s_color ? s_color : YAPLOG_COLOR_DEFAULT;
}
const char *InternalLog::getInfoVariable()
{
return s_info ? s_info : YAPLOG_INFO_DEFAULT;
}
void InternalLog::setDestinationVariable(const char *dest)
{
unsetDestinationVariable();
......@@ -156,6 +164,12 @@ void InternalLog::setColorVariable(const char *color)
s_color = strdup(color);
}
void InternalLog::setInfoVariable(const char *info)
{
unsetInfoVariable();
s_info = strdup(info);
}
void InternalLog::unsetDestinationVariable()
{
if (s_destination != NULL)
......@@ -177,6 +191,13 @@ void InternalLog::unsetColorVariable()
s_color = NULL;
}
void InternalLog::unsetInfoVariable()
{
if (s_info != NULL)
free(static_cast<void *>(s_info));
s_info = NULL;
}
bool InternalLog::getColor()
{
const char *color = std::getenv(getColorVariable());
......@@ -192,6 +213,17 @@ bool InternalLog::getColor()
}
}
bool InternalLog::getInfo()
{
const char *info = std::getenv(getInfoVariable());
if (info == NULL) {
return false;
} else {
return (strcmp(info, "debug") == 0);
}
}
const char *InternalLog::color_start(bool color)
{
if (!color)
......
......@@ -9,6 +9,7 @@
#define YAPLOG_LEVEL_DEFAULT "LOGLEVEL"
#define YAPLOG_DEST_DEFAULT "LOGDESTINATION"
#define YAPLOG_COLOR_DEFAULT "LOGCOLOR"
#define YAPLOG_INFO_DEFAULT "LOGINFO"
namespace logger {
......@@ -112,6 +113,13 @@ namespace logger {
* @return Flag to know if color needs to be used.
*/
static bool getColor();
/**
* @brief Get configured information level to use
*
* @return Flag to know if file, line and function needs to be
* printed.
*/
static bool getInfo();
public:
/**
......@@ -189,6 +197,12 @@ namespace logger {
* @return Color environment variable
*/
static const char *getColorVariable();
/**
* @brief Get environment variable to check for log info to show.
*
* @return Info environment variable
*/
static const char *getInfoVariable();
/**
* @brief Environment variable name of log destination
......@@ -203,6 +217,10 @@ namespace logger {
* @brief Environment variable name of log color configuration.
*/
static char *s_color;
/**
* @brief Environment variable name of log info configuration.
*/
static char *s_info;
public:
/**
......@@ -224,6 +242,12 @@ namespace logger {
*/
static void setColorVariable(const char *color);
/**
* @brief Set environment variable to check for log information.
*
* @param info New info environement variable
*/
static void setInfoVariable(const char *info);
/**
* @brief Unset environment variable to check for log file destination.
*/
static void unsetDestinationVariable();
......@@ -235,6 +259,10 @@ namespace logger {
* @brief Unset environment variable to check for log color.
*/
static void unsetColorVariable();
/**
* @brief Unset environment variable to check for log info.
*/
static void unsetInfoVariable();
};
/**
......
......@@ -16,11 +16,13 @@ TEST_GROUP(internallog)
{
void setup()
{
setenv("LOGINFO", "debug", 1);
my_real_stream = new std::ostringstream();
};
void teardown()
{
unsetenv("LOGINFO");
};
std::ostringstream *my_real_stream;
......@@ -82,6 +84,7 @@ TEST_GROUP(customout)
const char *destination = "unittests_customout.log";
void setup()
{
setenv("LOGINFO", "debug", 1);
unsetenv("LOGDESTINATION");
unsetenv("LOGLEVEL");
unlink(destination);
......@@ -89,6 +92,7 @@ TEST_GROUP(customout)
void teardown()
{
unsetenv("LOGINFO");
unlink(destination);
unsetenv("LOGDESTINATION");
unsetenv("LOGLEVEL");
......@@ -401,12 +405,14 @@ TEST_GROUP(stdoutlog)
{
void setup()
{
setenv("LOGINFO", "debug", 1);
setenv("LOGDESTINATION", "stdout", 1);
setenv("LOGLEVEL", "9", 1);
}
void teardown()
{
unsetenv("LOGINFO");
unsetenv("LOGDESTINATION");
unsetenv("LOGLEVEL");
}
......@@ -429,12 +435,14 @@ TEST_GROUP(stderrlog)
{
void setup()
{
setenv("LOGINFO", "debug", 1);
setenv("LOGDESTINATION", "stderr", 1);
setenv("LOGLEVEL", "9", 1);
}
void teardown()
{
unsetenv("LOGINFO");
unsetenv("LOGDESTINATION");
unsetenv("LOGLEVEL");
}
......@@ -457,12 +465,14 @@ TEST_GROUP(invalidfilelog)
{
void setup()
{
setenv("LOGINFO", "debug", 1);
setenv("LOGDESTINATION", "/probablynotexisting/path/to/a/logfile", 1);
setenv("LOGLEVEL", "9", 1);
}
void teardown()
{
unsetenv("LOGINFO");
unsetenv("LOGDESTINATION");
unsetenv("LOGLEVEL");
}
......@@ -486,6 +496,7 @@ TEST_GROUP(customconfvariable)
{
void setup()
{
setenv("LOGINFO", "debug", 1);
logger::setDestinationVariable("MYCUSTOM_DEST");
logger::setLevelVariable("MYCUSTOM_LEVEL");
setenv("MYCUSTOM_DEST", "custom_output.log", 1);
......@@ -496,6 +507,7 @@ TEST_GROUP(customconfvariable)
void teardown()
{
unsetenv("LOGINFO");
unsetenv("MYCUSTOM_DEST");
unsetenv("MYCUSTOM_LEVEL");
unsetenv("LOGLEVEL");
......@@ -536,6 +548,7 @@ TEST_GROUP(colorconf)
{
void setup()
{
setenv("LOGINFO", "debug", 1);
logger::setColorVariable("MYCUSTOM_COLOR");
setenv("LOGDESTINATION", "color_output.log", 1);
setenv("LOGLEVEL", "9", 1);
......@@ -543,6 +556,7 @@ TEST_GROUP(colorconf)
void teardown()
{
unsetenv("LOGINFO");
unsetenv("MYCUSTOM_COLOR");
logger::unsetColorVariable();
unlink("color_output.log");
......@@ -656,4 +670,128 @@ TEST(colorconf, alllogs_color_unauth_values_settings)
check_file_content("color_output.log", expected);
};
TEST_GROUP(infocontrol)
{
void setup()
{
setenv("LOGDESTINATION", "info_output.log", 1);
setenv("LOGLEVEL", "9", 1);
}
void teardown()
{
unlink("info_output.log");
unsetenv("LOGLEVEL");
unsetenv("LOGDESTINATION");
unsetenv("LOGINFO");
logger::unsetInfoVariable();
}
};
TEST(infocontrol, alllog_noinfo)
{
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] Fatal\n"
"[A] Alert\n"
"[C] Crit\n"
"[E] Error\n"
"[W] Warning\n"
"[N] Notice\n"
"[I] Info\n"
"[D] Debug\n"
"[T] Trace\n"
);
check_file_content("info_output.log", expected);
};
TEST(infocontrol, alllog_info)
{
setenv("LOGINFO", "debug", 1);
givelog(log_level::fatal) << "Fatal";
givelog(log_level::alert) << "Alert";
givelog(log_level::crit) << "Crit";
givelog(log_level::error) << "Error";
setenv("LOGINFO", "debug_ornot", 1);
givelog(log_level::warn) << "Warning";
givelog(log_level::notice) << "Notice";
setenv("LOGINFO", "debug", 1);
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] Warning\n"
"[N] Notice\n"
"[I] file2:38(function2) Info\n"
"[D] file2:38(function2) Debug\n"
"[T] file2:38(function2) Trace\n"
);
check_file_content("info_output.log", expected);
};
TEST(infocontrol, customvar_noinfo)
{
logger::setInfoVariable("MYCUSTOM_INFO");
setenv("LOGINFO", "debug", 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] Fatal\n"
"[A] Alert\n"
"[C] Crit\n"
"[E] Error\n"
"[W] Warning\n"
"[N] Notice\n"
"[I] Info\n"
"[D] Debug\n"
"[T] Trace\n"
);
check_file_content("info_output.log", expected);
};
TEST(infocontrol, customvar_info)
{
logger::setInfoVariable("MYCUSTOM_INFO");
setenv("MYCUSTOM_INFO", "debug", 1);
givelog(log_level::fatal) << "Fatal";
givelog(log_level::alert) << "Alert";
givelog(log_level::crit) << "Crit";
givelog(log_level::error) << "Error";
setenv("MYCUSTOM_INFO", "toto", 1);
givelog(log_level::warn) << "Warning";
setenv("MYCUSTOM_INFO", "debug", 1);
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] 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("info_output.log", expected);
unsetenv("MYCUSTOM_INFO");
};
#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