From 83864b315ff48bca33c39e1bdc03e522f68214fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 17 Jan 2019 15:19:13 +0100 Subject: testlib: Replace manual logger linked list with QVector Removes useless indirection and builds on existing primitives in Qt. Change-Id: I9fe50b21f5f77fc02566d5f5ff04c3e94c830e81 Reviewed-by: Edward Welbourne --- src/testlib/qtestlog.cpp | 182 ++++++++++++++--------------------------------- 1 file changed, 53 insertions(+), 129 deletions(-) (limited to 'src/testlib') diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp index c3e7385217..80f905e962 100644 --- a/src/testlib/qtestlog.cpp +++ b/src/testlib/qtestlog.cpp @@ -60,6 +60,7 @@ #include #include #include +#include #if QT_CONFIG(regularexpression) #include #endif @@ -93,6 +94,8 @@ 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) + namespace QTest { int fails = 0; @@ -160,109 +163,7 @@ namespace QTest { static IgnoreResultList *ignoreResultList = 0; - struct LoggerList - { - QAbstractTestLogger *logger; - LoggerList *next; - }; - - class TestLoggers - { - public: - static void addLogger(QAbstractTestLogger *logger) - { - LoggerList *l = new LoggerList; - l->logger = logger; - l->next = loggers; - loggers = l; - } - - static void destroyLoggers() - { - while (loggers) { - LoggerList *l = loggers; - loggers = loggers->next; - delete l->logger; - delete l; - } - } - -#define FOREACH_LOGGER(operation) \ - LoggerList *l = loggers; \ - while (l) { \ - QAbstractTestLogger *logger = l->logger; \ - Q_UNUSED(logger); \ - operation; \ - l = l->next; \ - } - - static void startLogging() - { - FOREACH_LOGGER(logger->startLogging()); - } - - static void stopLogging() - { - FOREACH_LOGGER(logger->stopLogging()); - } - - static void enterTestFunction(const char *function) - { - FOREACH_LOGGER(logger->enterTestFunction(function)); - } - - static void leaveTestFunction() - { - FOREACH_LOGGER(logger->leaveTestFunction()); - } - - static void enterTestData(QTestData *data) - { - FOREACH_LOGGER(logger->enterTestData(data)); - } - - static void addIncident(QAbstractTestLogger::IncidentTypes type, const char *description, - const char *file = 0, int line = 0) - { - FOREACH_LOGGER(logger->addIncident(type, description, file, line)); - } - - static void addBenchmarkResult(const QBenchmarkResult &result) - { - FOREACH_LOGGER(logger->addBenchmarkResult(result)); - } - - static void addMessage(QtMsgType type, const QMessageLogContext &context, - const QString &message) - { - FOREACH_LOGGER(logger->addMessage(type, context, message)); - } - - static void addMessage(QAbstractTestLogger::MessageTypes type, const QString &message, - const char *file = 0, int line = 0) - { - FOREACH_LOGGER(logger->addMessage(type, message, file, line)); - } - - static void outputString(const char *msg) - { - FOREACH_LOGGER(logger->outputString(msg)); - } - - static int loggerCount() - { - int count = 0; - FOREACH_LOGGER(++count); - return count; - } - - private: - static LoggerList *loggers; - }; - -#undef FOREACH_LOGGER - - LoggerList *TestLoggers::loggers = 0; + static QVector loggers; static bool loggerUsingStdout = false; static int verbosity = 0; @@ -301,10 +202,10 @@ namespace QTest { { static QBasicAtomicInt counter = Q_BASIC_ATOMIC_INITIALIZER(QTest::maxWarnings); - if (QTest::TestLoggers::loggerCount() == 0) { + if (QTestLog::loggerCount() == 0) { // if this goes wrong, something is seriously broken. qInstallMessageHandler(oldMessageHandler); - QTEST_ASSERT(QTest::TestLoggers::loggerCount() != 0); + QTEST_ASSERT(QTestLog::loggerCount() != 0); } if (handleIgnoredMessage(type, message)) { @@ -317,13 +218,16 @@ namespace QTest { return; if (!counter.deref()) { - QTest::TestLoggers::addMessage(QAbstractTestLogger::QSystem, + FOREACH_TEST_LOGGER { + logger->addMessage(QAbstractTestLogger::QSystem, QStringLiteral("Maximum amount of warnings exceeded. Use -maxwarnings to override.")); + } return; } } - QTest::TestLoggers::addMessage(type, context, message); + FOREACH_TEST_LOGGER + logger->addMessage(type, context, message); if (type == QtFatalMsg) { /* Right now, we're inside the custom message handler and we're @@ -346,13 +250,16 @@ void QTestLog::enterTestFunction(const char* function) QTEST_ASSERT(function); - QTest::TestLoggers::enterTestFunction(function); + FOREACH_TEST_LOGGER + logger->enterTestFunction(function); } void QTestLog::enterTestData(QTestData *data) { QTEST_ASSERT(data); - QTest::TestLoggers::enterTestData(data); + + FOREACH_TEST_LOGGER + logger->enterTestData(data); } int QTestLog::unhandledIgnoreMessages() @@ -371,7 +278,8 @@ void QTestLog::leaveTestFunction() if (printAvailableTags) return; - QTest::TestLoggers::leaveTestFunction(); + FOREACH_TEST_LOGGER + logger->leaveTestFunction(); } void QTestLog::printUnhandledIgnoreMessages() @@ -386,7 +294,8 @@ void QTestLog::printUnhandledIgnoreMessages() message = QStringLiteral("Did not receive any message matching: \"") + list->pattern.toRegularExpression().pattern() + QLatin1Char('"'); #endif } - QTest::TestLoggers::addMessage(QAbstractTestLogger::Info, message); + FOREACH_TEST_LOGGER + logger->addMessage(QAbstractTestLogger::Info, message); list = list->next; } @@ -406,7 +315,8 @@ void QTestLog::addPass(const char *msg) ++QTest::passes; - QTest::TestLoggers::addIncident(QAbstractTestLogger::Pass, msg); + FOREACH_TEST_LOGGER + logger->addIncident(QAbstractTestLogger::Pass, msg); } void QTestLog::addFail(const char *msg, const char *file, int line) @@ -415,7 +325,8 @@ void QTestLog::addFail(const char *msg, const char *file, int line) ++QTest::fails; - QTest::TestLoggers::addIncident(QAbstractTestLogger::Fail, msg, file, line); + FOREACH_TEST_LOGGER + logger->addIncident(QAbstractTestLogger::Fail, msg, file, line); } void QTestLog::addXFail(const char *msg, const char *file, int line) @@ -423,7 +334,8 @@ void QTestLog::addXFail(const char *msg, const char *file, int line) QTEST_ASSERT(msg); QTEST_ASSERT(file); - QTest::TestLoggers::addIncident(QAbstractTestLogger::XFail, msg, file, line); + FOREACH_TEST_LOGGER + logger->addIncident(QAbstractTestLogger::XFail, msg, file, line); } void QTestLog::addXPass(const char *msg, const char *file, int line) @@ -433,7 +345,8 @@ void QTestLog::addXPass(const char *msg, const char *file, int line) ++QTest::fails; - QTest::TestLoggers::addIncident(QAbstractTestLogger::XPass, msg, file, line); + FOREACH_TEST_LOGGER + logger->addIncident(QAbstractTestLogger::XPass, msg, file, line); } void QTestLog::addBPass(const char *msg) @@ -442,7 +355,8 @@ void QTestLog::addBPass(const char *msg) ++QTest::blacklists; - QTest::TestLoggers::addIncident(QAbstractTestLogger::BlacklistedPass, msg); + FOREACH_TEST_LOGGER + logger->addIncident(QAbstractTestLogger::BlacklistedPass, msg); } void QTestLog::addBFail(const char *msg, const char *file, int line) @@ -452,7 +366,8 @@ void QTestLog::addBFail(const char *msg, const char *file, int line) ++QTest::blacklists; - QTest::TestLoggers::addIncident(QAbstractTestLogger::BlacklistedFail, msg, file, line); + FOREACH_TEST_LOGGER + logger->addIncident(QAbstractTestLogger::BlacklistedFail, msg, file, line); } void QTestLog::addBXPass(const char *msg, const char *file, int line) @@ -462,7 +377,8 @@ void QTestLog::addBXPass(const char *msg, const char *file, int line) ++QTest::blacklists; - QTest::TestLoggers::addIncident(QAbstractTestLogger::BlacklistedXPass, msg, file, line); + FOREACH_TEST_LOGGER + logger->addIncident(QAbstractTestLogger::BlacklistedXPass, msg, file, line); } void QTestLog::addBXFail(const char *msg, const char *file, int line) @@ -472,7 +388,8 @@ void QTestLog::addBXFail(const char *msg, const char *file, int line) ++QTest::blacklists; - QTest::TestLoggers::addIncident(QAbstractTestLogger::BlacklistedXFail, msg, file, line); + FOREACH_TEST_LOGGER + logger->addIncident(QAbstractTestLogger::BlacklistedXFail, msg, file, line); } void QTestLog::addSkip(const char *msg, const char *file, int line) @@ -482,27 +399,33 @@ void QTestLog::addSkip(const char *msg, const char *file, int line) ++QTest::skips; - QTest::TestLoggers::addMessage(QAbstractTestLogger::Skip, QString::fromUtf8(msg), file, line); + FOREACH_TEST_LOGGER + logger->addMessage(QAbstractTestLogger::Skip, QString::fromUtf8(msg), file, line); } void QTestLog::addBenchmarkResult(const QBenchmarkResult &result) { - QTest::TestLoggers::addBenchmarkResult(result); + FOREACH_TEST_LOGGER + logger->addBenchmarkResult(result); } void QTestLog::startLogging() { elapsedTotalTime.start(); elapsedFunctionTime.start(); - QTest::TestLoggers::startLogging(); + FOREACH_TEST_LOGGER + logger->startLogging(); QTest::oldMessageHandler = qInstallMessageHandler(QTest::messageHandler); } void QTestLog::stopLogging() { qInstallMessageHandler(QTest::oldMessageHandler); - QTest::TestLoggers::stopLogging(); - QTest::TestLoggers::destroyLoggers(); + FOREACH_TEST_LOGGER { + logger->stopLogging(); + delete logger; + } + QTest::loggers.clear(); QTest::loggerUsingStdout = false; saveCoverageTool(QTestResult::currentAppName(), failCount() != 0, QTestLog::installedTestCoverage()); } @@ -553,12 +476,12 @@ void QTestLog::addLogger(LogMode mode, const char *filename) #endif QTEST_ASSERT(logger); - QTest::TestLoggers::addLogger(logger); + QTest::loggers.append(logger); } int QTestLog::loggerCount() { - return QTest::TestLoggers::loggerCount(); + return QTest::loggers.size(); } bool QTestLog::loggerUsingStdout() @@ -570,15 +493,16 @@ void QTestLog::warn(const char *msg, const char *file, int line) { QTEST_ASSERT(msg); - if (QTest::TestLoggers::loggerCount() > 0) - QTest::TestLoggers::addMessage(QAbstractTestLogger::Warn, QString::fromUtf8(msg), file, line); + FOREACH_TEST_LOGGER + logger->addMessage(QAbstractTestLogger::Warn, QString::fromUtf8(msg), file, line); } void QTestLog::info(const char *msg, const char *file, int line) { QTEST_ASSERT(msg); - QTest::TestLoggers::addMessage(QAbstractTestLogger::Info, QString::fromUtf8(msg), file, line); + FOREACH_TEST_LOGGER + logger->addMessage(QAbstractTestLogger::Info, QString::fromUtf8(msg), file, line); } void QTestLog::setVerboseLevel(int level) -- cgit v1.2.3 From 18da1dac20c7d27342e987586a16d125e39197c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 17 Jan 2019 16:03:03 +0100 Subject: testlib: Prevent Apple test logger from stomping on other loggers We were potentially adding the Apple test logger multiple times, and we didn't consider whether the existing loggers were logging to file or not when circumventing them. We now don't use the Apple logger if it would touch stderr and some other logger is using stdout. In the case of no explicit logger being specified on the command line, we allow the Apple logger to take priority over the default plain test logger. Change-Id: I31bbec4f4b3ab84ba9a2be35e8e5db08fee071a7 Reviewed-by: Edward Welbourne --- src/testlib/qappletestlogger.cpp | 20 ++++++-------------- src/testlib/qappletestlogger_p.h | 17 ++++------------- src/testlib/qtestcase.cpp | 31 +++++++++++++++++++++++++------ src/testlib/qtestlog.cpp | 13 +++++-------- src/testlib/qtestlog_p.h | 11 +++++++++-- 5 files changed, 49 insertions(+), 43 deletions(-) (limited to 'src/testlib') diff --git a/src/testlib/qappletestlogger.cpp b/src/testlib/qappletestlogger.cpp index 8e75da88f8..959ff6cf64 100644 --- a/src/testlib/qappletestlogger.cpp +++ b/src/testlib/qappletestlogger.cpp @@ -55,9 +55,8 @@ bool QAppleTestLogger::debugLoggingEnabled() return os_log_type_enabled(OS_LOG_DEFAULT, OS_LOG_TYPE_DEBUG); } -QAppleTestLogger::QAppleTestLogger(QAbstractTestLogger *logger) +QAppleTestLogger::QAppleTestLogger() : QAbstractTestLogger(nullptr) - , m_logger(logger) { } @@ -65,6 +64,8 @@ static QAppleLogActivity testFunctionActivity; void QAppleTestLogger::enterTestFunction(const char *function) { + Q_UNUSED(function); + // Re-create activity each time testFunctionActivity = QT_APPLE_LOG_ACTIVITY("Running test function").enter(); @@ -73,15 +74,12 @@ void QAppleTestLogger::enterTestFunction(const char *function) QString identifier = QString::fromLatin1(testIdentifier.data()); QMessageLogContext context(nullptr, 0, nullptr, "qt.test.enter"); QString message = identifier; - if (AppleUnifiedLogger::messageHandler(QtDebugMsg, context, message, identifier)) - return; // AUL already printed to stderr - m_logger->enterTestFunction(function); + AppleUnifiedLogger::messageHandler(QtDebugMsg, context, message, identifier); } void QAppleTestLogger::leaveTestFunction() { - m_logger->leaveTestFunction(); testFunctionActivity.leave(); } @@ -134,18 +132,12 @@ void QAppleTestLogger::addIncident(IncidentTypes type, const char *description, if (qstrlen(description)) message += QLatin1Char('\n') % QString::fromLatin1(description); - if (AppleUnifiedLogger::messageHandler(incidentClassification.first, context, message, subsystem)) - return; // AUL already printed to stderr - - m_logger->addIncident(type, description, file, line); + AppleUnifiedLogger::messageHandler(incidentClassification.first, context, message, subsystem); } void QAppleTestLogger::addMessage(QtMsgType type, const QMessageLogContext &context, const QString &message) { - if (AppleUnifiedLogger::messageHandler(type, context, message)) - return; // AUL already printed to stderr - - m_logger->addMessage(type, context, message); + AppleUnifiedLogger::messageHandler(type, context, message); } #endif // QT_USE_APPLE_UNIFIED_LOGGING diff --git a/src/testlib/qappletestlogger_p.h b/src/testlib/qappletestlogger_p.h index 5a45fad7a0..4217f4e6a2 100644 --- a/src/testlib/qappletestlogger_p.h +++ b/src/testlib/qappletestlogger_p.h @@ -63,12 +63,7 @@ class QAppleTestLogger : public QAbstractTestLogger public: static bool debugLoggingEnabled(); - QAppleTestLogger(QAbstractTestLogger *logger); - - void startLogging() override - { m_logger->startLogging(); } - void stopLogging() override - { m_logger->stopLogging(); } + QAppleTestLogger(); void enterTestFunction(const char *function) override; void leaveTestFunction() override; @@ -77,16 +72,12 @@ public: const char *file = 0, int line = 0) override; void addMessage(QtMsgType, const QMessageLogContext &, const QString &) override; - - void addBenchmarkResult(const QBenchmarkResult &result) override - { m_logger->addBenchmarkResult(result); } - void addMessage(MessageTypes type, const QString &message, const char *file = 0, int line = 0) override - { m_logger->addMessage(type, message, file, line); } + { Q_UNUSED(type); Q_UNUSED(message); Q_UNUSED(file); Q_UNUSED(line); Q_UNREACHABLE(); } -private: - QScopedPointer m_logger; + void addBenchmarkResult(const QBenchmarkResult &result) override + { Q_UNUSED(result); } }; #endif diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 22e8ac49bc..0c935a1f1c 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -77,6 +77,10 @@ #include #endif +#if defined(Q_OS_DARWIN) +#include +#endif + #include #include #include @@ -509,7 +513,7 @@ static int qToInt(char *str) Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) { - QTestLog::LogMode logFormat = QTestLog::Plain; + int logFormat = -1; // Not set const char *logFilename = 0; QTest::testFunctions.clear(); @@ -677,7 +681,7 @@ Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) fprintf(stderr, "only one logger can log to stdout\n"); exit(1); } - QTestLog::addLogger(logFormat, filename); + QTestLog::addLogger(QTestLog::LogMode(logFormat), filename); } delete [] filename; delete [] format; @@ -839,10 +843,25 @@ Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) QTestLog::setInstalledTestCoverage(installedTestCoverage); // If no loggers were created by the long version of the -o command-line - // option, create a logger using whatever filename and format were - // set using the old-style command-line options. - if (QTestLog::loggerCount() == 0) - QTestLog::addLogger(logFormat, logFilename); + // option, but a logger was requested via the old-style option, add it. + const bool explicitLoggerRequested = logFormat != -1; + if (QTestLog::loggerCount() == 0 && explicitLoggerRequested) + QTestLog::addLogger(QTestLog::LogMode(logFormat), logFilename); + + bool addFallbackLogger = !explicitLoggerRequested; + +#if defined(QT_USE_APPLE_UNIFIED_LOGGING) + // Any explicitly requested loggers will be added by now, so we can check if they use stdout + const bool safeToAddAppleLogger = !AppleUnifiedLogger::willMirrorToStderr() || !QTestLog::loggerUsingStdout(); + if (safeToAddAppleLogger && QAppleTestLogger::debugLoggingEnabled()) { + QTestLog::addLogger(QTestLog::Apple, nullptr); + if (AppleUnifiedLogger::willMirrorToStderr() && !logFilename) + addFallbackLogger = false; // Prevent plain test logger fallback below + } +#endif + + if (addFallbackLogger) + QTestLog::addLogger(QTestLog::Plain, logFilename); } QBenchmarkResult qMedian(const QVector &container) diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp index 80f905e962..57bb7d95a7 100644 --- a/src/testlib/qtestlog.cpp +++ b/src/testlib/qtestlog.cpp @@ -460,6 +460,11 @@ void QTestLog::addLogger(LogMode mode, const char *filename) case QTestLog::TAP: logger = new QTapTestLogger(filename); break; +#if defined(QT_USE_APPLE_UNIFIED_LOGGING) + case QTestLog::Apple: + logger = new QAppleTestLogger; + break; +#endif #if defined(HAVE_XCTEST) case QTestLog::XCTest: logger = new QXcodeTestLogger; @@ -467,14 +472,6 @@ void QTestLog::addLogger(LogMode mode, const char *filename) #endif } -#if defined(QT_USE_APPLE_UNIFIED_LOGGING) - // Logger that also feeds messages to AUL. It needs to wrap the existing - // logger, as it needs to be able to short circuit the existing logger - // in case AUL prints to stderr. - if (QAppleTestLogger::debugLoggingEnabled()) - logger = new QAppleTestLogger(logger); -#endif - QTEST_ASSERT(logger); QTest::loggers.append(logger); } diff --git a/src/testlib/qtestlog_p.h b/src/testlib/qtestlog_p.h index 0bdd6290e1..e63e89a78e 100644 --- a/src/testlib/qtestlog_p.h +++ b/src/testlib/qtestlog_p.h @@ -53,6 +53,10 @@ #include +#if defined(Q_OS_DARWIN) +#include +#endif + QT_BEGIN_NAMESPACE class QBenchmarkResult; @@ -63,9 +67,12 @@ class Q_TESTLIB_EXPORT QTestLog { public: enum LogMode { - Plain = 0, XML, LightXML, XunitXML, CSV, TeamCity, TAP, + Plain = 0, XML, LightXML, XunitXML, CSV, TeamCity, TAP +#if defined(QT_USE_APPLE_UNIFIED_LOGGING) + , Apple +#endif #if defined(HAVE_XCTEST) - XCTest + , XCTest #endif }; -- cgit v1.2.3