diff --git a/src/cpp-utils/logging/logging.h b/src/cpp-utils/logging/logging.h index 230a3f27..3310395a 100644 --- a/src/cpp-utils/logging/logging.h +++ b/src/cpp-utils/logging/logging.h @@ -34,6 +34,10 @@ namespace cpputils { logger().reset(); } + inline void flush() { + logger()->flush(); + } + inline void setLevel(ERROR_TYPE) { logger().setLevel(spdlog::level::err); } diff --git a/test/cpp-utils/logging/LoggingTest.cpp b/test/cpp-utils/logging/LoggingTest.cpp index 2cfd6f61..9dbea1f8 100644 --- a/test/cpp-utils/logging/LoggingTest.cpp +++ b/test/cpp-utils/logging/LoggingTest.cpp @@ -12,6 +12,7 @@ using std::string; TEST_F(LoggingTest, DefaultLoggerIsStderr) { string output = captureStderr([]{ LOG(INFO, "My log message"); + cpputils::logging::flush(); }); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(output, MatchesRegex(".*\\[Log\\].*\\[info\\].*My log message.*")); @@ -22,6 +23,7 @@ TEST_F(LoggingTest, SetLogger_NewLoggerIsUsed) { setLogger(spdlog::stderr_logger_mt("MyTestLog2")); string output = captureStderr([]{ LOG(INFO, "My log message"); + cpputils::logging::flush(); }); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(output, MatchesRegex(".*\\[MyTestLog2\\].*\\[info\\].*My log message.*")); @@ -31,6 +33,7 @@ TEST_F(LoggingTest, SetLogger_NewLoggerIsUsed) { TEST_F(LoggingTest, SetNonStderrLogger_LogsToNewLogger) { setLogger(mockLogger.get()); logger()->info("My log message"); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(output, MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"))); @@ -40,6 +43,7 @@ TEST_F(LoggingTest, SetNonStderrLogger_DoesNotLogToStderr) { setLogger(mockLogger.get()); string output = captureStderr([] { logger()->info("My log message"); + cpputils::logging::flush(); }); EXPECT_EQ("", output); } @@ -47,6 +51,7 @@ TEST_F(LoggingTest, SetNonStderrLogger_DoesNotLogToStderr) { TEST_F(LoggingTest, InfoLog) { setLogger(mockLogger.get()); LOG(INFO, "My log message"); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"))); @@ -55,6 +60,7 @@ TEST_F(LoggingTest, InfoLog) { TEST_F(LoggingTest, WarningLog) { setLogger(mockLogger.get()); LOG(WARN, "My log message"); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[warning\\].*My log message.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[warning\\].*My log message.*"))); @@ -64,6 +70,7 @@ TEST_F(LoggingTest, DebugLog) { setLevel(DEBUG); setLogger(mockLogger.get()); LOG(DEBUG, "My log message"); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[debug\\].*My log message.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[debug\\].*My log message.*"))); @@ -72,6 +79,7 @@ TEST_F(LoggingTest, DebugLog) { TEST_F(LoggingTest, ErrorLog) { setLogger(mockLogger.get()); LOG(ERR, "My log message"); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[error\\].*My log message.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[error\\].*My log message.*"))); @@ -79,6 +87,7 @@ TEST_F(LoggingTest, ErrorLog) { void logAndExit(const string &message) { LOG(INFO, message); + cpputils::logging::flush(); exit(1); } @@ -96,6 +105,7 @@ TEST_F(LoggingTest, LoggingAlsoWorksAfterFork) { TEST_F(LoggingTest, MessageIsConstChar) { setLogger(mockLogger.get()); LOG(INFO, "My log message"); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"))); @@ -105,6 +115,7 @@ TEST_F(LoggingTest, MessageIsString) { setLogger(mockLogger.get()); string msg = "My log message"; LOG(INFO, msg); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"))); @@ -114,6 +125,7 @@ TEST_F(LoggingTest, FormatWithStringPlaceholder) { setLogger(mockLogger.get()); string str = "placeholder"; LOG(INFO, "My log message: {}", str); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message: placeholder.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message: placeholder.*"))); @@ -122,6 +134,7 @@ TEST_F(LoggingTest, FormatWithStringPlaceholder) { TEST_F(LoggingTest, FormatWithConstCharPlaceholder) { setLogger(mockLogger.get()); LOG(INFO, "My log message: {}", "placeholder"); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message: placeholder.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message: placeholder.*"))); @@ -130,6 +143,7 @@ TEST_F(LoggingTest, FormatWithConstCharPlaceholder) { TEST_F(LoggingTest, FormatWithIntPlaceholder) { setLogger(mockLogger.get()); LOG(INFO, "My log message: {}", 4); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message: 4.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message: 4.*"))); @@ -138,6 +152,7 @@ TEST_F(LoggingTest, FormatWithIntPlaceholder) { TEST_F(LoggingTest, FormatWithMultiplePlaceholders) { setLogger(mockLogger.get()); LOG(INFO, "My log message: {}, {}, {}", 4, "then", true); + cpputils::logging::flush(); // For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? //EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message: 4, then, true.*")); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message: 4, then, true.*")));