Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_assert.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2013 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "kis_assert.h"
8
9#include <QString>
10#include <QMessageBox>
11#include <QThread>
12#include <QProcessEnvironment>
13#include <QCoreApplication>
14#include <QApplication>
15
16#include <klocalizedstring.h>
17#include <KisUsageLogger.h>
18#include <string>
19#include "config-safe-asserts.h"
20
34void kis_assert_common(const char *assertion, const char *file, int line, bool abort, bool isIgnorable)
35{
36 QString shortMessage =
37 QString("%4ASSERT (krita): \"%1\" in file %2, line %3")
38 .arg(assertion)
39 .arg(file)
40 .arg(line)
41 .arg(isIgnorable ? "SAFE " : "");
42
43 QString longMessage =
44 QString(
45 "Krita has encountered an internal error:\n\n"
46 "%1\n\n"
47 "Please report a bug to developers!\n\n"
48 "Press Ignore to try to continue.\n"
49 "Press Abort to see developers information (all unsaved data will be lost)")
50 .arg(shortMessage);
51
52 KisUsageLogger::log(shortMessage);
53
54 bool disableAssertMsg =
55 QProcessEnvironment::systemEnvironment().value("KRITA_NO_ASSERT_MSG", "0").toInt();
56
57 // disable message box if the assert happened in non-gui thread
58 // or if the GUI is not yet instantiated
59 if (!QCoreApplication::instance() || QThread::currentThread() != QCoreApplication::instance()->thread()) {
60 disableAssertMsg = true;
61 }
62
63 bool shouldIgnoreAsserts = false;
64 bool forceCrashOnSafeAsserts = false;
65
66#ifdef HIDE_SAFE_ASSERTS
67 shouldIgnoreAsserts |= HIDE_SAFE_ASSERTS;
68#endif
69
70#ifdef CRASH_ON_SAFE_ASSERTS
71 forceCrashOnSafeAsserts |= CRASH_ON_SAFE_ASSERTS;
72#endif
73
74 disableAssertMsg |= shouldIgnoreAsserts || forceCrashOnSafeAsserts;
75
76 QMessageBox::StandardButton button =
77 isIgnorable && !forceCrashOnSafeAsserts ?
78 QMessageBox::Ignore : QMessageBox::Abort;
79
80 if (!disableAssertMsg) {
81 button =
82 QMessageBox::critical(qApp->activeWindow(), i18nc("@title:window", "Krita: Internal Error"),
83 longMessage,
84 QMessageBox::Ignore | QMessageBox::Abort,
85 QMessageBox::Ignore);
86 }
87
88 if (button == QMessageBox::Abort || abort) {
89 qFatal("%s", shortMessage.toLatin1().data());
90 } else if (isIgnorable) {
91 // Assert is a bug! Please don't change this line to warnKrita,
92 // the user must see it!
93 qWarning("%s", shortMessage.toLatin1().data());
94 }
95}
96
97
98void kis_assert_recoverable(const char *assertion, const char *file, int line)
99{
100 kis_assert_common(assertion, file, line, false, false);
101}
102
103void kis_safe_assert_recoverable(const char *assertion, const char *file, int line)
104{
105 kis_assert_common(assertion, file, line, false, true);
106}
107
108void kis_assert_exception(const char *assertion, const char *file, int line)
109{
110 kis_assert_common(assertion, file, line, true, false);
111}
112
113void kis_assert_x_exception(const char *assertion,
114 const char *where,
115 const char *what,
116 const char *file, int line)
117{
118 QString res =
119 QString("ASSERT failure in %1: \"%2\" (%3)")
120 .arg(where, what, assertion);
121
122 kis_assert_common(res.toLatin1().data(), file, line, true, false);
123}
static void log(const QString &message)
Logs with date/time.
void kis_assert_exception(const char *assertion, const char *file, int line)
void kis_assert_x_exception(const char *assertion, const char *where, const char *what, const char *file, int line)
void kis_safe_assert_recoverable(const char *assertion, const char *file, int line)
void kis_assert_common(const char *assertion, const char *file, int line, bool abort, bool isIgnorable)
void kis_assert_recoverable(const char *assertion, const char *file, int line)
QString button(const QWheelEvent &ev)