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
4#include "qqmlloggingcategory_p.h"
5
6#include <QtQml/qqmlinfo.h>
7
8#include <memory>
9
10/*!
11 \qmltype LoggingCategory
12 \ingroup qml-utility-elements
13 \inqmlmodule QtQml
14 \brief Defines a logging category in QML.
15 \since 5.8
16
17 A logging category can be passed to console.log() and friends as the first argument.
18 If supplied to the logger the LoggingCategory's name will be used as Logging Category
19 otherwise the default logging category will be used.
20
21 \qml
22 import QtQuick 2.8
23
24 Item {
25 LoggingCategory {
26 id: category
27 name: "com.qt.category"
28 defaultLogLevel: LoggingCategory.Warning
29 }
30
31 Component.onCompleted: {
32 console.log(category, "message");
33 }
34 }
35 \endqml
36
37 \note As the creation of objects is expensive, it is encouraged to put the needed
38 LoggingCategory definitions into a singleton and import this where needed.
39
40 \sa QLoggingCategory
41*/
42
43/*!
44 \qmlproperty string QtQml::LoggingCategory::name
45
46 Holds the name of the logging category.
47
48 \note This property needs to be set when declaring the LoggingCategory
49 and cannot be changed later.
50
51 \sa QLoggingCategory::categoryName()
52*/
53
54/*!
55 \qmlproperty enumeration QtQml::LoggingCategory::defaultLogLevel
56 \since 5.12
57
58 Holds the default log level of the logging category. By default it is
59 created with the LoggingCategory.Debug log level.
60
61 \note This property needs to be set when declaring the LoggingCategory
62 and cannot be changed later.
63*/
64
65QQmlLoggingCategory::QQmlLoggingCategory(QObject *parent)
66 : QObject(parent)
67 , m_initialized(false)
68{
69}
70
71QQmlLoggingCategory::~QQmlLoggingCategory()
72{
73}
74
75QString QQmlLoggingCategory::name() const
76{
77 return QString::fromUtf8(ba: m_name);
78}
79
80QQmlLoggingCategory::DefaultLogLevel QQmlLoggingCategory::defaultLogLevel() const
81{
82 return m_defaultLogLevel;
83}
84
85QLoggingCategory *QQmlLoggingCategory::category() const
86{
87 return m_category.get();
88}
89
90void QQmlLoggingCategory::classBegin()
91{
92}
93
94void QQmlLoggingCategory::componentComplete()
95{
96 m_initialized = true;
97 if (m_name.isNull()) {
98 qmlWarning(me: this) << QLatin1String("Declaring the name of a LoggingCategory is mandatory and cannot be changed later");
99 } else {
100 auto category = std::make_unique<QLoggingCategory>(args: m_name.constData(), args: QtMsgType(m_defaultLogLevel));
101 m_category.swap(u&: category);
102 }
103}
104
105void QQmlLoggingCategory::setDefaultLogLevel(DefaultLogLevel defaultLogLevel)
106{
107 if (m_defaultLogLevel == defaultLogLevel)
108 return;
109
110 if (m_initialized) {
111 qmlWarning(me: this) << QLatin1String("The defaultLogLevel of a LoggingCategory cannot be changed after the component is completed");
112 return;
113 }
114
115 m_defaultLogLevel = defaultLogLevel;
116}
117
118void QQmlLoggingCategory::setName(const QString &name)
119{
120 const QByteArray newName = name.toUtf8();
121
122 if (m_name == newName)
123 return;
124
125 if (m_initialized) {
126 qmlWarning(me: this) << QLatin1String("The name of a LoggingCategory cannot be changed after the component is completed");
127 return;
128 }
129
130 m_name = newName;
131}
132
133#include "moc_qqmlloggingcategory_p.cpp"
134

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