Skip to content

Commit

Permalink
Updated logger tests.
Browse files Browse the repository at this point in the history
- Changed `pcpp::Logger::getInstance()` to use a cached `logger` variable.
- Removed the need for fully qualified names in the logger test.
- C-style casts to Cpp casts.
  • Loading branch information
Dimi1010 committed Jan 3, 2025
1 parent 421a419 commit d9df72b
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions Tests/Pcap++Test/Tests/LoggerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,24 +190,34 @@ PTF_TEST_CASE(TestLoggerMultiThread)

PTF_TEST_CASE(TestLogger)
{
using pcpp::Logger;
using pcpp::LogLevel;
using pcpp::LogModule;

auto& logger = Logger::getInstance();

// cppcheck-suppress unusedVariable
LoggerCleaner loggerCleaner;

// verify all modules are on info log level
for (int module = 1; module < pcpp::NumOfLogModules; module++)
for (int moduleInt = 1; moduleInt < LogModule::NumOfLogModules; moduleInt++)
{
PTF_ASSERT_EQUAL(pcpp::Logger::getInstance().getLogLevel((pcpp::LogModule)module), pcpp::Logger::Info, enum);
PTF_ASSERT_FALSE(pcpp::Logger::getInstance().isDebugEnabled((pcpp::LogModule)module));
const LogModule moduleEnum = static_cast<LogModule>(moduleInt);

PTF_ASSERT_EQUAL(logger.getLogLevel(moduleEnum), LogLevel::Info, enum);
PTF_ASSERT_FALSE(logger.isDebugEnabled(moduleEnum));
}

// invoke debug and error logs - expect to see only the error log
pcpp::Logger::getInstance().setLogPrinter(&LogPrinter::logPrinter);
logger.setLogPrinter(&LogPrinter::logPrinter);

pcpp::invokeDebugLog();
PTF_ASSERT_EQUAL(LogPrinter::lastLogLevelSeen, 999);
PTF_ASSERT_EQUAL(LogPrinter::lastLineSeen, 99999);
PTF_ASSERT_NULL(LogPrinter::lastLogMessageSeen);
PTF_ASSERT_NULL(LogPrinter::lastFilenameSeen);
PTF_ASSERT_NULL(LogPrinter::lastMethodSeen);

pcpp::invokeErrorLog();
PTF_ASSERT_EQUAL(LogPrinter::lastLogLevelSeen, (int)pcpp::Logger::Error);
PTF_ASSERT_EQUAL(*LogPrinter::lastLogMessageSeen, "error log");
Expand All @@ -216,9 +226,10 @@ PTF_TEST_CASE(TestLogger)
PTF_ASSERT_EQUAL(LogPrinter::lastLineSeen, 21);

// change one module log level
pcpp::Logger::getInstance().setLogLevel(pcpp::PacketLogModuleArpLayer, pcpp::Logger::Debug);
PTF_ASSERT_EQUAL(pcpp::Logger::getInstance().getLogLevel(pcpp::PacketLogModuleArpLayer), pcpp::Logger::Debug, enum);
PTF_ASSERT_TRUE(pcpp::Logger::getInstance().isDebugEnabled(pcpp::PacketLogModuleArpLayer));
logger.setLogLevel(pcpp::PacketLogModuleArpLayer, pcpp::Logger::Debug);
PTF_ASSERT_EQUAL(logger.getLogLevel(pcpp::PacketLogModuleArpLayer), pcpp::LogLevel::Debug,
enum);
PTF_ASSERT_TRUE(logger.isDebugEnabled(pcpp::PacketLogModuleArpLayer));

// invoke debug and error logs - expect to see both
pcpp::invokeDebugLog();
Expand All @@ -236,14 +247,16 @@ PTF_TEST_CASE(TestLogger)
PTF_ASSERT_EQUAL(LogPrinter::lastLineSeen, 21);

// verify the last error message
PTF_ASSERT_EQUAL(pcpp::Logger::getInstance().getLastError(), "error log");
PTF_ASSERT_EQUAL(logger.getLastError(), "error log");

// change all modules log level
pcpp::Logger::getInstance().setAllModulesToLogLevel(pcpp::Logger::Debug);
for (int module = 1; module < pcpp::NumOfLogModules; module++)
logger.setAllModulesToLogLevel(LogLevel::Debug);
for (int moduleInt = 1; moduleInt < LogModule::NumOfLogModules; moduleInt++)
{
PTF_ASSERT_EQUAL(pcpp::Logger::getInstance().getLogLevel((pcpp::LogModule)module), pcpp::Logger::Debug, enum);
PTF_ASSERT_TRUE(pcpp::Logger::getInstance().isDebugEnabled((pcpp::LogModule)module));
auto const moduleEnum = static_cast<LogModule>(moduleInt);

PTF_ASSERT_EQUAL(logger.getLogLevel(static_cast<LogModule>(moduleEnum)), pcpp::LogLevel::Debug, enum);
PTF_ASSERT_TRUE(logger.isDebugEnabled(static_cast<LogModule>(moduleEnum)));
}

// invoke debug log - expect to see it
Expand All @@ -255,9 +268,9 @@ PTF_TEST_CASE(TestLogger)
PTF_ASSERT_EQUAL(LogPrinter::lastLineSeen, 16);

// suppress logs
PTF_ASSERT_TRUE(pcpp::Logger::getInstance().logsEnabled())
pcpp::Logger::getInstance().suppressLogs();
PTF_ASSERT_FALSE(pcpp::Logger::getInstance().logsEnabled())
PTF_ASSERT_TRUE(logger.logsEnabled())
logger.suppressLogs();
PTF_ASSERT_FALSE(logger.logsEnabled())

// reset LogPrinter
LogPrinter::clean();
Expand All @@ -270,32 +283,32 @@ PTF_TEST_CASE(TestLogger)

// invoke another error log - expect to see it as the last error message although logs are suppressed
pcpp::invokeErrorLog("2");
PTF_ASSERT_EQUAL(pcpp::Logger::getInstance().getLastError(), "error log2");
PTF_ASSERT_EQUAL(logger.getLastError(), "error log2");

// re-enable logs
pcpp::Logger::getInstance().enableLogs();
PTF_ASSERT_TRUE(pcpp::Logger::getInstance().logsEnabled())
logger.enableLogs();
PTF_ASSERT_TRUE(logger.logsEnabled())

// invoke error log - expect to see it
pcpp::invokeErrorLog();
PTF_ASSERT_EQUAL(LogPrinter::lastLogLevelSeen, (int)pcpp::Logger::Error);
PTF_ASSERT_EQUAL(LogPrinter::lastLogLevelSeen, static_cast<int>(pcpp::LogLevel::Error));
PTF_ASSERT_EQUAL(*LogPrinter::lastLogMessageSeen, "error log");
PTF_ASSERT_EQUAL(getLowerCaseFileName(*LogPrinter::lastFilenameSeen), "loggertests.cpp");
PTF_ASSERT_EQUAL(getMethodWithoutNamespace(*LogPrinter::lastMethodSeen), "invokeErrorLog");
PTF_ASSERT_EQUAL(pcpp::Logger::getInstance().getLastError(), "error log");
PTF_ASSERT_EQUAL(logger.getLastError(), "error log");
PTF_ASSERT_EQUAL(LogPrinter::lastLineSeen, 21);

// reset LogPrinter
LogPrinter::clean();

// reset the log printer
pcpp::Logger::getInstance().resetLogPrinter();
logger.resetLogPrinter();

// disable std::cout for a bit
std::cout.setstate(std::ios_base::failbit);

// set debug log for a module, don't expect to see it in the custom log printer
pcpp::Logger::getInstance().setLogLevel(pcpp::PacketLogModuleArpLayer, pcpp::Logger::Debug);
logger.setLogLevel(pcpp::PacketLogModuleArpLayer, pcpp::LogLevel::Debug);
pcpp::invokeDebugLog();
pcpp::invokeErrorLog();
PTF_ASSERT_EQUAL(LogPrinter::lastLogLevelSeen, 999);
Expand Down

0 comments on commit d9df72b

Please sign in to comment.