umbrello 25.12.3
Umbrello UML Modeller is a Unified Modelling Language (UML) diagram program based on KDE Technology
debug_utils.h
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2011 Andi Fischer <andi.fischer@hispeed.ch>
3 SPDX-FileCopyrightText: 2012 Ralf Habacker <ralf.habacker@freenet.de>
4 SPDX-FileCopyrightText: 2022 Oliver Kellogg <okellogg@users.sourceforge.net>
5
6 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
7*/
8
9#ifndef DEBUG_UTILS_H
10#define DEBUG_UTILS_H
11
12/*
13 This file shall only by #included by implementation files (.cpp),
14 not by header files (.h)
15*/
16
17#include <QtGlobal>
18
19#include <QLoggingCategory>
20Q_DECLARE_LOGGING_CATEGORY(UMBRELLO)
21#include <QMetaEnum>
22#include <QTreeWidget>
23
57class Tracer : public QTreeWidget
58{
59 Q_OBJECT
60public:
61 static Tracer* instance();
62
63 ~Tracer();
64
65 bool isEnabled(const QString& name) const;
66 void enable(const QString& name);
67 void disable(const QString& name);
68
69 void enableAll();
70 void disableAll();
71
72 bool logToConsole();
73
74 static void registerClass(const char * name, bool state=true, const char *filePath = nullptr);
75
76protected:
77 void update(const QString &name);
79 void updateParentItemCheckBox(QTreeWidgetItem *parent);
80 virtual void showEvent(QShowEvent*);
81 void restoreParentCheckState(QTreeWidgetItem *parent);
82
83private Q_SLOTS:
84 void slotParentItemClicked(QTreeWidgetItem *parent);
85 void slotItemClicked(QTreeWidgetItem* item, int column);
86
87private:
88 class MapEntry {
89 public:
90 QString filePath;
91 bool state;
92 MapEntry() : state(false) {}
93 MapEntry(const QString &_filePath, bool _state) : filePath(_filePath), state(_state) {}
94 };
95
96 typedef QMap<QString, MapEntry> MapType;
97 typedef QMap<QString,Qt::CheckState> StateMap;
98
102 static bool s_logToConsole;
103
104 explicit Tracer(QWidget *parent = nullptr);
105};
106
107// convenience macros for console output to the Umbrello area
108#define uDebug() qCDebug(UMBRELLO)
109#define uError() qCCritical(UMBRELLO)
110#define uWarning() qCWarning(UMBRELLO)
111
112#ifndef DBG_SRC
113#define DBG_SRC QString::fromLatin1(metaObject()->className())
114#endif
115#define DEBUG_SHOW_FILTER() Tracer::instance()->show()
116#define DEBUG_N(latin1str) if (Tracer::instance()->logToConsole() || Tracer::instance()->isEnabled(latin1str)) uDebug()
117#define DEBUG() DEBUG_N(DBG_SRC)
118#define IS_DEBUG_ENABLED() Tracer::instance()->isEnabled(DBG_SRC)
119#define DEBUG_REGISTER(src) \
120 class src##Tracer { \
121 public: \
122 src##Tracer() { Tracer::registerClass(#src, true, __FILE__); } \
123 }; \
124 static src##Tracer src##TracerGlobal;
125#define DEBUG_REGISTER_DISABLED(src) \
126 class src##Tracer { \
127 public: \
128 src##Tracer() { Tracer::registerClass(#src, false, __FILE__); } \
129 }; \
130 static src##Tracer src##TracerGlobal;
131
132#define uIgnoreZeroPointer(a) if (!a) { uDebug() << "zero pointer detected" << __FILE__ << __LINE__; continue; }
133
134
141#define ENUM_NAME(o, e, v) (o::staticMetaObject.enumerator(o::staticMetaObject.indexOfEnumerator(#e)).valueToKey((v)))
142
143template <typename T>
144QString toString(const T& value);
145
146template <typename T>
147QString toString(const T& value)
148{
149 QString s;
150 QDebug(&s) << value; // relies on Qt's QDebug operator<<
151 return s.trimmed();
152}
153
154#define logDebug0(s) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
155 UMLApp::app()->logDebug(QStringLiteral(s))
156#define logInfo0(s) UMLApp::app()->logInfo(QStringLiteral(s))
157#define logWarn0(s) UMLApp::app()->logWarn(QStringLiteral(s))
158#define logError0(s) UMLApp::app()->logError(QStringLiteral(s))
159
160#define logDebug1(s, a) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
161 do { QString fmt = QString(QStringLiteral(s)).arg(a); UMLApp::app()->logDebug(fmt); } while (0)
162#define logInfo1(s, a) do { QString fmt = QString(QStringLiteral(s)).arg(a); UMLApp::app()->logInfo(fmt); } while (0)
163#define logWarn1(s, a) do { QString fmt = QString(QStringLiteral(s)).arg(a); UMLApp::app()->logWarn(fmt); } while (0)
164#define logError1(s, a) do { QString fmt = QString(QStringLiteral(s)).arg(a); UMLApp::app()->logError(fmt); } while (0)
165
166#define logDebug2(s, a, b) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
167 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b); UMLApp::app()->logDebug(fmt); } while (0)
168#define logInfo2(s, a, b) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b); UMLApp::app()->logInfo(fmt); } while (0)
169#define logWarn2(s, a, b) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b); UMLApp::app()->logWarn(fmt); } while (0)
170#define logError2(s, a, b) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b); UMLApp::app()->logError(fmt); } while (0)
171
172#define logDebug3(s, a, b, c) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
173 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c); UMLApp::app()->logDebug(fmt); } while (0)
174#define logInfo3(s, a, b, c) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c); UMLApp::app()->logInfo(fmt); } while (0)
175#define logWarn3(s, a, b, c) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c); UMLApp::app()->logWarn(fmt); } while (0)
176#define logError3(s, a, b, c) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c); UMLApp::app()->logError(fmt); } while (0)
177
178#define logDebug4(s, a, b, c, d) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
179 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d); UMLApp::app()->logDebug(fmt); } while (0)
180#define logInfo4(s, a, b, c, d) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d); UMLApp::app()->logInfo(fmt); } while (0)
181#define logWarn4(s, a, b, c, d) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d); UMLApp::app()->logWarn(fmt); } while (0)
182#define logError4(s, a, b, c, d) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d); UMLApp::app()->logError(fmt); } while (0)
183
184#define logDebug5(s, a, b, c, d, e) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
185 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d).arg(e); \
186 UMLApp::app()->logDebug(fmt); } while (0)
187#define logInfo5(s, a, b, c, d, e) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d).arg(e); \
188 UMLApp::app()->logInfo(fmt); } while (0)
189#define logWarn5(s, a, b, c, d, e) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d).arg(e); \
190 UMLApp::app()->logWarn(fmt); } while (0)
191#define logError5(s, a, b, c, d, e) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d).arg(e); \
192 UMLApp::app()->logError(fmt); } while (0)
193
194#define logDebug6(s, a, b, c, d, e, f) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
195 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d).arg(e).arg(f); UMLApp::app()->logDebug(fmt); } while (0)
196
197#endif
MapEntry(const QString &_filePath, bool _state)
Definition debug_utils.h:93
QString filePath
Definition debug_utils.h:90
bool state
Definition debug_utils.h:91
MapEntry()
Definition debug_utils.h:92
void restoreParentCheckState(QTreeWidgetItem *parent)
Definition debug_utils.cpp:351
static bool s_logToConsole
Definition debug_utils.h:102
void disable(const QString &name)
Definition debug_utils.cpp:185
static Tracer * s_instance
Definition debug_utils.h:99
void disableAll()
Definition debug_utils.cpp:209
void enableAll()
Definition debug_utils.cpp:192
void slotParentItemClicked(QTreeWidgetItem *parent)
Definition debug_utils.cpp:382
QMap< QString, Qt::CheckState > StateMap
Definition debug_utils.h:97
void updateParentItemCheckBox(QTreeWidgetItem *parent)
Definition debug_utils.cpp:294
void enable(const QString &name)
Definition debug_utils.cpp:175
~Tracer()
Definition debug_utils.cpp:148
bool logToConsole()
Definition debug_utils.cpp:226
static Tracer * instance()
Definition debug_utils.cpp:109
void updateAllItemCheckBox()
Definition debug_utils.cpp:267
static StateMap * s_states
Definition debug_utils.h:101
Tracer(QWidget *parent=nullptr)
Definition debug_utils.cpp:135
void update(const QString &name)
Definition debug_utils.cpp:257
bool isEnabled(const QString &name) const
Definition debug_utils.cpp:157
static MapType * s_classes
Definition debug_utils.h:100
static void registerClass(const char *name, bool state=true, const char *filePath=nullptr)
Definition debug_utils.cpp:237
virtual void showEvent(QShowEvent *)
Definition debug_utils.cpp:312
void slotItemClicked(QTreeWidgetItem *item, int column)
Definition debug_utils.cpp:401
QMap< QString, MapEntry > MapType
Definition debug_utils.h:96
QString toString(const T &value)
Definition debug_utils.h:147