2015-10-17 15:49:54 +02:00
|
|
|
#include "testutils/LoggingTest.h"
|
2018-07-09 04:34:08 +02:00
|
|
|
#include <regex>
|
2015-10-17 15:49:54 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Contains test cases for the following logging interface:
|
2017-02-04 20:03:20 +01:00
|
|
|
* LOG(INFO, "My log message)"
|
2015-10-17 15:49:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
using namespace cpputils::logging;
|
|
|
|
using std::string;
|
|
|
|
|
2021-12-11 23:06:01 +01:00
|
|
|
// Disable the next tests for MSVC debug builds since writing to stderr doesn't seem to work well there
|
|
|
|
#if !defined(_MSC_VER) || NDEBUG
|
|
|
|
|
2015-10-17 16:49:58 +02:00
|
|
|
TEST_F(LoggingTest, DefaultLoggerIsStderr) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const string output = captureStderr([]{
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(INFO, "My log message");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2015-10-17 15:49:54 +02:00
|
|
|
});
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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_TRUE(std::regex_search(output, std::regex(".*\\[Log\\].*\\[info\\].*My log message.*")));
|
2015-10-17 15:49:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, SetLogger_NewLoggerIsUsed) {
|
2015-10-17 16:49:58 +02:00
|
|
|
setLogger(spdlog::stderr_logger_mt("MyTestLog2"));
|
2023-07-08 23:48:59 +02:00
|
|
|
const string output = captureStderr([]{
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(INFO, "My log message");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2015-10-17 15:49:54 +02:00
|
|
|
});
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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_TRUE(std::regex_search(output, std::regex(".*\\[MyTestLog2\\].*\\[info\\].*My log message.*")));
|
2015-10-17 15:49:54 +02:00
|
|
|
}
|
|
|
|
|
2021-12-11 23:06:01 +01:00
|
|
|
#endif
|
|
|
|
|
2015-10-17 16:49:58 +02:00
|
|
|
TEST_F(LoggingTest, SetNonStderrLogger_LogsToNewLogger) {
|
2015-10-17 15:49:54 +02:00
|
|
|
setLogger(mockLogger.get());
|
2017-02-04 20:03:20 +01:00
|
|
|
logger()->info("My log message");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2015-10-17 15:49:54 +02:00
|
|
|
}
|
|
|
|
|
2015-10-17 16:49:58 +02:00
|
|
|
TEST_F(LoggingTest, SetNonStderrLogger_DoesNotLogToStderr) {
|
2015-10-17 15:49:54 +02:00
|
|
|
setLogger(mockLogger.get());
|
2023-07-08 23:48:59 +02:00
|
|
|
const string output = captureStderr([] {
|
2017-02-04 20:03:20 +01:00
|
|
|
logger()->info("My log message");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2015-10-17 15:49:54 +02:00
|
|
|
});
|
|
|
|
EXPECT_EQ("", output);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, InfoLog) {
|
|
|
|
setLogger(mockLogger.get());
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(INFO, "My log message");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2015-10-17 15:49:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, WarningLog) {
|
|
|
|
setLogger(mockLogger.get());
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(WARN, "My log message");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2015-10-17 15:49:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, DebugLog) {
|
|
|
|
setLevel(DEBUG);
|
|
|
|
setLogger(mockLogger.get());
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(DEBUG, "My log message");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2015-10-17 15:49:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, ErrorLog) {
|
|
|
|
setLogger(mockLogger.get());
|
2018-05-17 08:03:03 +02:00
|
|
|
LOG(ERR, "My log message");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2015-10-17 15:49:54 +02:00
|
|
|
}
|
2015-10-17 16:42:56 +02:00
|
|
|
|
|
|
|
void logAndExit(const string &message) {
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(INFO, message);
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2015-10-25 18:42:38 +01:00
|
|
|
exit(1);
|
2015-10-17 16:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fork() only forks the main thread. This test ensures that logging doesn't depend on threads that suddenly aren't
|
|
|
|
// there anymore after a fork().
|
2015-10-28 15:20:55 +01:00
|
|
|
TEST_F(LoggingTest, LoggingAlsoWorksAfterFork) {
|
2015-10-17 16:49:58 +02:00
|
|
|
setLogger(spdlog::stderr_logger_mt("StderrLogger"));
|
2015-10-25 18:42:38 +01:00
|
|
|
EXPECT_EXIT(
|
2015-10-17 16:42:56 +02:00
|
|
|
logAndExit("My log message"),
|
2015-10-25 18:42:38 +01:00
|
|
|
::testing::ExitedWithCode(1),
|
2015-10-17 16:42:56 +02:00
|
|
|
"My log message"
|
|
|
|
);
|
|
|
|
}
|
2017-02-04 20:03:20 +01:00
|
|
|
|
|
|
|
TEST_F(LoggingTest, MessageIsConstChar) {
|
|
|
|
setLogger(mockLogger.get());
|
|
|
|
LOG(INFO, "My log message");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2017-02-04 20:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, MessageIsString) {
|
|
|
|
setLogger(mockLogger.get());
|
2023-07-08 23:48:59 +02:00
|
|
|
const string msg = "My log message";
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(INFO, msg);
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2017-02-04 20:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, FormatWithStringPlaceholder) {
|
|
|
|
setLogger(mockLogger.get());
|
2023-07-08 23:48:59 +02:00
|
|
|
const string str = "placeholder";
|
2017-02-04 20:03:20 +01:00
|
|
|
LOG(INFO, "My log message: {}", str);
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2017-02-04 20:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, FormatWithConstCharPlaceholder) {
|
|
|
|
setLogger(mockLogger.get());
|
|
|
|
LOG(INFO, "My log message: {}", "placeholder");
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2017-02-04 20:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, FormatWithIntPlaceholder) {
|
|
|
|
setLogger(mockLogger.get());
|
|
|
|
LOG(INFO, "My log message: {}", 4);
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2017-02-04 20:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LoggingTest, FormatWithMultiplePlaceholders) {
|
|
|
|
setLogger(mockLogger.get());
|
|
|
|
LOG(INFO, "My log message: {}, {}, {}", 4, "then", true);
|
2021-12-11 11:15:40 +01:00
|
|
|
cpputils::logging::flush();
|
2018-07-09 04:34:08 +02:00
|
|
|
// 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.*")));
|
2017-02-04 20:03:20 +01:00
|
|
|
}
|