Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlloggingcategory.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Pelagicore AG
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6
7#include <QtQml/qqmlinfo.h>
8#include <QtQml/private/qqmldata_p.h>
9
10#include <memory>
11
12/*!
13 \qmltype LoggingCategory
14 \ingroup qml-utility-elements
15 \inqmlmodule QtQml
16 \brief Defines a logging category in QML.
17 \since 5.8
18
19 A logging category can be passed to console.log() and friends as the first argument.
20 If supplied to the logger the LoggingCategory's name will be used as logging category.
21 Otherwise the default logging category will be used.
22
23 \qml
24 import QtQuick
25
26 Item {
27 LoggingCategory {
28 id: category
29 name: "com.qt.category"
30 defaultLogLevel: LoggingCategory.Warning
31 }
32
33 Component.onCompleted: {
34 console.log(category, "log message");
35 console.warn(category, "warning message");
36 }
37 }
38 \endqml
39
40 By default this outputs only \c{com.qt.category: warning message}. The
41 \c{log message} is suppressed due to the \l{defaultLogLevel}. You can,
42 however, configure log levels for QML logging categories the same way
43 you can configure them for
44 \l{QLoggingCategory#configuring-categories}{QLoggingCategory}.
45
46 \note As the creation of objects is expensive, it is encouraged to put the needed
47 LoggingCategory definitions into a singleton and import this where needed.
48
49 \sa QLoggingCategory
50*/
51
52/*!
53 \qmlproperty string QtQml::LoggingCategory::name
54
55 Holds the name of the logging category.
56
57 \note This property needs to be set when declaring the LoggingCategory
58 and cannot be changed later.
59
60 \sa QLoggingCategory::categoryName()
61*/
62
63/*!
64 \qmlproperty enumeration QtQml::LoggingCategory::defaultLogLevel
65 \since 5.12
66
67 Holds the default log level of the logging category. By default it is
68 created with the LoggingCategory.Debug log level.
69
70 The following enumeration values are available:
71 \list
72 \li LoggingCategory.Debug
73 \li LoggingCategory.Info
74 \li LoggingCategory.Warning
75 \li LoggingCategory.Critical
76 \li LoggingCategory.Fatal
77 \endlist
78
79 They mirror the values of the \l{QtMsgType} enumeration.
80
81 \note This property needs to be set when declaring the LoggingCategory
82 and cannot be changed later.
83
84 \sa QtMsgType
85*/
86
87QQmlLoggingCategory::QQmlLoggingCategory(QObject *parent)
88 : QQmlLoggingCategoryBase(parent)
89 , m_initialized(false)
90{
91}
92
93QQmlLoggingCategory::~QQmlLoggingCategory()
94{
95}
96
97QString QQmlLoggingCategory::name() const
98{
99 return QString::fromUtf8(m_name);
100}
101
102QQmlLoggingCategory::DefaultLogLevel QQmlLoggingCategory::defaultLogLevel() const
103{
104 return m_defaultLogLevel;
105}
106
107void QQmlLoggingCategory::classBegin()
108{
109}
110
111void QQmlLoggingCategory::forceCompletion()
112{
113 if (m_initialized)
114 return;
115
116 m_initialized = true;
117 if (m_name.isNull())
118 qmlWarning(this) << QLatin1String("Declaring the name of a LoggingCategory is mandatory and cannot be changed later");
119 else
120 setCategory(m_name.constData(), QtMsgType(m_defaultLogLevel));
121}
122
123void QQmlLoggingCategory::componentComplete()
124{
125 forceCompletion();
126}
127
128void QQmlLoggingCategory::setDefaultLogLevel(DefaultLogLevel defaultLogLevel)
129{
130 if (m_defaultLogLevel == defaultLogLevel)
131 return;
132
133 if (m_initialized) {
134 qmlWarning(this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed");
135 return;
136 }
137
138 m_defaultLogLevel = defaultLogLevel;
139}
140
141void QQmlLoggingCategory::setName(const QString &name)
142{
143 const QByteArray newName = name.toUtf8();
144
145 if (m_name == newName)
146 return;
147
148 if (m_initialized) {
149 qmlWarning(this) << QLatin1String("The name of a LoggingCategory cannot be changed after the component is completed");
150 return;
151 }
152
153 m_name = newName;
154}
155
156#include "moc_qqmlloggingcategory_p.cpp"