umbrello 26.03.70-f7b1fd3
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);
78 void updateParentItemCheckBox(QTreeWidgetItem *parent);
79 virtual void showEvent(QShowEvent*);
80
81private Q_SLOTS:
82 void slotParentItemClicked(QTreeWidgetItem *parent);
83 void slotItemClicked(QTreeWidgetItem* item, int column);
84
85private:
86 class MapEntry {
87 public:
88 QString filePath;
89 bool state;
90 MapEntry() : state(false) {}
91 MapEntry(const QString &_filePath, bool _state) : filePath(_filePath), state(_state) {}
92 };
93
94 typedef QMap<QString, MapEntry> MapType;
95 typedef QMap<QString,Qt::CheckState> StateMap;
96
100 static bool s_logToConsole;
101
102 explicit Tracer(QWidget *parent = nullptr);
103};
104
105// convenience macros for console output to the Umbrello area
106#define uDebug() qCDebug(UMBRELLO)
107#define uError() qCCritical(UMBRELLO)
108#define uWarning() qCWarning(UMBRELLO)
109
110#ifndef DBG_SRC
111#define DBG_SRC QString::fromLatin1(metaObject()->className())
112#endif
113#define DEBUG_SHOW_FILTER() Tracer::instance()->show()
114#define DEBUG_N(latin1str) if (Tracer::instance()->logToConsole() || Tracer::instance()->isEnabled(latin1str)) uDebug()
115#define DEBUG() DEBUG_N(DBG_SRC)
116#define IS_DEBUG_ENABLED() Tracer::instance()->isEnabled(DBG_SRC)
117#define DEBUG_REGISTER(src) \
118 class src##Tracer { \
119 public: \
120 src##Tracer() { Tracer::registerClass(#src, true, __FILE__); } \
121 }; \
122 static src##Tracer src##TracerGlobal;
123#define DEBUG_REGISTER_DISABLED(src) \
124 class src##Tracer { \
125 public: \
126 src##Tracer() { Tracer::registerClass(#src, false, __FILE__); } \
127 }; \
128 static src##Tracer src##TracerGlobal;
129
130#define uIgnoreZeroPointer(a) if (!a) { uDebug() << "zero pointer detected" << __FILE__ << __LINE__; continue; }
131
132
139#define ENUM_NAME(o, e, v) (o::staticMetaObject.enumerator(o::staticMetaObject.indexOfEnumerator(#e)).valueToKey((v)))
140
141template <typename T>
142QString toString(const T& value);
143
144template <typename T>
145QString toString(const T& value)
146{
147 QString s;
148 QDebug(&s) << value; // relies on Qt's QDebug operator<<
149 return s.trimmed();
150}
151
152#define logDebug0(s) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
153 UMLApp::app()->logDebug(QStringLiteral(s))
154#define logInfo0(s) UMLApp::app()->logInfo(QStringLiteral(s))
155#define logWarn0(s) UMLApp::app()->logWarn(QStringLiteral(s))
156#define logError0(s) UMLApp::app()->logError(QStringLiteral(s))
157
158#define logDebug1(s, a) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
159 do { QString fmt = QString(QStringLiteral(s)).arg(a); UMLApp::app()->logDebug(fmt); } while (0)
160#define logInfo1(s, a) do { QString fmt = QString(QStringLiteral(s)).arg(a); UMLApp::app()->logInfo(fmt); } while (0)
161#define logWarn1(s, a) do { QString fmt = QString(QStringLiteral(s)).arg(a); UMLApp::app()->logWarn(fmt); } while (0)
162#define logError1(s, a) do { QString fmt = QString(QStringLiteral(s)).arg(a); UMLApp::app()->logError(fmt); } while (0)
163
164#define logDebug2(s, a, b) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
165 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b); UMLApp::app()->logDebug(fmt); } while (0)
166#define logInfo2(s, a, b) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b); UMLApp::app()->logInfo(fmt); } while (0)
167#define logWarn2(s, a, b) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b); UMLApp::app()->logWarn(fmt); } while (0)
168#define logError2(s, a, b) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b); UMLApp::app()->logError(fmt); } while (0)
169
170#define logDebug3(s, a, b, c) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
171 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c); UMLApp::app()->logDebug(fmt); } while (0)
172#define logInfo3(s, a, b, c) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c); UMLApp::app()->logInfo(fmt); } while (0)
173#define logWarn3(s, a, b, c) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c); UMLApp::app()->logWarn(fmt); } while (0)
174#define logError3(s, a, b, c) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c); UMLApp::app()->logError(fmt); } while (0)
175
176#define logDebug4(s, a, b, c, d) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
177 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d); UMLApp::app()->logDebug(fmt); } while (0)
178#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)
179#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)
180#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)
181
182#define logDebug5(s, a, b, c, d, e) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
183 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d).arg(e); \
184 UMLApp::app()->logDebug(fmt); } while (0)
185#define logInfo5(s, a, b, c, d, e) do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d).arg(e); \
186 UMLApp::app()->logInfo(fmt); } while (0)
187#define logWarn5(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()->logWarn(fmt); } while (0)
189#define logError5(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()->logError(fmt); } while (0)
191
192#define logDebug6(s, a, b, c, d, e, f) if (UMLApp::app()->logToConsole() || Tracer::instance()->isEnabled(DBG_SRC)) \
193 do { QString fmt = QString(QStringLiteral(s)).arg(a).arg(b).arg(c).arg(d).arg(e).arg(f); UMLApp::app()->logDebug(fmt); } while (0)
194
195#endif
Definition: debug_utils.h:86
MapEntry(const QString &_filePath, bool _state)
Definition: debug_utils.h:91
QString filePath
Definition: debug_utils.h:88
bool state
Definition: debug_utils.h:89
MapEntry()
Definition: debug_utils.h:90
The singleton class for switching on or off debug messages.
Definition: debug_utils.h:58
static StateMap * s_states
Definition: debug_utils.h:99
static bool s_logToConsole
Definition: debug_utils.h:100
void disable(const QString &name)
Definition: debug_utils.cpp:186
static Tracer * s_instance
Definition: debug_utils.h:97
void disableAll()
Definition: debug_utils.cpp:197
void enableAll()
Definition: debug_utils.cpp:192
void slotParentItemClicked(QTreeWidgetItem *parent)
Definition: debug_utils.cpp:299
QMap< QString, Qt::CheckState > StateMap
Definition: debug_utils.h:95
void updateParentItemCheckBox(QTreeWidgetItem *parent)
Definition: debug_utils.cpp:248
void enable(const QString &name)
Definition: debug_utils.cpp:176
~Tracer()
Definition: debug_utils.cpp:149
bool logToConsole()
Definition: debug_utils.cpp:202
static Tracer * instance()
Definition: debug_utils.cpp:109
void update(const QString &name)
Definition: debug_utils.cpp:233
bool isEnabled(const QString &name) const
Definition: debug_utils.cpp:158
static MapType * s_classes
Definition: debug_utils.h:98
static void registerClass(const char *name, bool state=true, const char *filePath=nullptr)
Definition: debug_utils.cpp:213
virtual void showEvent(QShowEvent *)
Definition: debug_utils.cpp:268
void slotItemClicked(QTreeWidgetItem *item, int column)
Definition: debug_utils.cpp:325
QMap< QString, MapEntry > MapType
Definition: debug_utils.h:94
QString toString(const T &value)
Definition: debug_utils.h:145