mirror of
https://github.com/KjellKod/g3log.git
synced 2025-02-23 07:12:46 +01:00
Custom levels can be added: So far dynamic on-off does not work
This commit is contained in:
parent
010ecc5bc7
commit
c4f9463b6b
@ -30,9 +30,7 @@ struct LEVELS {
|
||||
// force internal copy of the const char*. This is a simple safeguard for when g3log is used in a
|
||||
// "dynamic, runtime loading of shared libraries"
|
||||
|
||||
LEVELS(const LEVELS &other)
|
||||
: value(other.value), text(other.text.c_str()) {}
|
||||
|
||||
LEVELS(const LEVELS &other): value(other.value), text(other.text.c_str()) {}
|
||||
LEVELS(int id, const char *idtext) : value(id), text(idtext) {}
|
||||
|
||||
friend bool operator==(const LEVELS &lhs, const LEVELS &rhs) {
|
||||
@ -50,28 +48,43 @@ namespace g3 {
|
||||
}
|
||||
|
||||
#if (defined(CHANGE_G3LOG_DEBUG_TO_DBUG))
|
||||
const LEVELS DBUG {
|
||||
g3::kDebugVaulue, {"DEBUG"}
|
||||
},
|
||||
const LEVELS DBUG {g3::kDebugVaulue, {"DEBUG"}},
|
||||
#else
|
||||
const LEVELS DEBUG {
|
||||
g3::kDebugVaulue, {"DEBUG"}
|
||||
},
|
||||
const LEVELS DEBUG {g3::kDebugVaulue, {"DEBUG"}},
|
||||
#endif
|
||||
INFO {g3::kDebugVaulue + 1, {"INFO"}},
|
||||
WARNING {INFO.value + 1, {"WARNING"}},
|
||||
// Insert here *any* extra logging levels that is needed
|
||||
|
||||
|
||||
|
||||
// Insert here *any* extra logging levels that is needed. You can do so in your own source file
|
||||
// If it is a FATAL you should keep it above (FATAL.value and below internal::CONTRACT.value
|
||||
// If it is a non-fatal you can keep it above (WARNING.value and below FATAL.value)
|
||||
//
|
||||
// example: MyLoggingLevel.h
|
||||
// #pragma once
|
||||
// const LEVELS MYINFO {WARNING.value +1, {"MyInfoLevel"}};
|
||||
// const LEVELS MYFATAL {FATAL.value +1, {"MyFatalLevel"}};
|
||||
//
|
||||
// IMPORTANT: As of yet dynamic on/off of logging is NOT changed automatically
|
||||
// any changes of this, if you use dynamic on/off must be done in loglevels.cpp,
|
||||
// g_log_level_status and
|
||||
// void setLogLevel(LEVELS log_level, bool enabled) {...}
|
||||
// bool logLevel(LEVELS log_level){...}
|
||||
|
||||
|
||||
// 1) Remember to update the FATAL initialization below
|
||||
// 2) Remember to update the initialization of "g3loglevels.cpp/g_log_level_status"
|
||||
FATAL {WARNING.value + 1, {"FATAL"}};
|
||||
|
||||
FATAL {WARNING.value + 500, {"FATAL"}};
|
||||
|
||||
namespace g3 {
|
||||
namespace internal {
|
||||
const LEVELS CONTRACT {
|
||||
100, {"CONTRACT"}
|
||||
}, FATAL_SIGNAL {101, {"FATAL_SIGNAL"}},
|
||||
FATAL_EXCEPTION {102, {"FATAL_EXCEPTION"}};
|
||||
const LEVELS CONTRACT {1000, {"CONTRACT"}},
|
||||
FATAL_SIGNAL {1001, {"FATAL_SIGNAL"}},
|
||||
FATAL_EXCEPTION {1002, {"FATAL_EXCEPTION"}};
|
||||
|
||||
/// helper function to tell the logger if a log message was fatal. If it is it will force
|
||||
/// a shutdown after all log entries are saved to the sinks
|
||||
bool wasFatal(const LEVELS &level);
|
||||
}
|
||||
|
||||
|
@ -418,6 +418,37 @@ TEST(CHECK, CHECK_ThatWontThrow) {
|
||||
|
||||
|
||||
|
||||
TEST(CustomLogLevels, AddANonFatal){
|
||||
RestoreFileLogger logger(log_directory);
|
||||
const LEVELS MYINFO {WARNING.value +1, {"MY_INFO_LEVEL"}};
|
||||
LOG(MYINFO) << "Testing my own custom level"; auto line = __LINE__;
|
||||
logger.reset();
|
||||
std::string file_content = readFileToText(logger.logFile());
|
||||
std::string expected;
|
||||
expected += "MY_INFO_LEVEL [test_io.cpp L: " + std::to_string(line);
|
||||
EXPECT_TRUE(verifyContent(file_content, expected)) << file_content
|
||||
<< "\n\nExpected: \n" << expected;
|
||||
}
|
||||
|
||||
TEST(CustomLogLevels, AddFatal){
|
||||
RestoreFileLogger logger(log_directory);
|
||||
const LEVELS DEADLY {FATAL.value +1, {"DEADLY"}};
|
||||
g_fatal_counter.store(0);
|
||||
ASSERT_FALSE(mockFatalWasCalled());
|
||||
g3::setFatalPreLoggingHook(fatalCounter);
|
||||
|
||||
LOG(DEADLY) << "Testing my own custom level"; auto line = __LINE__;
|
||||
logger.reset();
|
||||
ASSERT_TRUE(mockFatalWasCalled());
|
||||
EXPECT_EQ(size_t{1}, g_fatal_counter.load());
|
||||
|
||||
std::string file_content = readFileToText(logger.logFile());
|
||||
std::string expected;
|
||||
expected += "DEADLY [test_io.cpp L: " + std::to_string(line);
|
||||
EXPECT_TRUE(verifyContent(file_content, expected)) << file_content
|
||||
<< "\n\nExpected: \n" << expected;
|
||||
g_fatal_counter.store(0); // restore
|
||||
}
|
||||
|
||||
|
||||
#ifdef G3_DYNAMIC_LOGGING
|
||||
@ -552,6 +583,7 @@ TEST(DynamicLogging, DynamicLogging_Check_WillAlsoBeTurnedOffWhen_Fatal_Is_Disab
|
||||
|
||||
|
||||
|
||||
|
||||
#else
|
||||
TEST(DynamicLogging, DynamicLogging_IS_NOT_ENABLED) {
|
||||
ASSERT_TRUE(g3::logLevel(DEBUG));
|
||||
|
Loading…
x
Reference in New Issue
Block a user