[vlc-commits] [Git][videolan/vlc][master] 6 commits: qt: add qt message handler to log application messages as VLC logs

Steve Lhomme (@robUx4) gitlab at videolan.org
Tue Nov 26 10:31:05 UTC 2024



Steve Lhomme pushed to branch master at VideoLAN / VLC


Commits:
6e632f8c by Pierre Lamot at 2024-11-26T09:18:44+00:00
qt: add qt message handler to log application messages as VLC logs

This allows to get application message written through console.xxx in qml or
qDebug logged as vlc messages. Qt internal messages are still sent through the
default Qt log handler

- - - - -
9ddb8e6c by Pierre Lamot at 2024-11-26T09:18:44+00:00
qt: register VlcQtMessageHandler in the application

- - - - -
6c616647 by Pierre Lamot at 2024-11-26T09:18:44+00:00
qt: move qt message filtering policies inside setupVlcQtMessageHandler

- - - - -
d8d7311e by Pierre Lamot at 2024-11-26T09:18:44+00:00
qt: log qt message through VLC when --qt-verbose is set

- - - - -
4eb1d3e6 by Pierre Lamot at 2024-11-26T09:18:44+00:00
qml: update log verbosity of Controlbar warning message

- - - - -
76c5196c by Pierre Lamot at 2024-11-26T09:18:44+00:00
qt: remove QML warning ad-hoc handler

QML warning are logged through the QMessageLogger using the "default" category,
hence they will be catched by default by our handler

- - - - -


8 changed files:

- modules/gui/qt/Makefile.am
- modules/gui/qt/maininterface/mainui.cpp
- modules/gui/qt/maininterface/mainui.hpp
- modules/gui/qt/meson.build
- modules/gui/qt/player/qml/PlayerControlbarControls.qml
- modules/gui/qt/qt.cpp
- + modules/gui/qt/util/vlcqtmessagehandler.cpp
- + modules/gui/qt/util/vlcqtmessagehandler.hpp


Changes:

=====================================
modules/gui/qt/Makefile.am
=====================================
@@ -332,6 +332,8 @@ libqt_plugin_la_SOURCES = \
 	util/list_selection_model.cpp \
 	util/list_selection_model.hpp \
 	util/model_recovery_agent.hpp \
+	util/vlcqtmessagehandler.cpp \
+	util/vlcqtmessagehandler.hpp \
 	widgets/native/animators.cpp \
 	widgets/native/animators.hpp \
 	widgets/native/customwidgets.cpp widgets/native/customwidgets.hpp \


=====================================
modules/gui/qt/maininterface/mainui.cpp
=====================================
@@ -99,9 +99,6 @@ MainUI::~MainUI()
 
 bool MainUI::setup(QQmlEngine* engine)
 {
-    engine->setOutputWarningsToStandardError(false);
-    connect(engine, &QQmlEngine::warnings, this, &MainUI::onQmlWarning);
-
     if (m_mainCtx->hasMediaLibrary())
     {
         engine->addImageProvider(MLCustomCover::providerId, new MLCustomCover(m_mainCtx->getMediaLibrary()));
@@ -406,32 +403,3 @@ void MainUI::registerQMLTypes()
     // Do not protect, types can still be registered.
 #endif
 }
-
-void MainUI::onQmlWarning(const QList<QQmlError>& qmlErrors)
-{
-    for( const auto& error: qmlErrors )
-    {
-        vlc_log_type type;
-
-        switch( error.messageType() )
-        {
-        case QtInfoMsg:
-            type = VLC_MSG_INFO; break;
-        case QtWarningMsg:
-            type = VLC_MSG_WARN; break;
-        case QtCriticalMsg:
-        case QtFatalMsg:
-            type = VLC_MSG_ERR; break;
-        case QtDebugMsg:
-        default:
-            type = VLC_MSG_DBG;
-        }
-
-        msg_Generic( m_intf,
-                     type,
-                     "qml message %s:%i %s",
-                     qtu(error.url().toString()),
-                     error.line(),
-                     qtu(error.description()) );
-    }
-}


=====================================
modules/gui/qt/maininterface/mainui.hpp
=====================================
@@ -28,9 +28,6 @@ public:
     VLC_USED
     QQuickItem* createRootItem();
 
-private slots:
-    void onQmlWarning(const QList<QQmlError>& errors);
-
 private:
     void registerQMLTypes();
     static QObject* getMainCtxInstance(QQmlEngine *, QJSEngine *);


=====================================
modules/gui/qt/meson.build
=====================================
@@ -481,6 +481,8 @@ some_sources = files(
     'util/list_selection_model.cpp',
     'util/list_selection_model.hpp',
     'util/model_recovery_agent.hpp',
+    'util/vlcqtmessagehandler.cpp',
+    'util/vlcqtmessagehandler.hpp',
     'widgets/native/animators.cpp',
     'widgets/native/animators.hpp',
     'widgets/native/customwidgets.cpp',


=====================================
modules/gui/qt/player/qml/PlayerControlbarControls.qml
=====================================
@@ -75,7 +75,7 @@ QtObject {
         const entry = controlList.find( function(e) { return ( e.id === id ) } )
 
         if (entry === undefined) {
-            console.log("control delegate id " + id +  " doesn't exist")
+            console.warn("control delegate id " + id +  " doesn't exist")
             return { source: controlPath + "Fallback.qml" }
         }
 


=====================================
modules/gui/qt/qt.cpp
=====================================
@@ -85,6 +85,7 @@ extern "C" char **environ;
 #include "util/vlctick.hpp"
 #include "util/shared_input_item.hpp"
 #include "util/model_recovery_agent.hpp"
+#include "util/vlcqtmessagehandler.hpp"
 #include "network/networkmediamodel.hpp"
 #include "network/devicesourceprovider.hpp"
 #include "playlist/playlist_common.hpp"
@@ -764,36 +765,7 @@ static void *Thread( void *obj )
 {
     qt_intf_t *p_intf = (qt_intf_t *)obj;
 
-    {
-        QString filterRules;
-
-        const int verbosity = var_InheritInteger(p_intf, "verbose");
-        if (verbosity < VLC_MSG_DBG)
-        {
-            filterRules += QStringLiteral("*.debug=false\n");
-            if (verbosity < VLC_MSG_WARN)
-            {
-                filterRules += QStringLiteral("*.warning=false\n");
-                if (verbosity < VLC_MSG_ERR)
-                {
-                    filterRules += QStringLiteral("*.critical=false\n");
-                    if (verbosity < VLC_MSG_INFO)
-                    {
-                        filterRules += QStringLiteral("*.info=false\n");
-                    }
-                }
-            }
-        }
-
-        if (var_InheritBool(p_intf, "qt-verbose"))
-        {
-            filterRules += QStringLiteral("*=true\n" /* Qt by default does not enable some info and error messages */
-                                          "qt.*.debug=false\n" /* Qt's own debug messages are way too much verbose */
-                                          "qt.widgets.painting=false\n" /* Not necessary */);
-        }
-
-        QLoggingCategory::setFilterRules(filterRules);
-    }
+    auto vlcQtMessageHandler = VlcQtMessageHandlerRegisterer{VLC_OBJECT(p_intf)};
 
     char vlc_name[] = "vlc"; /* for WM_CLASS */
     char *argv[3] = { nullptr };


=====================================
modules/gui/qt/util/vlcqtmessagehandler.cpp
=====================================
@@ -0,0 +1,107 @@
+/*****************************************************************************
+ * Copyright (C) 2024 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "vlcqtmessagehandler.hpp"
+
+#include <QtGlobal>
+#include <QString>
+#include <QLoggingCategory>
+
+#include <vlc_common.h>
+
+static vlc_object_t* g_intf = nullptr;
+static bool g_logQtMessages = false;
+static QtMessageHandler g_defaultMessageHandler = nullptr;
+
+static void vlcQtMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
+{
+    //application logs from QML should be logged using vlc log system
+    //"qml" and "js" are the category used by for console.xxxx
+    //"default" is for qDebug(), most of our code logs using msg_Dbg but some parts will use qDebug,
+    //usually when they don't have access to the vlc_object. Qt itself uses categorised logger so it
+    //should be safe to assume that the default logger is only used by us.
+    if (g_logQtMessages
+        || qstrcmp(context.category, "default") == 0
+        || qstrcmp(context.category, "qml") == 0
+        || qstrcmp(context.category, "js") == 0)
+    {
+        const char *file = context.file ? context.file : "";
+        const char *function = context.function ? context.function : "";
+        int vlcLogLevel = 0;
+        switch (type)
+        {
+        case QtDebugMsg:
+            vlcLogLevel = VLC_MSG_DBG;
+            break;
+        case QtWarningMsg:
+            vlcLogLevel = VLC_MSG_WARN;
+            break;
+        case QtCriticalMsg:
+        case QtFatalMsg:
+            vlcLogLevel = VLC_MSG_ERR;
+            break;
+        case QtInfoMsg:
+            vlcLogLevel = VLC_MSG_INFO;
+            break;
+        default:
+            vlcLogLevel = VLC_MSG_DBG;
+        }
+
+        vlc_object_Log(g_intf, vlcLogLevel, vlc_module_name, file, context.line, function, "(%s) %s", context.category,  msg.toUtf8().constData());
+    }
+    else
+    {
+        g_defaultMessageHandler(type, context, msg);
+    }
+};
+
+void setupVlcQtMessageHandler(vlc_object_t* p_intf)
+{
+    assert(g_intf == nullptr);
+    assert(p_intf != nullptr);
+
+    g_intf = p_intf;
+    g_logQtMessages = var_InheritBool(p_intf, "qt-verbose");
+
+    g_defaultMessageHandler = qInstallMessageHandler(vlcQtMessageHandler);
+    qSetMessagePattern("[qt] (%{category}) %{message}");
+
+    QString filterRules;
+
+    if (g_logQtMessages)
+    {
+        filterRules = QStringLiteral("*=true\n"
+                                    "qt.*.debug=false\n" /* Qt's own debug messages are way too much verbose */
+                                    "qt.widgets.painting=false\n" /* Not necessary */);
+    }
+
+    QLoggingCategory::setFilterRules(filterRules);
+}
+
+void cleanupVlcQtMessageHandler()
+{
+    assert(g_intf != nullptr);
+
+    qInstallMessageHandler(nullptr);
+    g_defaultMessageHandler = nullptr;
+    g_intf = nullptr;
+}


=====================================
modules/gui/qt/util/vlcqtmessagehandler.hpp
=====================================
@@ -0,0 +1,42 @@
+/*****************************************************************************
+ * Copyright (C) 2024 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef VLC_QT_MESSAGE_HANDLER_HPP
+#define VLC_QT_MESSAGE_HANDLER_HPP
+
+extern "C" {
+typedef struct vlc_object_t vlc_object_t ;
+}
+
+void setupVlcQtMessageHandler(vlc_object_t* p_intf);
+void cleanupVlcQtMessageHandler();
+
+//(un)register qt message handler using RAII
+class VlcQtMessageHandlerRegisterer
+{
+public:
+    inline VlcQtMessageHandlerRegisterer(vlc_object_t* p_intf) {
+        setupVlcQtMessageHandler(p_intf);
+    }
+
+    inline ~VlcQtMessageHandlerRegisterer() {
+        cleanupVlcQtMessageHandler();
+    }
+};
+
+#endif /* VLC_QT_MESSAGE_HANDLER_HPP */



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/c198c3a1020248468ee1bebc3876365c9defdb9d...76c5196c708df1e7f9f59b45d0ea8e090f8c9d0d

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/c198c3a1020248468ee1bebc3876365c9defdb9d...76c5196c708df1e7f9f59b45d0ea8e090f8c9d0d
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list