wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
7
8#pragma once
9
10#include <stdexcept>
11
12#include <QCoreApplication>
13#include <QLoggingCategory>
14
15Q_DECLARE_LOGGING_CATEGORY(zero)
16Q_DECLARE_LOGGING_CATEGORY(one)
17Q_DECLARE_LOGGING_CATEGORY(two)
18Q_DECLARE_LOGGING_CATEGORY(three)
19Q_DECLARE_LOGGING_CATEGORY(four)
20Q_DECLARE_LOGGING_CATEGORY(five)
21
22#undef qCDebug // to add noquote() and nospace()
23
24#if QT_VERSION < QT_VERSION_CHECK(6, 3, 0)
25#define qCDebug(category) \
26 for (bool qt_category_enabled = category().isDebugEnabled(); qt_category_enabled; qt_category_enabled = false) \
27 QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName()).debug().noquote().nospace()
28#else
29#define qCDebug(category) QT_MESSAGE_LOGGER_COMMON(category, QtDebugMsg).debug().noquote().nospace()
30#endif
31
37{
38public:
43 static void setVerbosity(const int verbosity);
44};
45
54class Pandaception : public std::runtime_error
55{
56public:
57 // --- Lifecycle ---
58
66 explicit Pandaception(const QString &translatedMessage, const QString &englishMessage,
67 const char *file = nullptr, int line = 0);
68
78 ~Pandaception() override;
79
80 // --- Message access ---
81
83 QString englishMessage() const;
84
86 const char *file() const;
87
89 int line() const;
90
91private:
92 QString m_englishMessage;
93 const char *m_file = nullptr;
94 int m_line = 0;
95};
96
97// Main macro for class contexts that can use tr() - uses __VA_OPT__ for optional arguments
98#define PANDACEPTION(msg, ...) \
99 Pandaception(tr(msg) __VA_OPT__(.arg(__VA_ARGS__)), QString(msg) __VA_OPT__(.arg(__VA_ARGS__)), __FILE__, __LINE__)
100
101// Special macro for non-class contexts - uses __VA_OPT__ for optional arguments
102#define PANDACEPTION_WITH_CONTEXT(context, msg, ...) \
103 Pandaception(QCoreApplication::translate(context, msg) __VA_OPT__(.arg(__VA_ARGS__)), QString(msg) __VA_OPT__(.arg(__VA_ARGS__)), __FILE__, __LINE__)
104
105// Macro for test/internal contexts where translation is not needed.
106// Uses QStringLiteral so lupdate does not extract the string.
107#define PANDACEPTION_LITERAL(msg, ...) \
108 Pandaception(QStringLiteral(msg) __VA_OPT__(.arg(__VA_ARGS__)), QStringLiteral(msg) __VA_OPT__(.arg(__VA_ARGS__)), __FILE__, __LINE__)
Controls the verbosity level of diagnostic logging categories.
Definition Common.h:37
static void setVerbosity(const int verbosity)
Sets the active logging verbosity level.
Definition Common.cpp:13
QString englishMessage() const
Returns the English (non-translated) message for logging and crash reports.
Definition Common.cpp:50
const char * file() const
Returns the source file where the exception was thrown, or nullptr.
Definition Common.cpp:55
Pandaception(const QString &translatedMessage, const QString &englishMessage, const char *file=nullptr, int line=0)
Constructs the exception with throw-site location for Sentry.
Definition Common.cpp:34
~Pandaception() override
int line() const
Returns the source line where the exception was thrown, or 0.
Definition Common.cpp:60