diff options
Diffstat (limited to 'src/qml/qml/qqmlloggingcategory.cpp')
-rw-r--r-- | src/qml/qml/qqmlloggingcategory.cpp | 91 |
1 files changed, 42 insertions, 49 deletions
diff --git a/src/qml/qml/qqmlloggingcategory.cpp b/src/qml/qml/qqmlloggingcategory.cpp index b59a26e17e..8d7fd6c04d 100644 --- a/src/qml/qml/qqmlloggingcategory.cpp +++ b/src/qml/qml/qqmlloggingcategory.cpp @@ -1,46 +1,12 @@ -/**************************************************************************** -** -** Copyright (C) 2016 Pelagicore AG -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 Pelagicore AG +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qqmlloggingcategory_p.h" #include <QtQml/qqmlinfo.h> +#include <memory> + /*! \qmltype LoggingCategory \ingroup qml-utility-elements @@ -49,11 +15,11 @@ \since 5.8 A logging category can be passed to console.log() and friends as the first argument. - If supplied to to the logger the LoggingCategory's name will be used as Logging Category - otherwise the default logging category will be used. + If supplied to the logger the LoggingCategory's name will be used as logging category. + Otherwise the default logging category will be used. \qml - import QtQuick 2.8 + import QtQuick Item { LoggingCategory { @@ -63,11 +29,18 @@ } Component.onCompleted: { - console.log(category, "message"); + console.log(category, "log message"); + console.warn(category, "warning message"); } } \endqml + By default this outputs only \c{com.qt.category: warning message}. The + \c{log message} is suppressed due to the \l{defaultLogLevel}. You can, + however, configure log levels for QML logging categories the same way + you can configure them for + \l{QLoggingCategory#configuring-categories}{QLoggingCategory}. + \note As the creation of objects is expensive, it is encouraged to put the needed LoggingCategory definitions into a singleton and import this where needed. @@ -92,8 +65,21 @@ Holds the default log level of the logging category. By default it is created with the LoggingCategory.Debug log level. + The following enumeration values are available: + \list + \li LoggingCategory.Debug + \li LoggingCategory.Info + \li LoggingCategory.Warning + \li LoggingCategory.Critical + \li LoggingCategory.Fatal + \endlist + + They mirror the values of the \l{QtMsgType} enumeration. + \note This property needs to be set when declaring the LoggingCategory and cannot be changed later. + + \sa QtMsgType */ QQmlLoggingCategory::QQmlLoggingCategory(QObject *parent) @@ -118,7 +104,7 @@ QQmlLoggingCategory::DefaultLogLevel QQmlLoggingCategory::defaultLogLevel() cons QLoggingCategory *QQmlLoggingCategory::category() const { - return m_category.data(); + return m_category.get(); } void QQmlLoggingCategory::classBegin() @@ -129,32 +115,39 @@ void QQmlLoggingCategory::componentComplete() { m_initialized = true; if (m_name.isNull()) { - qmlWarning(this) << QLatin1String("Declaring the name of the LoggingCategory is mandatory and cannot be changed later !"); + qmlWarning(this) << QLatin1String("Declaring the name of a LoggingCategory is mandatory and cannot be changed later"); } else { - QScopedPointer<QLoggingCategory> category(new QLoggingCategory(m_name.constData(), QtMsgType(m_defaultLogLevel))); + auto category = std::make_unique<QLoggingCategory>(m_name.constData(), QtMsgType(m_defaultLogLevel)); m_category.swap(category); } } void QQmlLoggingCategory::setDefaultLogLevel(DefaultLogLevel defaultLogLevel) { + if (m_defaultLogLevel == defaultLogLevel) + return; + if (m_initialized) { - qmlWarning(this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the Item is created"); + qmlWarning(this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed"); return; } m_defaultLogLevel = defaultLogLevel; } - void QQmlLoggingCategory::setName(const QString &name) { + const QByteArray newName = name.toUtf8(); + + if (m_name == newName) + return; + if (m_initialized) { - qmlWarning(this) << QLatin1String("The name of a LoggingCategory cannot be changed after the Item is created"); + qmlWarning(this) << QLatin1String("The name of a LoggingCategory cannot be changed after the component is completed"); return; } - m_name = name.toUtf8(); + m_name = newName; } #include "moc_qqmlloggingcategory_p.cpp" |