From aec75990e86663a95124f8b3021a9d5c5c5f1025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomi=20Lepp=C3=A4nen?= Date: Mon, 1 Feb 2021 17:03:05 +0200 Subject: [PATCH] [qtbase] Allow reinitializing logging. Contributes to JB#52717 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We need this in MDeclarativeCache of mapplauncherd-qt5 to reload the rules and other logging options after environment variables have been updated. Signed-off-by: Tomi Leppänen --- src/corelib/global/qlogging.cpp | 43 ++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 6b95449b3d..635b105169 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -147,18 +147,29 @@ Q_NORETURN static void qt_message_fatal(QtMsgType, const QMessageLogContext &context, const QString &message); static void qt_message_print(QtMsgType, const QMessageLogContext &context, const QString &message); +static bool fatalCriticalsInitialized = false; +static bool fatalWarningsInitialized = false; + static bool isFatal(QtMsgType msgType) { if (msgType == QtFatalMsg) return true; if (msgType == QtCriticalMsg) { - static bool fatalCriticals = !qEnvironmentVariableIsEmpty("QT_FATAL_CRITICALS"); + static bool fatalCriticals = false; + if (!fatalCriticalsInitialized) { + fatalCriticals = !qEnvironmentVariableIsEmpty("QT_FATAL_CRITICALS"); + fatalCriticalsInitialized = true; + } return fatalCriticals; } if (msgType == QtWarningMsg || msgType == QtCriticalMsg) { - static bool fatalWarnings = !qEnvironmentVariableIsEmpty("QT_FATAL_WARNINGS"); + static bool fatalWarnings = false; + if (!fatalWarningsInitialized) { + fatalWarnings = !qEnvironmentVariableIsEmpty("QT_FATAL_WARNINGS"); + fatalWarningsInitialized = true; + } return fatalWarnings; } @@ -208,9 +219,15 @@ static bool willLogToConsole() #endif } +static bool logToConsoleInitialized = false; + Q_CORE_EXPORT bool qt_logging_to_console() { - static const bool logToConsole = willLogToConsole(); + static bool logToConsole = false; + if (!logToConsoleInitialized) { + logToConsole = willLogToConsole(); + logToConsoleInitialized = true; + } return logToConsole; } @@ -1908,4 +1925,24 @@ void QMessageLogContext::copy(const QMessageLogContext &logContext) \a lineNumber, in function \a functionName, and category \a categoryName. */ +/*! + \internal + + For MDeclarativeCache. + */ +Q_CORE_EXPORT void qt_reinit_logging() +{ +#ifndef QT_BOOTSTRAPPED + fatalCriticalsInitialized = false; + fatalWarningsInitialized = false; + logToConsoleInitialized = false; + QLoggingRegistry::instance()->init(); + const QString envPattern = QString::fromLocal8Bit(qgetenv("QT_MESSAGE_PATTERN")); + if (!envPattern.isEmpty()) { + QMutexLocker lock(&QMessagePattern::mutex); + qMessagePattern()->setPattern(envPattern); + } +#endif +} + QT_END_NAMESPACE