Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
BornAgain
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mlz
BornAgain
Commits
27716f28
Commit
27716f28
authored
2 years ago
by
Wuttke, Joachim
Browse files
Options
Downloads
Patches
Plain Diff
Assert under GUI: debug mode, raise SIGTERM. Add explanations
parent
3bfde60d
No related branches found
No related tags found
1 merge request
!1393
ASSERT uses function pointer to call qFatal, so that core must not depend on Qt (#494)
Pipeline
#90595
passed
2 years ago
Stage: build
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
App/main.cpp
+10
-1
10 additions, 1 deletion
App/main.cpp
Base/Util/Assert.cpp
+0
-7
0 additions, 7 deletions
Base/Util/Assert.cpp
Base/Util/Assert.h
+28
-5
28 additions, 5 deletions
Base/Util/Assert.h
with
38 additions
and
13 deletions
App/main.cpp
+
10
−
1
View file @
27716f28
...
@@ -28,6 +28,15 @@
...
@@ -28,6 +28,15 @@
#include
<QMetaType>
#include
<QMetaType>
#include
<QtGlobal>
#include
<QtGlobal>
auto
guiFailedAssertion
=
[](
std
::
string
msg
)
{
#ifdef BA_DEBUG
std
::
cerr
<<
"FATAL (catched in debug mode): "
<<
msg
<<
std
::
endl
;
std
::
raise
(
11
);
// abort so that we can inspect the backtrace
#else
qFatal
(
msg
.
c_str
());
#endif
};
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
{
ApplicationOptions
options
(
argc
,
argv
);
ApplicationOptions
options
(
argc
,
argv
);
...
@@ -46,7 +55,7 @@ int main(int argc, char* argv[])
...
@@ -46,7 +55,7 @@ int main(int argc, char* argv[])
ApplicationSettings
applicationSettings
;
ApplicationSettings
applicationSettings
;
qInstallMessageHandler
(
messageHandler
);
qInstallMessageHandler
(
messageHandler
);
failedAssertion
=
[](
std
::
string
msg
)
{
qFatal
(
msg
.
c_str
());
}
;
failedAssertion
=
guiFailedAssertion
;
register1DDataLoaders
();
register1DDataLoaders
();
...
...
This diff is collapsed.
Click to expand it.
Base/Util/Assert.cpp
+
0
−
7
View file @
27716f28
...
@@ -13,14 +13,7 @@
...
@@ -13,14 +13,7 @@
// ************************************************************************************************
// ************************************************************************************************
#include
"Base/Util/Assert.h"
#include
"Base/Util/Assert.h"
#include
<csignal>
#include
<iostream>
std
::
function
<
void
(
std
::
string
)
>
failedAssertion
=
[](
std
::
string
msg
)
{
std
::
function
<
void
(
std
::
string
)
>
failedAssertion
=
[](
std
::
string
msg
)
{
#ifdef BA_DEBUG
std
::
cerr
<<
msg
<<
std
::
endl
;
std
::
raise
(
11
);
// abort so that we can inspect the backtrace
#else
throw
std
::
runtime_error
(
msg
);
throw
std
::
runtime_error
(
msg
);
#endif
};
};
This diff is collapsed.
Click to expand it.
Base/Util/Assert.h
+
28
−
5
View file @
27716f28
...
@@ -20,23 +20,46 @@
...
@@ -20,23 +20,46 @@
#include
"Wrap/WinDllMacros.h"
#include
"Wrap/WinDllMacros.h"
#include
<functional>
#include
<functional>
#include
<sstream>
#include
<stdexcept>
#include
<string>
#include
<string>
// Function called upon failed assert; set in Assert.cpp, overriden by GUI.
// Function called upon failed assert; set in Assert.cpp, overriden by GUI.
extern
BA_BASE_API_
std
::
function
<
void
(
std
::
string
)
>
failedAssertion
;
extern
BA_BASE_API_
std
::
function
<
void
(
std
::
string
)
>
failedAssertion
;
// ASSERT must be declared as a macro, not a function, in order for the error
// ASSERT macro: terminate if condition is false.
// message to correctly report the source line where the assertion failed.
//
// Implementation notes:
// - Must be declared as a macro, not a function, so that we can use preprocessor
// macros for informative error messages.
// - Must terminate with a throw statement to prevent compiler warning -Wreturn-type.
// - In the GUI, the function pointer failedAssertion will be reset to call qFatal,
// which then pops up a message window (as defined by qInstallMessageHandler).
// - No direct call to qFatal here, because we do not want core to depend on Qt,
// lest Python wheel becomes too difficult.
#ifdef BA_DEBUG
#include
<csignal>
#include
<iostream>
#define ASSERT(condition) \
if (!(condition)) { \
std::cerr << "Assertion " << (#condition) << " failed in " << __FILE__ << ", line " \
<< __LINE__ << std::endl; \
std::raise(SIGTERM);
/* abort so that we can inspect the backtrace */
\
throw std::runtime_error("Assertion failed ... and we should never get here"); \
}
#else
#include
<sstream>
#include
<stdexcept>
#define ASSERT(condition) \
#define ASSERT(condition) \
if (!(condition)) { \
if (!(condition)) { \
std::stringstream msg; \
std::stringstream msg; \
msg << "Assertion " << (#condition) << " failed in " << __FILE__ << ", line " << __LINE__; \
msg << "Assertion " << (#condition) << " failed in " << __FILE__ << ", line " << __LINE__; \
failedAssertion(msg.str()); \
failedAssertion(msg.str()); \
/* The following throw is needed to prevent compiler warning -Wreturn-type */
\
throw std::runtime_error("Assertion failed ... and we should never get here"); \
throw std::runtime_error("Assertion failed ... and we should never get here"); \
}
}
#endif // BA_DEBUG
#endif // BORNAGAIN_BASE_UTIL_ASSERT_H
#endif // BORNAGAIN_BASE_UTIL_ASSERT_H
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment