effectively remove feature - it is almost useless. Keep the change to the unit test avoiding premature exit when fatal signal handling is not enabled in the library

This commit is contained in:
Andreas Schönle 2016-12-06 13:55:47 +01:00
parent b43848c527
commit 4bd1870a76
4 changed files with 9 additions and 28 deletions

View File

@ -76,9 +76,8 @@ If the ```<boolean-expression>``` evaluates to false then the the message for th
const LEVELS DEADLY {FATAL.value + 1, {"DEADLY"}}; const LEVELS DEADLY {FATAL.value + 1, {"DEADLY"}};
``` ```
Both custom and built-in log levels can be turned on and off using the function ```g3::only_change_at_initialization::setLogLevel```. Calling ```g3::only_change_at_initialization::reset``` will reset all built-in levels to their defaults and remove custom levels. If the latter is not the desired behavior the function ```g3::only_change_at_initialization::addLogLevel``` can be used to turn a custom log level on or off and save this state as the default which is restored when ```reset()``` is called.
Calling ```g3::only_change_at_initialization::reset(true)``` will remove all custom levels regardless of whether ```addLevel``` was used with them.
## Sink <a name="sink_creation">creation</a> and utilization ## Sink <a name="sink_creation">creation</a> and utilization
The default sink for g3log is the one as used in g2log. It is a simple file sink with a limited API. The details for the default file sink can be found in [filesink.hpp](src/g3log/filesink.hpp), [filesink.cpp](src/filesink.cpp), [filesinkhelper.ipp](src/filesinkhelper.ipp) The default sink for g3log is the one as used in g2log. It is a simple file sink with a limited API. The details for the default file sink can be found in [filesink.hpp](src/g3log/filesink.hpp), [filesink.cpp](src/filesink.cpp), [filesinkhelper.ipp](src/filesinkhelper.ipp)

View File

@ -115,9 +115,8 @@ namespace g3 {
namespace only_change_at_initialization { namespace only_change_at_initialization {
// Enable/Disable a log level {DEBUG,INFO,WARNING,FATAL} // Enable/Disable a log level {DEBUG,INFO,WARNING,FATAL}
void setLogLevel(LEVELS level, bool enabled_status); void setLogLevel(LEVELS level, bool enabled_status);
void addLogLevel(LEVELS level, bool enabled_status = true);
std::string printLevels(); std::string printLevels();
void reset(bool remove_custom_levels = false); void reset();
} // only_change_at_initialization } // only_change_at_initialization
#endif #endif

View File

@ -40,21 +40,15 @@ namespace g3 {
} }
#ifdef G3_DYNAMIC_LOGGING #ifdef G3_DYNAMIC_LOGGING
std::map<int, atomicbool> g_default_log_level_status = {{g3::kDebugValue, true}, {INFO.value, true}, {WARNING.value, true}, {FATAL.value, true}}; std::map<int, atomicbool> g_log_level_status = {{g3::kDebugValue, true}, {INFO.value, true}, {WARNING.value, true}, {FATAL.value, true}};
std::map<int, atomicbool> g_log_level_status(g_default_log_level_status);
#endif #endif
} // internal } // internal
#ifdef G3_DYNAMIC_LOGGING #ifdef G3_DYNAMIC_LOGGING
namespace only_change_at_initialization { namespace only_change_at_initialization {
void setLogLevel(LEVELS log_level, bool enabled_status) { void setLogLevel(LEVELS log_level, bool enabled) {
int level = log_level.value; int level = log_level.value;
internal::g_log_level_status[level].get().store(enabled_status, std::memory_order_release); internal::g_log_level_status[level].get().store(enabled, std::memory_order_release);
}
void addLogLevel(LEVELS log_level, bool enabled_status) {
internal::g_default_log_level_status[log_level.value].get().store(enabled_status, std::memory_order_release);
setLogLevel(log_level, enabled_status);
} }
std::string printLevels() { std::string printLevels() {
@ -65,10 +59,9 @@ namespace g3 {
return levels; return levels;
} }
void reset(bool remove_custom_levels) { void reset() {
if (remove_custom_levels) internal::g_log_level_status.clear();
internal::g_default_log_level_status = { { g3::kDebugValue, true },{ INFO.value, true },{ WARNING.value, true },{ FATAL.value, true } }; internal::g_log_level_status = std::map<int, atomicbool>{{g3::kDebugValue, true}, {INFO.value, true}, {WARNING.value, true}, {FATAL.value, true}};
internal::g_log_level_status = internal::g_default_log_level_status;
} }
} // only_change_at_initialization } // only_change_at_initialization
#endif #endif

View File

@ -572,7 +572,7 @@ namespace {
RestoreDynamicLoggingLevels() { RestoreDynamicLoggingLevels() {
}; };
~RestoreDynamicLoggingLevels() { ~RestoreDynamicLoggingLevels() {
g3::only_change_at_initialization::reset(true); g3::only_change_at_initialization::reset();
g3::only_change_at_initialization::setLogLevel(DEBUG, false); g3::only_change_at_initialization::setLogLevel(DEBUG, false);
g3::only_change_at_initialization::setLogLevel(INFO, false); g3::only_change_at_initialization::setLogLevel(INFO, false);
g3::only_change_at_initialization::setLogLevel(WARNING, false); g3::only_change_at_initialization::setLogLevel(WARNING, false);
@ -592,16 +592,6 @@ TEST(CustomLogLevels, AddANonFatal__ThenReset) {
EXPECT_FALSE(g3::logLevel(MYINFO)); EXPECT_FALSE(g3::logLevel(MYINFO));
} }
TEST(CustomLogLevels, AddANonFatalWithDefault__ThenReset) {
RestoreFileLogger logger(log_directory);
RestoreDynamicLoggingLevels raiiLevelRestore;
const LEVELS MYINFO{ WARNING.value + 2,{ "MY_INFO_LEVEL" } };
EXPECT_FALSE(g3::logLevel(MYINFO));
g3::only_change_at_initialization::addLogLevel(MYINFO, true);
EXPECT_TRUE(g3::logLevel(MYINFO));
g3::only_change_at_initialization::reset();
EXPECT_TRUE(g3::logLevel(MYINFO));
}
TEST(CustomLogLevels, AddANonFatal__DidNotAddItToEnabledValue1) { TEST(CustomLogLevels, AddANonFatal__DidNotAddItToEnabledValue1) {
RestoreFileLogger logger(log_directory); RestoreFileLogger logger(log_directory);