Flush logs in test cases

This commit is contained in:
Sebastian Messmer 2021-12-11 11:15:40 +01:00
parent 69770c77d4
commit 67bd0ed295
2 changed files with 19 additions and 0 deletions

View File

@ -34,6 +34,10 @@ namespace cpputils {
logger().reset(); logger().reset();
} }
inline void flush() {
logger()->flush();
}
inline void setLevel(ERROR_TYPE) { inline void setLevel(ERROR_TYPE) {
logger().setLevel(spdlog::level::err); logger().setLevel(spdlog::level::err);
} }

View File

@ -12,6 +12,7 @@ using std::string;
TEST_F(LoggingTest, DefaultLoggerIsStderr) { TEST_F(LoggingTest, DefaultLoggerIsStderr) {
string output = captureStderr([]{ string output = captureStderr([]{
LOG(INFO, "My log message"); 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? // 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.*")); //EXPECT_THAT(output, MatchesRegex(".*\\[Log\\].*\\[info\\].*My log message.*"));
@ -22,6 +23,7 @@ TEST_F(LoggingTest, SetLogger_NewLoggerIsUsed) {
setLogger(spdlog::stderr_logger_mt("MyTestLog2")); setLogger(spdlog::stderr_logger_mt("MyTestLog2"));
string output = captureStderr([]{ string output = captureStderr([]{
LOG(INFO, "My log message"); 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? // 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.*")); //EXPECT_THAT(output, MatchesRegex(".*\\[MyTestLog2\\].*\\[info\\].*My log message.*"));
@ -31,6 +33,7 @@ TEST_F(LoggingTest, SetLogger_NewLoggerIsUsed) {
TEST_F(LoggingTest, SetNonStderrLogger_LogsToNewLogger) { TEST_F(LoggingTest, SetNonStderrLogger_LogsToNewLogger) {
setLogger(mockLogger.get()); setLogger(mockLogger.get());
logger()->info("My log message"); 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? // 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_THAT(output, MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[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()); setLogger(mockLogger.get());
string output = captureStderr([] { string output = captureStderr([] {
logger()->info("My log message"); logger()->info("My log message");
cpputils::logging::flush();
}); });
EXPECT_EQ("", output); EXPECT_EQ("", output);
} }
@ -47,6 +51,7 @@ TEST_F(LoggingTest, SetNonStderrLogger_DoesNotLogToStderr) {
TEST_F(LoggingTest, InfoLog) { TEST_F(LoggingTest, InfoLog) {
setLogger(mockLogger.get()); setLogger(mockLogger.get());
LOG(INFO, "My log message"); 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? // 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_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[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) { TEST_F(LoggingTest, WarningLog) {
setLogger(mockLogger.get()); setLogger(mockLogger.get());
LOG(WARN, "My log message"); 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? // 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_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[warning\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[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); setLevel(DEBUG);
setLogger(mockLogger.get()); setLogger(mockLogger.get());
LOG(DEBUG, "My log message"); 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? // 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_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[debug\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[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) { TEST_F(LoggingTest, ErrorLog) {
setLogger(mockLogger.get()); setLogger(mockLogger.get());
LOG(ERR, "My log message"); 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? // 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_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[error\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[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) { void logAndExit(const string &message) {
LOG(INFO, message); LOG(INFO, message);
cpputils::logging::flush();
exit(1); exit(1);
} }
@ -96,6 +105,7 @@ TEST_F(LoggingTest, LoggingAlsoWorksAfterFork) {
TEST_F(LoggingTest, MessageIsConstChar) { TEST_F(LoggingTest, MessageIsConstChar) {
setLogger(mockLogger.get()); setLogger(mockLogger.get());
LOG(INFO, "My log message"); 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? // 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_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[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()); setLogger(mockLogger.get());
string msg = "My log message"; string msg = "My log message";
LOG(INFO, msg); LOG(INFO, msg);
cpputils::logging::flush();
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string? // 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_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[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()); setLogger(mockLogger.get());
string str = "placeholder"; string str = "placeholder";
LOG(INFO, "My log message: {}", str); 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? // 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_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.*"))); 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) { TEST_F(LoggingTest, FormatWithConstCharPlaceholder) {
setLogger(mockLogger.get()); setLogger(mockLogger.get());
LOG(INFO, "My log message: {}", "placeholder"); 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? // 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_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.*"))); 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) { TEST_F(LoggingTest, FormatWithIntPlaceholder) {
setLogger(mockLogger.get()); setLogger(mockLogger.get());
LOG(INFO, "My log message: {}", 4); 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? // 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_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.*"))); 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) { TEST_F(LoggingTest, FormatWithMultiplePlaceholders) {
setLogger(mockLogger.get()); setLogger(mockLogger.get());
LOG(INFO, "My log message: {}, {}, {}", 4, "then", true); 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? // 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_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.*"))); EXPECT_TRUE(std::regex_search(mockLogger.capturedLog(), std::regex(".*\\[MockLogger\\].*\\[info\\].*My log message: 4, then, true.*")));