Levels api changes 2 (#170)

* in progress

* Update loglevels.hpp

* Update loglevels.hpp

* don't code when too tired

* revert back

* removed comment
This commit is contained in:
Kjell Hedström 2017-04-05 23:03:55 -06:00 committed by GitHub
parent 9f9062f45f
commit e04681ac42
7 changed files with 273 additions and 142 deletions

View File

@ -33,7 +33,7 @@ IF(USE_DYNAMIC_LOGGING_LEVELS)
LIST(APPEND G3_DEFINITIONS G3_DYNAMIC_LOGGING)
MESSAGE("-DUSE_DYNAMIC_LOGGING_LEVELS=ON")
MESSAGE("\tDynamic logging levels is used")
MESSAGE("\tUse [g3::setLogLevel(LEVEL boolean)] to enable/disable logging on specified levels\n\n")
MESSAGE("\tUse [g3::addLogLevel(LEVEL boolean)] to enable/disable logging on specified levels\n\n")
ELSE()
MESSAGE("-DUSE_DYNAMIC_LOGGING_LEVELS=OFF")
ENDIF(USE_DYNAMIC_LOGGING_LEVELS)

View File

@ -62,7 +62,7 @@ struct LEVELS {
};
// If you want to add any extra logging level then please add to your own source file the logging level you need
// then insert it using g3::setLogLevel(...). Please note that this only works for dynamic logging levels.
// then insert it using g3::only_change_at_initialization::addLogLevel(...). Please note that this only works for dynamic logging levels.
//
// There should be NO reason for modifying this source file when adding custom levels
//
@ -75,11 +75,11 @@ struct LEVELS {
// const LEVELS MYFATAL {FATAL.value +1, "MyFatalLevel"};
//
// ... somewhere else when G3_DYNAMIC_LOGGING is enabled
// setLogLevel(MYINFO, true);
// addLogLevel(MYINFO, true);
// LOG(MYINFO) << "some text";
//
// ... another example, when G3_DYNAMIC_LOGGING is enabled
// 'setLogLevel' is NOT required
// 'addLogLevel' is NOT required
// LOG(MYFATL) << "this will just work, and it will be counted as a FATAL event";
namespace g3 {
static const int kDebugValue = 100;
@ -145,44 +145,48 @@ namespace g3 {
// Only safe if done at initialization in a single-thread context
namespace only_change_at_initialization {
// This sets the logging level status enabled or disabled for a logging level.
// If the logging level is NEW it will also ADD the logging level to the set of logging levels.
void setLogLevel(LEVELS level, bool enabled_status);
/// add a custom level - enabled or disabled
void addLogLevel(LEVELS level, bool enabled);
/// for a custom level, 'MYL_LEVEL', previously not added the following is equivalent
/// setLogLevel(myLevel, true)
/// add a custom level - enabled
void addLogLevel(LEVELS level);
/// Enable log level >= log_level.
/// log levels below will be disabled
/// log levels equal or higher will be enabled.
/// NOTE: If the level didn't exist prior then it will be added. This is similar to how
/// 'setLogLevel' works
void setHighestLogLevel(LEVELS level);
/// print all levels with their disabled or enabled status
std::string printLevels(std::map<int, g3::LoggingLevel> levelsToPrint);
/// print snapshot of system levels with their
/// disabled or enabled status
std::string printLevels();
/// reset all default logging levels to enabled
/// remove any added logging levels so that the only ones left are
/// {DEBUG,INFO,WARNING,ERROR,FATAL}
void reset();
} // only_change_at_initialization
namespace log_levels {
/// Enable log level >= log_level.
/// log levels below will be disabled
/// log levels equal or higher will be enabled.
void setHighest(LEVELS level);
void set(LEVELS level, bool enabled);
void disable(LEVELS level);
void enable(LEVELS level);
/// disable all logging levels
/// WARNING: This will also disable FATAL events from being logged
void disableAll();
void enableAll();
/// print all levels with their disabled or enabled status
std::string to_string(std::map<int, g3::LoggingLevel> levelsToPrint);
/// print snapshot of system levels with their
/// disabled or enabled status
std::string to_string();
/// Snapshot view of the current logging levels' status
std::map<int, g3::LoggingLevel> getAllLevels();
std::map<int, g3::LoggingLevel> getAll();
enum class level_status {Absent, Enabled, Disabled};
level_status LevelStatus(LEVELS level);
} // only_change_at_initialization
enum class status {Absent, Enabled, Disabled};
status getStatus(LEVELS level);
} // log_levels
#endif
/// Enabled status for the given logging level

View File

@ -32,46 +32,55 @@ namespace g3 {
#ifdef G3_DYNAMIC_LOGGING
namespace only_change_at_initialization {
void setLogLevel(LEVELS lvl, bool enabled) {
void addLogLevel(LEVELS lvl, bool enabled) {
int value = lvl.value;
internal::g_log_levels[value] = {lvl, enabled};
}
void addLogLevel(LEVELS level) {
setLogLevel(level, true);
}
void setHighestLogLevel(LEVELS enabledFrom) {
auto it = internal::g_log_levels.find(enabledFrom.value);
if (it == internal::g_log_levels.end()) {
addLogLevel(enabledFrom);
}
for (auto& v : internal::g_log_levels) {
if (v.first < enabledFrom.value) {
setLogLevel(v.second.level, false);
}
}
}
std::string printLevels(std::map<int, g3::LoggingLevel> levelsToPrint) {
std::string levels;
for (auto& v : levelsToPrint) {
levels += "name: " + v.second.level.text + " level: " + std::to_string(v.first) + " status: " + std::to_string(v.second.status.value()) + "\n";
}
return levels;
}
std::string printLevels() {
return printLevels(internal::g_log_levels);
addLogLevel(level, true);
}
void reset() {
g3::internal::g_log_levels = g3::internal::g_log_level_defaults;
}
} // only_change_at_initialization
namespace log_levels {
void setHighest(LEVELS enabledFrom) {
auto it = internal::g_log_levels.find(enabledFrom.value);
if (it != internal::g_log_levels.end()) {
for (auto& v : internal::g_log_levels) {
if (v.first < enabledFrom.value) {
disable(v.second.level);
} else {
enable(v.second.level);
}
}
}
}
void set(LEVELS level, bool enabled) {
auto it = internal::g_log_levels.find(level.value);
if (it != internal::g_log_levels.end()) {
internal::g_log_levels[level.value] = {level, enabled};
}
}
void disable(LEVELS level) {
set(level, false);
}
void enable(LEVELS level) {
set(level, true);
}
void disableAll() {
for (auto& v : internal::g_log_levels) {
@ -79,24 +88,41 @@ namespace g3 {
}
}
std::map<int, g3::LoggingLevel> getAllLevels() {
void enableAll() {
for (auto& v : internal::g_log_levels) {
v.second.status = true;
}
}
std::string to_string(std::map<int, g3::LoggingLevel> levelsToPrint) {
std::string levels;
for (auto& v : levelsToPrint) {
levels += "name: " + v.second.level.text + " level: " + std::to_string(v.first) + " status: " + std::to_string(v.second.status.value()) + "\n";
}
return levels;
}
std::string to_string() {
return to_string(internal::g_log_levels);
}
std::map<int, g3::LoggingLevel> getAll() {
return internal::g_log_levels;
}
//enum class level_status {Absent, Enabled, Disabled};
level_status LevelStatus(LEVELS level) {
// status : {Absent, Enabled, Disabled};
status getStatus(LEVELS level) {
const auto it = internal::g_log_levels.find(level.value);
if (internal::g_log_levels.end() == it) {
return level_status::Absent;
return status::Absent;
}
return (it->second.status.get().load() ? level_status::Enabled : level_status::Disabled);
return (it->second.status.get().load() ? status::Enabled : status::Disabled);
}
} // only_change_at_initialization
} // log_levels
#endif

View File

@ -528,7 +528,7 @@ TEST(CustomLogLevels, AddANonFatal) {
RestoreFileLogger logger(log_directory);
const LEVELS MYINFO {WARNING.value + 1, {"MY_INFO_LEVEL"}};
#ifdef G3_DYNAMIC_LOGGING
g3::only_change_at_initialization::setLogLevel(MYINFO, true);
g3::only_change_at_initialization::addLogLevel(MYINFO, true);
#endif
LOG(MYINFO) << "Testing my own custom level"; auto line = __LINE__;
logger.reset();
@ -547,7 +547,7 @@ TEST(CustomLogLevels, AddFatal) {
ASSERT_FALSE(mockFatalWasCalled());
g3::setFatalPreLoggingHook(fatalCounter);
#ifdef G3_DYNAMIC_LOGGING
g3::only_change_at_initialization::setLogLevel(DEADLY, true);
g3::only_change_at_initialization::addLogLevel(DEADLY, true);
#endif
LOG(DEADLY) << "Testing my own custom level"; auto line = __LINE__;
@ -573,10 +573,10 @@ namespace {
};
~RestoreDynamicLoggingLevels() {
g3::only_change_at_initialization::reset();
g3::only_change_at_initialization::setLogLevel(DEBUG, false);
g3::only_change_at_initialization::setLogLevel(INFO, false);
g3::only_change_at_initialization::setLogLevel(WARNING, false);
g3::only_change_at_initialization::setLogLevel(FATAL, false);
g3::only_change_at_initialization::addLogLevel(DEBUG, false);
g3::only_change_at_initialization::addLogLevel(INFO, false);
g3::only_change_at_initialization::addLogLevel(WARNING, false);
g3::only_change_at_initialization::addLogLevel(FATAL, false);
}
};
} // anonymous
@ -586,7 +586,7 @@ TEST(CustomLogLevels, AddANonFatal__ThenReset) {
RestoreFileLogger logger(log_directory);
const LEVELS MYINFO {WARNING.value + 2, {"MY_INFO_LEVEL"}};
EXPECT_FALSE(g3::logLevel(MYINFO));
g3::only_change_at_initialization::setLogLevel(MYINFO, true);
g3::only_change_at_initialization::addLogLevel(MYINFO, true);
EXPECT_TRUE(g3::logLevel(MYINFO));
g3::only_change_at_initialization::reset();
EXPECT_FALSE(g3::logLevel(MYINFO));
@ -603,7 +603,7 @@ TEST(CustomLogLevels, AddANonFatal__DidNotAddItToEnabledValue1) {
std::string expected;
expected += "MY_INFO_LEVEL [test_io.cpp:" + std::to_string(line);
EXPECT_FALSE(verifyContent(file_content, expected)) << file_content
<< "\n\nExpected: \n" << expected << "\nLevels:\n" << g3::only_change_at_initialization::printLevels();
<< "\n\nExpected: \n" << expected << "\nLevels:\n" << g3::log_levels::to_string();
}
TEST(CustomLogLevels, AddANonFatal__DidNotAddItToEnabledValue2) {
@ -617,13 +617,13 @@ TEST(CustomLogLevels, AddANonFatal__DidNotAddItToEnabledValue2) {
std::string expected;
expected += "MY_INFO_LEVEL [test_io.cpp:" + std::to_string(line);
EXPECT_FALSE(verifyContent(file_content, expected)) << file_content
<< "\n\nExpected: \n" << expected << "\nLevels:\n" << g3::only_change_at_initialization::printLevels();
<< "\n\nExpected: \n" << expected << "\nLevels:\n" << g3::log_levels::to_string();
}
TEST(CustomLogLevels, AddANonFatal__DidtAddItToEnabledValue) {
RestoreFileLogger logger(log_directory);
const LEVELS MYINFO {WARNING.value + 3, {"MY_INFO_LEVEL"}};
g3::only_change_at_initialization::setLogLevel(MYINFO, true);
g3::only_change_at_initialization::addLogLevel(MYINFO, true);
LOG(MYINFO) << "Testing my own custom level"; auto line = __LINE__;
logger.reset();
std::string file_content = readFileToText(logger.logFile());
@ -641,25 +641,25 @@ TEST(DynamicLogging, DynamicLogging_IS_ENABLED) {
ASSERT_TRUE(g3::logLevel(INFO));
ASSERT_TRUE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::setLogLevel(DEBUG, false);
g3::only_change_at_initialization::addLogLevel(DEBUG, false);
ASSERT_FALSE(g3::logLevel(DEBUG));
ASSERT_TRUE(g3::logLevel(INFO));
ASSERT_TRUE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::setLogLevel(INFO, false);
g3::only_change_at_initialization::addLogLevel(INFO, false);
ASSERT_FALSE(g3::logLevel(DEBUG));
ASSERT_FALSE(g3::logLevel(INFO));
ASSERT_TRUE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::setLogLevel(WARNING, false);
g3::only_change_at_initialization::addLogLevel(WARNING, false);
ASSERT_FALSE(g3::logLevel(DEBUG));
ASSERT_FALSE(g3::logLevel(INFO));
ASSERT_FALSE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::setLogLevel(FATAL, false);
g3::only_change_at_initialization::addLogLevel(FATAL, false);
ASSERT_FALSE(g3::logLevel(DEBUG));
ASSERT_FALSE(g3::logLevel(INFO));
ASSERT_FALSE(g3::logLevel(WARNING));
@ -688,7 +688,7 @@ TEST(DynamicLogging, DynamicLogging_No_Logs_If_Disabled) {
{
RestoreFileLogger logger(log_directory);
g3::only_change_at_initialization::setLogLevel(DEBUG, false);
g3::only_change_at_initialization::addLogLevel(DEBUG, false);
EXPECT_FALSE(g3::logLevel(DEBUG));
LOG(DEBUG) << msg_debugOff;
auto content = logger.resetAndRetrieveContent();
@ -718,7 +718,7 @@ TEST(DynamicLogging, DynamicLogging_No_Fatal_If_Disabled) {
EXPECT_FALSE(mockFatalWasCalled());
g3::only_change_at_initialization::setLogLevel(FATAL, false);
g3::only_change_at_initialization::addLogLevel(FATAL, false);
std::string msg3 = "This is NOT fatal (not crash, since it is unit test. FATAL is disabled";
LOG(FATAL) << msg3;
EXPECT_FALSE(mockFatalWasCalled());
@ -741,7 +741,7 @@ TEST(DynamicLogging, DynamicLogging_Check_WillAlsoBeTurnedOffWhen_Fatal_Is_Disab
EXPECT_FALSE(mockFatalWasCalled());
// Disable also CHECK calls
g3::only_change_at_initialization::setLogLevel(FATAL, false);
g3::only_change_at_initialization::addLogLevel(FATAL, false);
ASSERT_FALSE(g3::logLevel(FATAL));
LOG(FATAL) << msg3;
EXPECT_FALSE(mockFatalWasCalled());
@ -753,7 +753,7 @@ TEST(DynamicLogging, DynamicLogging_Check_WillAlsoBeTurnedOffWhen_Fatal_Is_Disab
#else
TEST(DynamicLogging, DynamicLogging_IS_NOT_ENABLED) {
ASSERT_TRUE(g3::logLevel(DEBUG));
//g3::setLogLevel(DEBUG, false); this line will not compile since G3_DYNAMIC_LOGGING is not enabled. Kept for show.
//g3::addLogLevel(DEBUG, false); this line will not compile since G3_DYNAMIC_LOGGING is not enabled. Kept for show.
//ASSERT_FALSE(g3::logLevel(DEBUG));
}
#endif // Dynamic logging

View File

@ -215,19 +215,39 @@ namespace {
} // anonymous
TEST(Level, Default) {
g3::only_change_at_initialization::reset();
auto defaults = g3::only_change_at_initialization::getAllLevels();
auto defaults = g3::log_levels::getAll();
EXPECT_EQ(defaults.size(), g_test_log_level_defaults.size());
EXPECT_TRUE(mapCompare(defaults, g_test_log_level_defaults));
}
TEST(Level, DefaultChanged) {
TEST(Level, DefaultChanged_only_change_at_initialization) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::only_change_at_initialization::setLogLevel(INFO, false);
auto defaults = g3::only_change_at_initialization::getAllLevels();
g3::only_change_at_initialization::addLogLevel(INFO, false);
auto defaults = g3::log_levels::getAll();
EXPECT_EQ(defaults.size(), g_test_log_level_defaults.size());
EXPECT_FALSE(mapCompare(defaults, g_test_log_level_defaults));
const LevelsContainer defaultsWithInfoChangged = {
{g3::kDebugValue, {DEBUG, true}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
};
EXPECT_TRUE(mapCompare(defaults, defaultsWithInfoChangged));
}
TEST(Level, DefaultChanged_log_levels) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::log_levels::disable(INFO);
auto defaults = g3::log_levels::getAll();
EXPECT_EQ(defaults.size(), g_test_log_level_defaults.size());
EXPECT_FALSE(mapCompare(defaults, g_test_log_level_defaults));
@ -245,12 +265,12 @@ TEST(Level, Reset) {
g3::only_change_at_initialization::reset();
});
g3::only_change_at_initialization::disableAll();
auto all_levels = g3::only_change_at_initialization::getAllLevels();
g3::log_levels::disableAll();
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, g_test_all_disabled));
g3::only_change_at_initialization::reset();
all_levels = g3::only_change_at_initialization::getAllLevels();
all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, g_test_log_level_defaults));
@ -266,15 +286,88 @@ TEST(Level, AllDisabled) {
});
auto all_levels = g3::only_change_at_initialization::getAllLevels();
auto all_levels = g3::log_levels::getAll();
EXPECT_EQ(all_levels.size(), g_test_all_disabled.size());
EXPECT_FALSE(mapCompare(all_levels, g_test_all_disabled));
g3::only_change_at_initialization::disableAll();
all_levels = g3::only_change_at_initialization::getAllLevels();
g3::log_levels::disableAll();
all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, g_test_all_disabled));
}
TEST(Level, setHighestLogLevel_high_end) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::log_levels::enableAll();
g3::log_levels::disable(FATAL);
g3::log_levels::setHighest(FATAL);
LevelsContainer expected = {
{g3::kDebugValue, {DEBUG, false}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, false}},
{FATAL.value, {FATAL, true}}
};
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, expected)) << g3::log_levels::to_string();
}
TEST(Level, setHighestLogLevel_low_end) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::log_levels::disableAll();
g3::log_levels::setHighest(DEBUG);
LevelsContainer expected = {
{g3::kDebugValue, {DEBUG, true}},
{INFO.value, {INFO, true}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
};
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, expected)) << g3::log_levels::to_string();
}
TEST(Level, setHighestLogLevel_middle) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
g3::log_levels::enableAll();
g3::log_levels::setHighest(WARNING);
LevelsContainer expected = {
{g3::kDebugValue, {DEBUG, false}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
};
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, expected));
}
TEST(Level, setHighestLogLevel_StepWiseDisableAll) {
g3::only_change_at_initialization::reset();
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
@ -288,19 +381,19 @@ TEST(Level, setHighestLogLevel_StepWiseDisableAll) {
{FATAL.value, {FATAL, true}}
};
auto all_levels = g3::only_change_at_initialization::getAllLevels();
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, g_test_log_level_defaults));
size_t counter = 0;
for (auto& lvl : changing_levels) {
g3::only_change_at_initialization::setHighestLogLevel(lvl.second.level);
all_levels = g3::only_change_at_initialization::getAllLevels();
g3::log_levels::setHighest(lvl.second.level);
all_levels = g3::log_levels::getAll();
ASSERT_TRUE(mapCompare(all_levels, changing_levels)) <<
"counter: " << counter << "\nsystem:\n" <<
g3::only_change_at_initialization::printLevels(all_levels) <<
g3::log_levels::to_string(all_levels) <<
"\nexpected:\n" <<
g3::only_change_at_initialization::printLevels(changing_levels);
g3::log_levels::to_string(changing_levels);
++counter;
if (counter != changing_levels.size()) {
@ -315,12 +408,12 @@ TEST(Level, setHighestLogLevel_StepWiseDisableAll) {
mostly_disabled[FATAL.value].status = true;
EXPECT_TRUE(mapCompare(changing_levels, mostly_disabled));
all_levels = g3::only_change_at_initialization::getAllLevels();
all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(all_levels, mostly_disabled)) <<
"\nsystem:\n" <<
g3::only_change_at_initialization::printLevels(all_levels) <<
g3::log_levels::to_string(all_levels) <<
"\nexpected:\n" <<
g3::only_change_at_initialization::printLevels(mostly_disabled);
g3::log_levels::to_string(mostly_disabled);
}
TEST(Level, Print) {
@ -329,7 +422,7 @@ TEST(Level, Print) {
+ "name: INFO level: 300 status: 1\n"
+ "name: WARNING level: 500 status: 1\n"
+ "name: FATAL level: 1000 status: 1\n";
EXPECT_EQ(g3::only_change_at_initialization::printLevels(), expected);
EXPECT_EQ(g3::log_levels::to_string(), expected);
}
TEST(Level, AddOneEnabled_option1) {
@ -339,16 +432,16 @@ TEST(Level, AddOneEnabled_option1) {
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
g3::only_change_at_initialization::setLogLevel(MYINFO, true);
g3::only_change_at_initialization::addLogLevel(MYINFO, true);
auto modified = g_test_log_level_defaults;
modified[MYINFO.value] = MYINFO;
auto all_levels = g3::only_change_at_initialization::getAllLevels();
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(modified, all_levels)) << "\nsystem:\n" <<
g3::only_change_at_initialization::printLevels(all_levels) <<
g3::log_levels::to_string(all_levels) <<
"\nexpected:\n" <<
g3::only_change_at_initialization::printLevels(modified);
g3::log_levels::to_string(modified);
}
@ -364,11 +457,11 @@ TEST(Level, AddOneEnabled_option2) {
auto modified = g_test_log_level_defaults;
modified[MYINFO.value] = MYINFO;
auto all_levels = g3::only_change_at_initialization::getAllLevels();
auto all_levels = g3::log_levels::getAll();
EXPECT_TRUE(mapCompare(modified, all_levels)) << "\nsystem:\n" <<
g3::only_change_at_initialization::printLevels(all_levels) <<
g3::log_levels::to_string(all_levels) <<
"\nexpected:\n" <<
g3::only_change_at_initialization::printLevels(modified);
g3::log_levels::to_string(modified);
}
@ -376,59 +469,68 @@ TEST(Level, AddOneEnabled_option2) {
TEST(Level, Addlevel_using_addLevel) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
auto status = g3::only_change_at_initialization::LevelStatus(MYINFO);
EXPECT_EQ(status, g3::only_change_at_initialization::level_status::Absent);
auto status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::only_change_at_initialization::addLogLevel(MYINFO);
status = g3::only_change_at_initialization::LevelStatus(MYINFO);
EXPECT_EQ(status, g3::only_change_at_initialization::level_status::Enabled);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Enabled);
}
TEST(Level, Addlevel_using_setLogLevel_disabled) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
TEST(Level, Addlevel_using_addLogLevel_disabled) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
auto status = g3::only_change_at_initialization::LevelStatus(MYINFO);
EXPECT_EQ(status, g3::only_change_at_initialization::level_status::Absent);
auto status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::only_change_at_initialization::setLogLevel(MYINFO, false);
status = g3::only_change_at_initialization::LevelStatus(MYINFO);
EXPECT_EQ(status, g3::only_change_at_initialization::level_status::Disabled);
g3::only_change_at_initialization::addLogLevel(MYINFO, false);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Disabled);
}
TEST(Level, Addlevel_using_setLogLevel_enabled) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
TEST(Level, Addlevel__disabled) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
auto status = g3::only_change_at_initialization::LevelStatus(MYINFO);
EXPECT_EQ(status, g3::only_change_at_initialization::level_status::Absent);
auto status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::only_change_at_initialization::setLogLevel(MYINFO, true);
status = g3::only_change_at_initialization::LevelStatus(MYINFO);
EXPECT_EQ(status, g3::only_change_at_initialization::level_status::Enabled);
g3::log_levels::enable(MYINFO);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::log_levels::set(MYINFO, true);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::only_change_at_initialization::addLogLevel(MYINFO, false);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Disabled);
}
TEST(Level, Addlevel_using_setHighestLogLevel) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
TEST(Level, Addlevel__enabled) {
std::shared_ptr<void> RaiiLeveReset(nullptr, [&](void*) {
g3::only_change_at_initialization::reset();
});
LEVELS MYINFO {WARNING.value + 1, "MyInfoLevel"};
auto status = g3::only_change_at_initialization::LevelStatus(MYINFO);
EXPECT_EQ(status, g3::only_change_at_initialization::level_status::Absent);
auto status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Absent);
g3::only_change_at_initialization::setHighestLogLevel(MYINFO);
status = g3::only_change_at_initialization::LevelStatus(MYINFO);
EXPECT_EQ(status, g3::only_change_at_initialization::level_status::Enabled);
g3::only_change_at_initialization::addLogLevel(MYINFO);
status = g3::log_levels::getStatus(MYINFO);
EXPECT_EQ(status, g3::log_levels::status::Enabled);
}
#endif // G3_DYNAMIC_LOGGING

View File

@ -231,4 +231,3 @@ TEST(ConceptSink, AggressiveThreadCallsDuringShutdown) {
}

View File

@ -122,10 +122,10 @@ namespace testing_helpers {
_log_file = filename.get();
#ifdef G3_DYNAMIC_LOGGING
g3::only_change_at_initialization::setLogLevel(INFO, true);
g3::only_change_at_initialization::setLogLevel(DEBUG, true);
g3::only_change_at_initialization::setLogLevel(WARNING, true);
g3::only_change_at_initialization::setLogLevel(FATAL, true);
g3::only_change_at_initialization::addLogLevel(INFO, true);
g3::only_change_at_initialization::addLogLevel(DEBUG, true);
g3::only_change_at_initialization::addLogLevel(WARNING, true);
g3::only_change_at_initialization::addLogLevel(FATAL, true);
#endif
}