summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-11-08 13:55:51 +0100
committerUlf Hermann <ulf.hermann@qt.io>2021-11-09 21:36:50 +0100
commit81c92aec66dc71cbea932f12606c9f6c546a8ac1 (patch)
treee8fa17d48b9f7462bb9c4c8634fd3b3b45529c8e
parenta4ce85f356b78401fe727a07b908a1e7b5a25198 (diff)
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>
-rw-r--r--src/testlib/qtestcase.cpp2
-rw-r--r--src/testlib/qtestlog.cpp18
-rw-r--r--src/testlib/qtestlog_p.h2
3 files changed, 12 insertions, 10 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index ce10b0a8eb..a160b07bde 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -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;
diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp
index 7e311d273e..cebe548ec6 100644
--- a/src/testlib/qtestlog.cpp
+++ b/src/testlib/qtestlog.cpp
@@ -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()
diff --git a/src/testlib/qtestlog_p.h b/src/testlib/qtestlog_p.h
index bdb22acbd3..c422aadbf8 100644
--- a/src/testlib/qtestlog_p.h
+++ b/src/testlib/qtestlog_p.h
@@ -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);