From 959938d67a208196e4730cacf0117d0de5f2fd74 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kjell=20Hedstr=C3=B6m?=
<331742+KjellKod@users.noreply.github.com>
Date: Thu, 30 Nov 2023 14:07:21 -0700
Subject: [PATCH] Let's always have unit test being built as the default
---
docs/building.md | 2 +-
test_unit/Test.cmake | 10 +-
test_unit/test_io.cpp | 260 +++++++++++++++++++++++-------------------
3 files changed, 147 insertions(+), 125 deletions(-)
diff --git a/docs/building.md b/docs/building.md
index cae52f5b..0883b7b8 100644
--- a/docs/building.md
+++ b/docs/building.md
@@ -213,7 +213,7 @@ will install the g3log library to `CPACK_PACKAGING_INSTALL_PREFIX`.
## Testing
-By default, tests will not be built. To enable unit testing, you should turn on `ADD_G3LOG_UNIT_TEST`.
+By default, tests will be built. To disable unit testing, you should turn off `ADD_G3LOG_UNIT_TEST`.
Suppose the build process has completed, then you can run the tests with:
```
diff --git a/test_unit/Test.cmake b/test_unit/Test.cmake
index 691e1c2f..3a28639d 100644
--- a/test_unit/Test.cmake
+++ b/test_unit/Test.cmake
@@ -11,17 +11,13 @@
# ============================================================================
# TEST OPTIONS: Turn OFF the ones that is of no interest to you
- # ---- by default all is OFF: except 'g3log-FATAL-example -----
- # ---- the reason for this is that
- # ----- 1) the performance tests were only thoroughly tested on Ubuntu, not windows-
- # (g3log windows/linux, but Google's glog only on linux)
- #
- # 2) The unit test were tested windows/linux
+ # ---- by default unit tests and g3log-FATAL-example are enabled.
+ # Performance tests are turned off by default since they were not tested on Windows.
# ============================================================================
# Unit test for g3log (cmake -DUSE_G3LOG_UNIT_TEST=ON ..)
- option (ADD_G3LOG_UNIT_TEST "g3log unit tests" OFF)
+ option (ADD_G3LOG_UNIT_TEST "g3log unit tests" ON)
# 4. create the unit tests for g3log --- ONLY TESTED THE UNIT TEST ON LINUX
diff --git a/test_unit/test_io.cpp b/test_unit/test_io.cpp
index 4993bcfb..f6ca1b53 100644
--- a/test_unit/test_io.cpp
+++ b/test_unit/test_io.cpp
@@ -8,19 +8,19 @@
#include
#include "g3log/g3log.hpp"
-#include "g3log/generated_definitions.hpp"
-#include "g3log/loglevels.hpp"
#include "g3log/logworker.hpp"
#include "testing_helpers.h"
+#include "g3log/loglevels.hpp"
+#include "g3log/generated_definitions.hpp"
-#include
-#include
-#include
-#include
-#include
#include
#include
+#include
#include
+#include
+#include
+#include
+#include
namespace {
const std::string log_directory = "./";
@@ -36,10 +36,12 @@ namespace {
++g_fatal_counter;
}
-} // end anonymous namespace
+} // end anonymous namespace
+
using namespace testing_helpers;
+
/// THIS MUST BE THE FIRST UNIT TEST TO RUN! If any unit test run before this
/// one then it could fail. For dynamic levels all levels are turned on only AT
/// instantiation so we do different test for dynamic logging levels
@@ -69,18 +71,18 @@ TEST(Initialization, No_Logger_Initialized___Expecting_LOG_calls_to_be_Still_OKi
std::string err_msg1 = "Hey. I am not instantiated but I still should not crash. (I am g3log)";
std::string err_msg3_ignored = "This uninitialized message should be ignored";
try {
- LOG(INFO) << err_msg1; // nothing happened. level not ON
- LOG(INFO) << err_msg3_ignored; // nothing happened. level not ON
+ LOG(INFO) << err_msg1; // nothing happened. level not ON
+ LOG(INFO) << err_msg3_ignored; // nothing happened. level not ON
} catch (std::exception& e) {
ADD_FAILURE() << "Should never have thrown even if it is not instantiated. Ignored exception: " << e.what();
}
- RestoreFileLogger logger(log_directory); // now instantiate the logger
+ RestoreFileLogger logger(log_directory); // now instantiate the logger
std::string good_msg1 = "This message could have pulled in the uninitialized_call message";
LOG(INFO) << good_msg1;
- auto content = logger.resetAndRetrieveContent(); // this synchronizes with the LOG(INFO) call if debug level would be ON.
+ auto content = logger.resetAndRetrieveContent(); // this synchronizes with the LOG(INFO) call if debug level would be ON.
ASSERT_TRUE(verifyContent(content, err_msg1)) << "Content: [" << content << "]";
ASSERT_FALSE(verifyContent(content, err_msg3_ignored)) << "Content: [" << content << "]";
ASSERT_TRUE(verifyContent(content, good_msg1)) << "Content: [" << content << "]";
@@ -103,16 +105,16 @@ TEST(Initialization, No_Logger_Initialized___Expecting_LOG_calls_to_be_Still_OKi
ADD_FAILURE() << "Should never have thrown even if it is not instantiated: " << e.what();
}
- RestoreFileLogger logger(log_directory); // now instantiate the logger
+ RestoreFileLogger logger(log_directory); // now instantiate the logger
std::string good_msg1 = "This message will pull in also the uninitialized_call message";
LOG(INFO) << good_msg1;
- auto content = logger.resetAndRetrieveContent(); // this synchronizes with the LOG(INFO) call.
+ auto content = logger.resetAndRetrieveContent(); // this synchronizes with the LOG(INFO) call.
ASSERT_TRUE(verifyContent(content, err_msg1)) << "Content: [" << content << "]";
ASSERT_FALSE(verifyContent(content, err_msg3_ignored)) << "Content: [" << content << "]";
ASSERT_TRUE(verifyContent(content, good_msg1)) << "Content: [" << content << "]";
}
-#endif // #ifdef G3_DYNAMIC_LOGGING
+#endif // #ifdef G3_DYNAMIC_LOGGING
TEST(Basics, Levels_StdFind) {
std::vector levels = {INFO, WARNING, FATAL};
@@ -138,6 +140,7 @@ TEST(Basics, Levels_StdFind) {
EXPECT_FALSE(wasNotFoundIterator != levels.end());
}
+
TEST(Basics, Levels_Operator) {
auto info = INFO;
auto warning = WARNING;
@@ -156,10 +159,10 @@ TEST(Basics, Shutdown) {
LOG(INFO) << "First message buffered, then flushed";
LOG(INFO) << "Second message still in the buffer";
LOG(INFO) << "Not yet shutdown. This message should make it";
- logger.reset(); // force flush of logger (which will trigger a shutdown)
+ logger.reset(); // force flush of logger (which will trigger a shutdown)
LOG(INFO) << "Logger is shutdown,. this message will not make it (but it's safe to try)";
- file_content = readFileToText(logger.logFile()); // logger is already reset
- SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
+ file_content = readFileToText(logger.logFile()); // logger is already reset
+ SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
}
EXPECT_TRUE(verifyContent(file_content, "First message buffered, then flushed"));
EXPECT_TRUE(verifyContent(file_content, "Second message still in the buffer"));
@@ -172,11 +175,11 @@ TEST(Basics, Shutdownx2) {
{
RestoreFileLogger logger(log_directory);
LOG(INFO) << "Not yet shutdown. This message should make it";
- logger.reset(); // force flush of logger (which will trigger a shutdown)
- g3::internal::shutDownLogging(); // already called in reset, but safe to call again
+ logger.reset(); // force flush of logger (which will trigger a shutdown)
+ g3::internal::shutDownLogging(); // already called in reset, but safe to call again
LOG(INFO) << "Logger is shutdown,. this message will not make it (but it's safe to try)";
- file_content = readFileToText(logger.logFile()); // already reset
- SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
+ file_content = readFileToText(logger.logFile()); // already reset
+ SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
}
EXPECT_TRUE(verifyContent(file_content, "Not yet shutdown. This message should make it"));
EXPECT_FALSE(verifyContent(file_content, "Logger is shutdown,. this message will not make it (but it's safe to try)"));
@@ -190,10 +193,9 @@ TEST(Basics, ShutdownActiveLogger) {
EXPECT_TRUE(g3::internal::shutDownLoggingForActiveOnly(logger._scope->get()));
LOG(INFO) << "Logger is shutdown,. this message will not make it (but it's safe to try)";
file_content = logger.resetAndRetrieveContent();
- SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
+ SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
}
- EXPECT_TRUE(verifyContent(file_content, "Not yet shutdown. This message should make it")) << "\n\n\n***************************\n"
- << file_content;
+ EXPECT_TRUE(verifyContent(file_content, "Not yet shutdown. This message should make it")) << "\n\n\n***************************\n" << file_content;
EXPECT_FALSE(verifyContent(file_content, "Logger is shutdown,. this message will not make it (but it's safe to try)"));
}
@@ -206,12 +208,13 @@ TEST(Basics, DoNotShutdownActiveLogger) {
EXPECT_FALSE(g3::internal::shutDownLoggingForActiveOnly(duplicateLogWorker.get()));
LOG(INFO) << "Logger is (NOT) shutdown,. this message WILL make it";
file_content = logger.resetAndRetrieveContent();
- SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
+ SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
}
EXPECT_TRUE(verifyContent(file_content, "Not yet shutdown. This message should make it"));
EXPECT_TRUE(verifyContent(file_content, "Logger is (NOT) shutdown,. this message WILL make it")) << file_content;
}
+
TEST(LOGTest, LOG) {
std::string file_content;
{
@@ -219,17 +222,20 @@ TEST(LOGTest, LOG) {
EXPECT_TRUE(g3::logLevel(INFO));
EXPECT_TRUE(g3::logLevel(FATAL));
LOG(INFO) << "test LOG(INFO)";
- logger.reset(); // force flush of logger
+ logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
- SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
+ SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
}
EXPECT_TRUE(verifyContent(file_content, "test LOG(INFO)"));
EXPECT_TRUE(g3::logLevel(INFO));
EXPECT_TRUE(g3::logLevel(FATAL));
}
+
+
// printf-type log
+
TEST(LogTest, LOG_F) {
std::string file_content;
{
@@ -239,15 +245,18 @@ TEST(LogTest, LOG_F) {
LOGF(INFO, std::string(t_info + "%d").c_str(), 123);
LOGF(G3LOG_DEBUG, std::string(t_debug + "%f").c_str(), 1.123456);
LOGF(WARNING, std::string(t_warning + "%s").c_str(), "yello");
- logger.reset(); // force flush of logger
+ logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
- SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
+ SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
}
ASSERT_TRUE(verifyContent(file_content, t_info2));
ASSERT_TRUE(verifyContent(file_content, t_debug3));
ASSERT_TRUE(verifyContent(file_content, t_warning3));
}
+
+
+
// stream-type log
TEST(LogTest, LOG) {
std::string file_content;
@@ -256,9 +265,9 @@ TEST(LogTest, LOG) {
LOG(INFO) << t_info << 123;
LOG(G3LOG_DEBUG) << t_debug << std::setprecision(7) << 1.123456f;
LOG(WARNING) << t_warning << "yello";
- logger.reset(); // force flush of logger
+ logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
- SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
+ SCOPED_TRACE("LOG_INFO"); // Scope exit be prepared for destructor failure
}
ASSERT_TRUE(verifyContent(file_content, t_info2));
ASSERT_TRUE(verifyContent(file_content, t_debug3));
@@ -269,12 +278,12 @@ TEST(LogTest, LOG_after_if) {
std::string file_content;
{
RestoreFileLogger logger(log_directory);
- if (false == file_content.empty())
+ if(false == file_content.empty())
LOG(INFO) << "This-should-NOT-show-up";
else
- LOG(INFO) << "This-should-show-up";
+ LOG(INFO) << "This-should-show-up";
- logger.reset(); // force flush of logger
+ logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
}
@@ -282,17 +291,18 @@ TEST(LogTest, LOG_after_if) {
ASSERT_TRUE(verifyContent(file_content, "This-should-show-up"));
}
+
TEST(LogTest, LOG_after_if_with_parentesis) {
std::string file_content;
{
RestoreFileLogger logger(log_directory);
- if (false == file_content.empty()) {
+ if(false == file_content.empty()) {
LOG(INFO) << "This-should-NOT-show-up";
} else {
- LOG(INFO) << "This-should-show-up";
+ LOG(INFO) << "This-should-show-up";
}
- logger.reset(); // force flush of logger
+ logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
}
@@ -300,29 +310,32 @@ TEST(LogTest, LOG_after_if_with_parentesis) {
ASSERT_TRUE(verifyContent(file_content, "This-should-show-up"));
}
+
+
TEST(LogTest, LOG_F_IF) {
std::string file_content;
{
RestoreFileLogger logger(log_directory);
LOGF_IF(INFO, (2 == 2), std::string(t_info + "%d").c_str(), 123);
LOGF_IF(G3LOG_DEBUG, (2 != 2), std::string(t_debug + "%f").c_str(), 1.123456);
- logger.reset(); // force flush of logger
+ logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
- SCOPED_TRACE("LOG_IF"); // Scope exit be prepared for destructor failure
+ SCOPED_TRACE("LOG_IF"); // Scope exit be prepared for destructor failure
}
ASSERT_TRUE(verifyContent(file_content, t_info2));
ASSERT_FALSE(verifyContent(file_content, t_debug3));
}
+
TEST(LogTest, LOG_IF) {
std::string file_content;
{
RestoreFileLogger logger(log_directory);
LOG_IF(INFO, (2 == 2)) << t_info << 123;
LOG_IF(G3LOG_DEBUG, (2 != 2)) << t_debug << std::setprecision(7) << 1.123456f;
- logger.reset(); // force flush of logger
+ logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
- SCOPED_TRACE("LOG_IF"); // Scope exit be prepared for destructor failure
+ SCOPED_TRACE("LOG_IF"); // Scope exit be prepared for destructor failure
}
EXPECT_TRUE(verifyContent(file_content, t_info2));
EXPECT_FALSE(verifyContent(file_content, t_debug3));
@@ -352,38 +365,39 @@ TEST(LogTest, FatalSIGTERM__UsingDefaultHandler) {
EXPECT_EQ(g_fatal_counter.load(), size_t{1});
}
-#if !(defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
+#if !(defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
namespace {
std::atomic customFatalCounter = {0};
std::atomic lastEncounteredSignal = {0};
void customSignalHandler(int signal_number, siginfo_t* info, void* unused_context) {
lastEncounteredSignal.store(signal_number);
- ++customFatalCounter;
+ ++customFatalCounter;
}
void installCustomSIGTERM() {
struct sigaction action;
- memset(&action, 0, sizeof(action));
- sigemptyset(&action.sa_mask);
- action.sa_sigaction = &customSignalHandler;
- action.sa_flags = SA_SIGINFO;
- sigaction(SIGTERM, &action, nullptr);
+ memset(&action, 0, sizeof(action));
+ sigemptyset(&action.sa_mask);
+ action.sa_sigaction = &customSignalHandler;
+ action.sa_flags = SA_SIGINFO;
+ sigaction(SIGTERM, &action, nullptr);
}
- std::atomic oldSigTermCheck = {false};
+
+ std::atomic oldSigTermCheck = {false};
void customOldSignalHandler(int signal_number, siginfo_t* info, void* unused_context) {
lastEncounteredSignal.store(signal_number);
oldSigTermCheck.store(true);
}
void installCustomOldSIGTERM() {
struct sigaction action;
- memset(&action, 0, sizeof(action));
- sigemptyset(&action.sa_mask);
- action.sa_sigaction = &customOldSignalHandler;
- action.sa_flags = SA_SIGINFO;
- sigaction(SIGTERM, &action, nullptr);
+ memset(&action, 0, sizeof(action));
+ sigemptyset(&action.sa_mask);
+ action.sa_sigaction = &customOldSignalHandler;
+ action.sa_flags = SA_SIGINFO;
+ sigaction(SIGTERM, &action, nullptr);
}
-} // namespace
+} // anonymous
// Override of signal handling and testing of it should be fairly easy to port to windows
// ref: https://github.com/KjellKod/g3log/blob/master/src/crashhandler_windows.cpp
@@ -406,14 +420,15 @@ namespace {
//
//void installCustomSIGTERM() {
// ASSERT_TRUE(SIG_ERR != signal(SIGTERM, customSignalHandler));
-//}
+//}
+
TEST(LogTest, FatalSIGTERM__UsingCustomHandler) {
RestoreFileLogger logger(log_directory);
g_fatal_counter.store(0);
g3::setFatalPreLoggingHook(fatalCounter);
installCustomSIGTERM();
- g3::overrideSetupSignals({{SIGABRT, "SIGABRT"}, {SIGFPE, "SIGFPE"}, {SIGILL, "SIGILL"}});
+ g3::overrideSetupSignals({ {SIGABRT, "SIGABRT"}, {SIGFPE, "SIGFPE"}, {SIGILL, "SIGILL"}});
installCustomSIGTERM();
EXPECT_EQ(customFatalCounter.load(), size_t{0});
@@ -434,8 +449,8 @@ TEST(LogTest, FatalSIGTERM__VerifyingOldCustomHandler) {
g3::setFatalPreLoggingHook(fatalCounter);
installCustomOldSIGTERM();
- g3::overrideSetupSignals({{SIGABRT, "SIGABRT"}, {SIGFPE, "SIGFPE"}, {SIGILL, "SIGILL"}, {SIGTERM, "SIGTERM"}});
- g3::restoreSignalHandler(SIGTERM); // revert SIGTERM installation
+ g3::overrideSetupSignals({ {SIGABRT, "SIGABRT"}, {SIGFPE, "SIGFPE"}, {SIGILL, "SIGILL"}, {SIGTERM, "SIGTERM"}});
+ g3::restoreSignalHandler(SIGTERM); // revert SIGTERM installation
EXPECT_EQ(customFatalCounter.load(), size_t{0});
EXPECT_EQ(lastEncounteredSignal.load(), 0);
@@ -444,9 +459,13 @@ TEST(LogTest, FatalSIGTERM__VerifyingOldCustomHandler) {
logger.reset();
EXPECT_EQ(g_fatal_counter.load(), size_t{0});
EXPECT_EQ(lastEncounteredSignal.load(), SIGTERM);
- EXPECT_TRUE(oldSigTermCheck.load());
+ EXPECT_TRUE(oldSigTermCheck.load());
}
+
+
+
+
#endif
#endif
@@ -471,15 +490,19 @@ TEST(LogTest, LOG_preFatalLogging_hook) {
}
}
+
+
TEST(LogTest, LOG_FATAL) {
RestoreFileLogger logger(log_directory);
ASSERT_FALSE(mockFatalWasCalled());
+
+
LOG(FATAL) << "This message is fatal";
EXPECT_TRUE(mockFatalWasCalled());
EXPECT_TRUE(verifyContent(mockFatalMessage(), "EXIT trigger caused by "));
EXPECT_TRUE(verifyContent(mockFatalMessage(), "This message is fatal"))
- << "\ncontent: [[" << mockFatalMessage() << "]]";
+ << "\ncontent: [[" << mockFatalMessage() << "]]";
EXPECT_TRUE(verifyContent(mockFatalMessage(), "FATAL"));
logger.reset();
@@ -493,8 +516,7 @@ TEST(LogTest, LOGF_IF__FATAL) {
EXPECT_FALSE(mockFatalWasCalled());
LOGF_IF(FATAL, (2 < 3), "This message %s be worse", "could");
EXPECT_TRUE(mockFatalWasCalled());
- EXPECT_TRUE(verifyContent(mockFatalMessage(), "EXIT trigger caused by ")) << "\n"
- << mockFatalMessage();
+ EXPECT_TRUE(verifyContent(mockFatalMessage(), "EXIT trigger caused by ")) << "\n" << mockFatalMessage();
EXPECT_TRUE(verifyContent(mockFatalMessage(), "FATAL"));
EXPECT_TRUE(verifyContent(mockFatalMessage(), "This message could be worse"));
@@ -527,6 +549,7 @@ TEST(LogTest, LOG_IF__FATAL__NO_THROW) {
ASSERT_FALSE(mockFatalWasCalled());
}
+
// CHECK_F
TEST(CheckTest, CHECK_F__thisWILL_PrintErrorMsg) {
RestoreFileLogger logger(log_directory);
@@ -540,9 +563,10 @@ TEST(CheckTest, CHECK_F__thisWILL_PrintErrorMsg) {
logger.reset();
std::string file_content = readFileToText(logger.logFile());
EXPECT_TRUE(verifyContent(mockFatalMessage(), "EXIT trigger caused by "));
- EXPECT_TRUE(verifyContent(file_content, "CONTRACT")) << "**** " << mockFatalMessage();
+ EXPECT_TRUE(verifyContent(file_content, "CONTRACT")) << "**** " << mockFatalMessage();
}
+
TEST(CHECK_F_Test, CHECK_F__thisWILL_PrintErrorMsg) {
RestoreFileLogger logger(log_directory);
std::string msg = "This message is added to throw %s and %s";
@@ -556,6 +580,7 @@ TEST(CHECK_F_Test, CHECK_F__thisWILL_PrintErrorMsg) {
EXPECT_TRUE(verifyContent(file_content, "CONTRACT"));
}
+
TEST(CHECK_Test, CHECK__thisWILL_PrintErrorMsg) {
RestoreFileLogger logger(log_directory);
std::string msg = "This message is added to throw message and log";
@@ -585,24 +610,24 @@ TEST(CHECK, CHECK_runtimeError) {
RestoreFileLogger logger(log_directory);
g3::setFatalExitHandler([](g3::FatalMessagePtr msg) {
- throw std::runtime_error("fatal test handler");
+ throw std::runtime_error("fatal test handler");
});
class dynamic_int_array {
- std::unique_ptr data_;
- const int size_;
-
- public:
- explicit dynamic_int_array(int size) :
- data_{std::make_unique(size)},
- size_(size) {}
-
- int& at(int i) {
- CHECK(i < size_);
-
- // unreachable if i >= size_
- return data_[i];
- }
+ std::unique_ptr data_;
+ const int size_;
+ public:
+ explicit dynamic_int_array(int size)
+ : data_{std::make_unique(size)}
+ , size_(size)
+ {}
+
+ int& at(int i) {
+ CHECK(i < size_);
+
+ // unreachable if i >= size_
+ return data_[i];
+ }
};
dynamic_int_array arr{3};
@@ -612,24 +637,22 @@ TEST(CHECK, CHECK_runtimeError) {
TEST(CustomLogLevels, AddANonFatal) {
RestoreFileLogger logger(log_directory);
- const LEVELS MYINFO{WARNING.value + 1, {"MY_INFO_LEVEL"}};
+ const LEVELS MYINFO {WARNING.value + 1, {"MY_INFO_LEVEL"}};
#ifdef G3_DYNAMIC_LOGGING
g3::only_change_at_initialization::addLogLevel(MYINFO, true);
#endif
- LOG(MYINFO) << "Testing my own custom level";
- auto line = __LINE__;
+ 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->" + std::string(G3LOG_PRETTY_FUNCTION) + ":" + std::to_string(line);
EXPECT_TRUE(verifyContent(file_content, expected)) << file_content
- << "\n\nExpected: \n"
- << expected;
+ << "\n\nExpected: \n" << expected;
}
TEST(CustomLogLevels, AddFatal) {
RestoreFileLogger logger(log_directory);
- const LEVELS DEADLY{FATAL.value + 1, {"DEADLY"}};
+ const LEVELS DEADLY {FATAL.value + 1, {"DEADLY"}};
EXPECT_TRUE(g3::internal::wasFatal(DEADLY));
g_fatal_counter.store(0);
ASSERT_FALSE(mockFatalWasCalled());
@@ -638,8 +661,7 @@ TEST(CustomLogLevels, AddFatal) {
g3::only_change_at_initialization::addLogLevel(DEADLY, true);
#endif
- LOG(DEADLY) << "Testing my own custom level";
- auto line = __LINE__;
+ LOG(DEADLY) << "Testing my own custom level"; auto line = __LINE__;
logger.reset();
ASSERT_TRUE(mockFatalWasCalled());
EXPECT_EQ(size_t{1}, g_fatal_counter.load());
@@ -648,17 +670,18 @@ TEST(CustomLogLevels, AddFatal) {
std::string expected;
expected += "DEADLY [test_io.cpp->" + std::string(G3LOG_PRETTY_FUNCTION) + ":" + std::to_string(line);
EXPECT_TRUE(verifyContent(file_content, expected)) << file_content
- << "\n\nExpected: \n"
- << expected;
- g_fatal_counter.store(0); // restore
+ << "\n\nExpected: \n" << expected;
+ g_fatal_counter.store(0); // restore
}
+
#ifdef G3_DYNAMIC_LOGGING
namespace {
// Restore dynamic levels if turned off
struct RestoreDynamicLoggingLevels {
- RestoreDynamicLoggingLevels(){};
+ RestoreDynamicLoggingLevels() {
+ };
~RestoreDynamicLoggingLevels() {
g3::only_change_at_initialization::reset();
g3::only_change_at_initialization::addLogLevel(G3LOG_DEBUG, false);
@@ -667,11 +690,12 @@ namespace {
g3::only_change_at_initialization::addLogLevel(FATAL, false);
}
};
-} // namespace
+} // anonymous
+
TEST(CustomLogLevels, AddANonFatal__ThenReset) {
RestoreFileLogger logger(log_directory);
- const LEVELS MYINFO{WARNING.value + 2, {"MY_INFO_LEVEL"}};
+ 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));
@@ -679,84 +703,78 @@ TEST(CustomLogLevels, AddANonFatal__ThenReset) {
EXPECT_FALSE(g3::logLevel(MYINFO));
}
+
TEST(CustomLogLevels, AddANonFatal__DidNotAddItToEnabledValue1) {
RestoreFileLogger logger(log_directory);
- const LEVELS MYINFO{WARNING.value + 2, {"MY_INFO_LEVEL"}};
- LOG(MYINFO) << "Testing my own custom level";
- auto line = __LINE__;
+ const LEVELS MYINFO {WARNING.value + 2, {"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:" + std::to_string(line);
EXPECT_FALSE(verifyContent(file_content, expected)) << file_content
- << "\n\nExpected: \n"
- << expected << "\nLevels:\n"
- << g3::log_levels::to_string();
+ << "\n\nExpected: \n" << expected << "\nLevels:\n" << g3::log_levels::to_string();
}
TEST(CustomLogLevels, AddANonFatal__DidNotAddItToEnabledValue2) {
RestoreFileLogger logger(log_directory);
- const LEVELS MYINFO{WARNING.value + 2, {"MY_INFO_LEVEL"}};
+ const LEVELS MYINFO {WARNING.value + 2, {"MY_INFO_LEVEL"}};
EXPECT_FALSE(g3::logLevel(MYINFO));
- LOG(MYINFO) << "Testing my own custom level";
- auto line = __LINE__;
+ 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:" + std::to_string(line);
EXPECT_FALSE(verifyContent(file_content, expected)) << file_content
- << "\n\nExpected: \n"
- << expected << "\nLevels:\n"
- << g3::log_levels::to_string();
+ << "\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"}};
+ const LEVELS MYINFO {WARNING.value + 3, {"MY_INFO_LEVEL"}};
g3::only_change_at_initialization::addLogLevel(MYINFO, true);
- LOG(MYINFO) << "Testing my own custom level";
- auto line = __LINE__;
+ 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->" + std::string(G3LOG_PRETTY_FUNCTION) + ":" + std::to_string(line);
EXPECT_TRUE(verifyContent(file_content, expected)) << file_content
- << "\n\nExpected: \n"
- << expected;
+ << "\n\nExpected: \n" << expected;
}
+
TEST(DynamicLogging, DynamicLogging_IS_ENABLED) {
RestoreDynamicLoggingLevels raiiLevelRestore;
ASSERT_TRUE(g3::logLevel(G3LOG_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.
+ ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::addLogLevel(G3LOG_DEBUG, false);
ASSERT_FALSE(g3::logLevel(G3LOG_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.
+ ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::addLogLevel(INFO, false);
ASSERT_FALSE(g3::logLevel(G3LOG_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.
+ ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::addLogLevel(WARNING, false);
ASSERT_FALSE(g3::logLevel(G3LOG_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.
+ ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::addLogLevel(FATAL, false);
ASSERT_FALSE(g3::logLevel(G3LOG_DEBUG));
ASSERT_FALSE(g3::logLevel(INFO));
ASSERT_FALSE(g3::logLevel(WARNING));
- ASSERT_FALSE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
+ ASSERT_FALSE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
}
TEST(DynamicLogging, DynamicLogging_No_Logs_If_Disabled) {
{
@@ -810,6 +828,7 @@ TEST(DynamicLogging, DynamicLogging_No_Fatal_If_Disabled) {
clearMockFatal();
EXPECT_FALSE(mockFatalWasCalled());
+
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;
@@ -817,6 +836,7 @@ TEST(DynamicLogging, DynamicLogging_No_Fatal_If_Disabled) {
EXPECT_TRUE(mockFatalMessage().empty());
}
+
TEST(DynamicLogging, DynamicLogging_Check_WillAlsoBeTurnedOffWhen_Fatal_Is_Disabled) {
RestoreFileLogger logger(log_directory);
RestoreDynamicLoggingLevels raiiLevelRestore;
@@ -838,10 +858,16 @@ TEST(DynamicLogging, DynamicLogging_Check_WillAlsoBeTurnedOffWhen_Fatal_Is_Disab
EXPECT_FALSE(mockFatalWasCalled());
}
+
+
+
#else
TEST(DynamicLogging, DynamicLogging_IS_NOT_ENABLED) {
ASSERT_TRUE(g3::logLevel(G3LOG_DEBUG));
//g3::addLogLevel(G3LOG_DEBUG, false); this line will not compile since G3_DYNAMIC_LOGGING is not enabled. Kept for show.
//ASSERT_FALSE(g3::logLevel(G3LOG_DEBUG));
}
-#endif // Dynamic logging
+#endif // Dynamic logging
+
+
+