diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-11-15 10:18:37 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-11-23 10:27:55 +0100 |
commit | ed47bff4118f677e135f3b7c113b035ac991c5ca (patch) | |
tree | 0709485d06f22c397a45e4e1aaf79378089d49d0 /src/qml/qml/qqml.cpp | |
parent | bce216d5c086a5aa8f88d13933eeccebca316359 (diff) |
QmlCompiler: Implement console logging methods
We provide semi-private functions in the AOT context for this. Since we
cannot know the complete run time type of the potential logging category
at compile time, we have to check any first argument that might be one
separately.
Fixes: QTBUG-107175
Change-Id: I46a8922b1c5c16d2b450b8728d650d31dfd867e3
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/qml/qqml.cpp')
-rw-r--r-- | src/qml/qml/qqml.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/qml/qml/qqml.cpp b/src/qml/qml/qqml.cpp index 5e99f92bc7..db36cf6655 100644 --- a/src/qml/qml/qqml.cpp +++ b/src/qml/qml/qqml.cpp @@ -21,11 +21,14 @@ #include <private/qv4errorobject_p.h> #include <private/qqmlbuiltinfunctions_p.h> #include <private/qqmlfinalizer_p.h> +#include <private/qqmlloggingcategory_p.h> #include <QtCore/qmutex.h> QT_BEGIN_NAMESPACE +Q_DECLARE_LOGGING_CATEGORY(lcQml); +Q_DECLARE_LOGGING_CATEGORY(lcJs); /*! \internal @@ -1239,6 +1242,56 @@ QJSValue AOTCompiledContext::javaScriptGlobalProperty(uint nameIndex) const return QJSValuePrivate::fromReturnedValue(global->get(name->toPropertyKey())); } +const QLoggingCategory *AOTCompiledContext::resolveLoggingCategory(QObject *wrapper, bool *ok) const +{ + if (wrapper) { + // We have to check this here because you may pass a plain QObject that only + // turns out to be a QQmlLoggingCategory at run time. + if (QQmlLoggingCategory *qQmlLoggingCategory + = qobject_cast<QQmlLoggingCategory *>(wrapper)) { + QLoggingCategory *loggingCategory = qQmlLoggingCategory->category(); + *ok = true; + if (!loggingCategory) { + engine->handle()->throwError( + QStringLiteral("A QmlLoggingCatgory was provided without a valid name")); + } + return loggingCategory; + } + } + + *ok = false; + return qmlEngine() ? &lcQml() : &lcJs(); +} + +void AOTCompiledContext::writeToConsole( + QtMsgType type, const QString &message, const QLoggingCategory *loggingCategory) const +{ + Q_ASSERT(loggingCategory->isEnabled(type)); + + const QV4::CppStackFrame *frame = engine->handle()->currentStackFrame; + Q_ASSERT(frame); + + QMessageLogger logger(qUtf8Printable(frame->source()), frame->lineNumber(), + qUtf8Printable(frame->function()), loggingCategory->categoryName()); + + switch (type) { + case QtDebugMsg: + logger.debug("%s", qUtf8Printable(message)); + break; + case QtInfoMsg: + logger.info("%s", qUtf8Printable(message)); + break; + case QtWarningMsg: + logger.warning("%s", qUtf8Printable(message)); + break; + case QtCriticalMsg: + logger.critical("%s", qUtf8Printable(message)); + break; + default: + break; + } +} + bool AOTCompiledContext::callQmlContextPropertyLookup( uint index, void **args, const QMetaType *types, int argc) const { |