QTestLog: Properly own the loggers

Previously, the loggers would leak if the application failed to call
stopLogging(). Now they are owned by the global static which will delete
them in that case.

Also, since we have to adapt loggerCount() to the fact that std::vector
uses size_t, recognize that we only ever want to know whether the number
of loggers is 0. Change the method to only provide that information
rather than the actual number.

Change-Id: Ieb2e185048d573ec7f36373ad49bb2a0ca391ce3
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Ulf Hermann 2021-11-08 13:55:51 +01:00
parent a4ce85f356
commit 81c92aec66
3 changed files with 12 additions and 10 deletions

View File

@ -863,7 +863,7 @@ Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, const char *const argv[], bool
// If no loggers were created by the long version of the -o command-line
// option, but a logger was requested via the old-style option, add it.
const bool explicitLoggerRequested = logFormat != -1;
if (QTestLog::loggerCount() == 0 && explicitLoggerRequested)
if (!QTestLog::hasLoggers() && explicitLoggerRequested)
QTestLog::addLogger(QTestLog::LogMode(logFormat), logFilename);
bool addFallbackLogger = !explicitLoggerRequested;

View File

@ -69,6 +69,9 @@
#include <string.h>
#include <limits.h>
#include <vector>
#include <memory>
QT_BEGIN_NAMESPACE
static void saveCoverageTool(const char * appname, bool testfailed, bool installedTestCoverage)
@ -99,7 +102,7 @@ static void saveCoverageTool(const char * appname, bool testfailed, bool install
static QElapsedTimer elapsedFunctionTime;
static QElapsedTimer elapsedTotalTime;
#define FOREACH_TEST_LOGGER for (QAbstractTestLogger *logger : *QTest::loggers())
#define FOREACH_TEST_LOGGER for (const auto &logger : qAsConst(*QTest::loggers()))
namespace QTest {
@ -168,7 +171,7 @@ namespace QTest {
static IgnoreResultList *ignoreResultList = nullptr;
Q_GLOBAL_STATIC(QList<QAbstractTestLogger *>, loggers)
Q_GLOBAL_STATIC(std::vector<std::unique_ptr<QAbstractTestLogger>>, loggers)
static int verbosity = 0;
static int maxWarnings = 2002;
@ -206,10 +209,10 @@ namespace QTest {
{
static QBasicAtomicInt counter = Q_BASIC_ATOMIC_INITIALIZER(QTest::maxWarnings);
if (QTestLog::loggerCount() == 0) {
if (!QTestLog::hasLoggers()) {
// if this goes wrong, something is seriously broken.
qInstallMessageHandler(oldMessageHandler);
QTEST_ASSERT(QTestLog::loggerCount() != 0);
QTEST_ASSERT(QTestLog::hasLoggers());
}
if (handleIgnoredMessage(type, message)) {
@ -423,7 +426,6 @@ void QTestLog::stopLogging()
qInstallMessageHandler(QTest::oldMessageHandler);
FOREACH_TEST_LOGGER {
logger->stopLogging();
delete logger;
}
QTest::loggers()->clear();
saveCoverageTool(QTestResult::currentAppName(), failCount() != 0, QTestLog::installedTestCoverage());
@ -484,12 +486,12 @@ void QTestLog::addLogger(LogMode mode, const char *filename)
void QTestLog::addLogger(QAbstractTestLogger *logger)
{
QTEST_ASSERT(logger);
QTest::loggers()->append(logger);
QTest::loggers()->emplace_back(logger);
}
int QTestLog::loggerCount()
bool QTestLog::hasLoggers()
{
return QTest::loggers()->size();
return !QTest::loggers()->empty();
}
bool QTestLog::loggerUsingStdout()

View File

@ -118,7 +118,7 @@ public:
static void addLogger(LogMode mode, const char *filename);
static void addLogger(QAbstractTestLogger *logger);
static int loggerCount();
static bool hasLoggers();
static bool loggerUsingStdout();
static void setVerboseLevel(int level);