QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCLoggingCategory.cc
Go to the documentation of this file.
2
3#include <QtCore/QGlobalStatic>
4#include <QtCore/QSettings>
5
6QGC_LOGGING_CATEGORY(QGCLoggingCategoryRegisterLog, "Utilities.QGCLoggingCategoryManager")
7
9
14
15void QGCLoggingCategoryManager::_insertSorted(QmlObjectListModel* model, QGCLoggingCategoryItem* item)
16{
17 for (int i=0; i<model->count(); i++) {
18 auto existingItem = qobject_cast<QGCLoggingCategoryItem*>(model->get(i));
19 if (item->fullCategory < existingItem->fullCategory) {
20 model->insert(i, item);
21 return;
22 }
23 }
24 model->append(item);
25}
26
27void QGCLoggingCategoryManager::registerCategory(const QString &fullCategory)
28{
29 //qDebug() << "Registering logging full category" << fullCategory;
30
31 QString parentCategory;
32 QString childCategory(fullCategory);
33 auto currentParentModel = &_treeCategoryModel;
34
35 auto hierarchyIndex = fullCategory.indexOf(".");
36 if (hierarchyIndex != -1) {
37 parentCategory = fullCategory.left(hierarchyIndex);
38 childCategory = fullCategory.mid(hierarchyIndex + 1);
39 QString fullParentCategory = parentCategory + ".";
40 //qDebug() << " Parent category" << parentCategory << "child category" << childCategory << "full parent category" << fullParentCategory;
41
42 bool found = false;
43 for (int j=0; j<currentParentModel->count(); j++) {
44 auto item = qobject_cast<QGCLoggingCategoryItem*>(currentParentModel->get(j));
45 if (item->fullCategory == fullParentCategory && item->children) {
46 //qDebug() << " Found existing parent full category" << item->fullCategory;
47 currentParentModel = item->children;
48 found = true;
49 break;
50 }
51 }
52 if (!found) {
53 auto newParentItem = new QGCLoggingCategoryItem(parentCategory, fullParentCategory, false /* enabled */, currentParentModel);
54 newParentItem->children = new QmlObjectListModel(newParentItem);
55 _insertSorted(&_flatCategoryModel, newParentItem);
56 _insertSorted(currentParentModel, newParentItem);
57 currentParentModel = newParentItem->children;
58 //qDebug() << " New parent full category" << newParentItem->fullCategory;
59 }
60 }
61
62 auto categoryItem = new QGCLoggingCategoryItem(childCategory, fullCategory, false /* enabled */, currentParentModel);
63 _insertSorted(&_flatCategoryModel, categoryItem);
64 _insertSorted(currentParentModel, categoryItem);
65 //qDebug() << " New category full category" << categoryItem->fullCategory << "childCategory" << childCategory;
66}
67
68void QGCLoggingCategoryManager::setCategoryLoggingOn(const QString &fullCategoryName, bool enable)
69{
70 qCDebug(QGCLoggingCategoryRegisterLog) << "Set category logging" << fullCategoryName << enable;
71
72 QSettings settings;
73 settings.beginGroup(kFilterRulesSettingsGroup);
74 if (enable) {
75 settings.setValue(fullCategoryName, enable);
76 } else {
77 settings.remove(fullCategoryName);
78 }
79
81}
82
83bool QGCLoggingCategoryManager::categoryLoggingOn(const QString &fullCategoryName)
84{
85 QSettings settings;
86
87 settings.beginGroup(kFilterRulesSettingsGroup);
88 return settings.value(fullCategoryName, false).toBool();
89}
90
91void QGCLoggingCategoryManager::setFilterRulesFromSettings(const QString &commandLineLoggingOptions)
92{
93 QString filterRules;
94 QString filterRuleFormat("%1.debug=true\n");
95
96 QSettings settings;
97 settings.beginGroup(kFilterRulesSettingsGroup);
98 for (const QString &fullCategoryName : settings.childKeys()) {
99 QString parentCategory;
100 QString childCategory;
101 _splitFullCategoryName(fullCategoryName, parentCategory, childCategory);
102
103 qCDebug(QGCLoggingCategoryRegisterLog) << "Setting filter rule for saved settings" << fullCategoryName << parentCategory << childCategory << settings.value(fullCategoryName).toBool();
104
105 auto categoryItem = _findLoggingCategory(fullCategoryName);
106 if (categoryItem) {
107 categoryItem->setEnabled(settings.value(fullCategoryName).toBool());
108 if (categoryItem->enabled()) {
109 if (childCategory.isEmpty()) {
110 // Wildcard parent category
111 filterRules += filterRuleFormat.arg(fullCategoryName + "*");
112 } else {
113 filterRules += filterRuleFormat.arg(fullCategoryName);
114 }
115 }
116 } else {
117 qCWarning(QGCLoggingCategoryRegisterLog) << "Category not found for saved settings" << fullCategoryName;
118 }
119 }
120
121 // Command line rules take precedence, so they go last in the list
122 if (!commandLineLoggingOptions.isEmpty()) {
123 const QStringList categoryList = commandLineLoggingOptions.split(",");
124
125 if (categoryList[0] == QStringLiteral("full")) {
126 filterRules += filterRuleFormat.arg("*");
127 } else {
128 for (const QString &category: categoryList) {
129 filterRules += filterRuleFormat.arg(category);
130 }
131 }
132 }
133
134 filterRules += QStringLiteral("qt.qml.connections=false");
135
136 qCDebug(QGCLoggingCategoryRegisterLog) << "Filter rules" << filterRules;
137 QLoggingCategory::setFilterRules(filterRules);
138}
139
140void QGCLoggingCategoryManager::_splitFullCategoryName(const QString &fullCategoryName, QString &parentCategory, QString &childCategory)
141{
142 const int hierarchyIndex = fullCategoryName.indexOf(".");
143 if (hierarchyIndex == -1) {
144 parentCategory = QString();
145 childCategory = fullCategoryName;
146 } else {
147 parentCategory = fullCategoryName.left(hierarchyIndex);
148 childCategory = fullCategoryName.mid(hierarchyIndex + 1);
149 }
150}
151
153{
154 QSettings settings;
155 settings.beginGroup(kFilterRulesSettingsGroup);
156 settings.remove("");
157
158 for (int i=0; i<_flatCategoryModel.count(); i++) {
159 auto item = qobject_cast<QGCLoggingCategoryItem*>(_flatCategoryModel.get(i));
160 item->setEnabled(false);
161 }
162
164}
165
166QGCLoggingCategoryItem *QGCLoggingCategoryManager::_findLoggingCategory(const QString &fullCategoryName)
167{
168 for (int i=0; i<_flatCategoryModel.count(); i++) {
169 auto item = qobject_cast<QGCLoggingCategoryItem*>(_flatCategoryModel.get(i));
170 if (item->fullCategory == fullCategoryName) {
171 return item;
172 }
173 }
174
175 return nullptr;
176}
177
178QGCLoggingCategoryItem::QGCLoggingCategoryItem(const QString& shortCategory_, const QString& fullCategory_, bool enabled_, QObject* parent)
179 : QObject(parent)
180 , shortCategory(shortCategory_)
181 , fullCategory(fullCategory_)
182 , _enabled(enabled_)
183{
184 connect(this, &QGCLoggingCategoryItem::enabledChanged, this, [this]() {
185 QGCLoggingCategoryManager::instance()->setCategoryLoggingOn(this->fullCategory, this->_enabled);
186 });
187}
188
189void QGCLoggingCategoryItem::setEnabled(bool enabled)
190{
191 if (enabled != _enabled) {
192 _enabled = enabled;
193 emit enabledChanged();
194 }
195}
Q_GLOBAL_STATIC(FirmwarePluginFactoryRegister, _firmwarePluginFactoryRegisterInstance)
_QGCLoggingCategoryManagerInstance
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void registerCategory(const QString &category)
Registers the specified logging category to the system.
static bool categoryLoggingOn(const QString &fullCategroryName)
Returns true if logging is turned on for the specified category.
static QGCLoggingCategoryManager * instance()
void setCategoryLoggingOn(const QString &fullCategoryName, bool enable)
Turns on/off logging for the specified category. State is saved in app settings.
void setFilterRulesFromSettings(const QString &commandLineLoggingOptions)
void append(QObject *object)
Caller maintains responsibility for object ownership and deletion.
QObject * get(int index)
int count() const override final
void insert(int index, QObject *object)
int indexOf(const QObject *object)