1/****************************************************************************
2**
3** Copyright (C) 2016 Pelagicore AG
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qqmlloggingcategory_p.h"
41
42#include <QtQml/qqmlinfo.h>
43
44/*!
45 \qmltype LoggingCategory
46 \ingroup qml-utility-elements
47 \inqmlmodule QtQml
48 \brief Defines a logging category in QML.
49 \since 5.8
50
51 A logging category can be passed to console.log() and friends as the first argument.
52 If supplied to to the logger the LoggingCategory's name will be used as Logging Category
53 otherwise the default logging category will be used.
54
55 \qml
56 import QtQuick 2.8
57
58 Item {
59 LoggingCategory {
60 id: category
61 name: "com.qt.category"
62 defaultLogLevel: LoggingCategory.Warning
63 }
64
65 Component.onCompleted: {
66 console.log(category, "message");
67 }
68 }
69 \endqml
70
71 \note As the creation of objects is expensive, it is encouraged to put the needed
72 LoggingCategory definitions into a singleton and import this where needed.
73
74 \sa QLoggingCategory
75*/
76
77/*!
78 \qmlproperty string QtQml::LoggingCategory::name
79
80 Holds the name of the logging category.
81
82 \note This property needs to be set when declaring the LoggingCategory
83 and cannot be changed later.
84
85 \sa QLoggingCategory::categoryName()
86*/
87
88/*!
89 \qmlproperty enumeration QtQml::LoggingCategory::defaultLogLevel
90 \since 5.12
91
92 Holds the default log level of the logging category. By default it is
93 created with the LoggingCategory.Debug log level.
94
95 \note This property needs to be set when declaring the LoggingCategory
96 and cannot be changed later.
97*/
98
99QQmlLoggingCategory::QQmlLoggingCategory(QObject *parent)
100 : QObject(parent)
101 , m_initialized(false)
102{
103}
104
105QQmlLoggingCategory::~QQmlLoggingCategory()
106{
107}
108
109QString QQmlLoggingCategory::name() const
110{
111 return QString::fromUtf8(str: m_name);
112}
113
114QQmlLoggingCategory::DefaultLogLevel QQmlLoggingCategory::defaultLogLevel() const
115{
116 return m_defaultLogLevel;
117}
118
119QLoggingCategory *QQmlLoggingCategory::category() const
120{
121 return m_category.data();
122}
123
124void QQmlLoggingCategory::classBegin()
125{
126}
127
128void QQmlLoggingCategory::componentComplete()
129{
130 m_initialized = true;
131 if (m_name.isNull()) {
132 qmlWarning(me: this) << QLatin1String("Declaring the name of a LoggingCategory is mandatory and cannot be changed later");
133 } else {
134 QScopedPointer<QLoggingCategory> category(new QLoggingCategory(m_name.constData(), QtMsgType(m_defaultLogLevel)));
135 m_category.swap(other&: category);
136 }
137}
138
139void QQmlLoggingCategory::setDefaultLogLevel(DefaultLogLevel defaultLogLevel)
140{
141 if (m_defaultLogLevel == defaultLogLevel)
142 return;
143
144 if (m_initialized) {
145 qmlWarning(me: this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed");
146 return;
147 }
148
149 m_defaultLogLevel = defaultLogLevel;
150}
151
152void QQmlLoggingCategory::setName(const QString &name)
153{
154 const QByteArray newName = name.toUtf8();
155
156 if (m_name == newName)
157 return;
158
159 if (m_initialized) {
160 qmlWarning(me: this) << QLatin1String("The name of a LoggingCategory cannot be changed after the component is completed");
161 return;
162 }
163
164 m_name = newName;
165}
166
167#include "moc_qqmlloggingcategory_p.cpp"
168

source code of qtdeclarative/src/qml/qml/qqmlloggingcategory.cpp