summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestlog.cpp
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 /src/testlib/qtestlog.cpp
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>
Diffstat (limited to 'src/testlib/qtestlog.cpp')
-rw-r--r--src/testlib/qtestlog.cpp18
1 files changed, 10 insertions, 8 deletions
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()