SPH
Assert.cpp
Go to the documentation of this file.
1 #include "common/Assert.h"
2 #include "io/Logger.h"
4 #include "system/Platform.h"
5 #include <assert.h>
6 #include <execinfo.h>
7 #include <mutex>
8 #include <signal.h>
9 
11 
12 static Array<std::string> getStackTrace() {
13  constexpr Size TRACE_SIZE = 10;
14  void* buffer[TRACE_SIZE];
15  const Size nptrs = backtrace(buffer, TRACE_SIZE);
16 
17  char** strings = backtrace_symbols(buffer, nptrs);
18  if (!strings) {
19  return { "No backtrace could be generated!" };
20  }
21 
22  Array<std::string> trace;
23  for (Size i = 0; i < nptrs; ++i) {
24  trace.push(strings[i]);
25  }
26  free(strings);
27  return trace;
28 }
29 
30 bool Assert::throwAssertException = false;
31 
32 bool Assert::isTest = false;
33 
35 
36 void Assert::fireParams(const char* message,
37  const char* file,
38  const char* func,
39  const int line,
40  const char* text) {
41  static std::mutex mutex;
42  std::unique_lock<std::mutex> lock(mutex);
43  if (!throwAssertException) {
44  AutoPtr<ILogger> logger;
45  if (handler) {
46  // write the message to string and provide it to the custom handler
47  logger = makeAuto<StringLogger>();
48  } else {
49  // by default, print the message to stdout
50  logger = makeAuto<StdOutLogger>();
51 
52  // also add some padding
53  logger->write(
54  "============================================================================================"
55  "==============");
56  }
57 
58  logger->write("Assert fired in file ", file, ", executing function ", func, " on line ", line);
59  logger->write("Condition: ", message);
60  if (strlen(text) != 0) {
61  logger->write("Assert parameters: ", text);
62  }
63  if (false) {
64  logger->write("Stack trace:");
65  Array<std::string> trace = getStackTrace();
66  for (std::string& s : trace) {
67  logger->write(s);
68  }
69  }
70 
71  if (handler) {
72  // execute the custom assert handler
73  const bool retval = (*handler)(dynamic_cast<StringLogger*>(&*logger)->toString());
74  if (!retval) {
75  // ignore the assert
76  return;
77  }
78  } else {
79  logger->write(
80  "============================================================================================"
81  "==============");
82  }
83 
84  if (isDebuggerPresent()) {
85  raise(SIGTRAP);
86  }
87 
88  assert(false);
89  } else {
90  throw Exception(message);
91  }
92 }
93 
94 void Assert::todo(const char* message, const char* func, const int line) {
95  StdOutLogger logger;
96  logger.write("===========================================================");
97  logger.write("Missing implementation at ", func, " on line ", line);
98  logger.write(message);
99  logger.write("===========================================================");
100  if (isDebuggerPresent()) {
101  raise(SIGTRAP);
102  }
103 }
104 
Generic dynamically allocated resizable storage.
Custom assertions.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
Logging routines of the run.
#define NAMESPACE_SPH_END
Definition: Object.h:12
bool isDebuggerPresent()
Returns true if the program is running with attached debugger.
Definition: Platform.cpp:149
System functions.
INLINE String toString(const T &value)
Definition: String.h:163
INLINE void push(U &&u)
Adds new element to the end of the array, resizing the array if necessary.
Definition: Array.h:306
void write(TArgs &&... args)
Creates and logs a message by concatenating arguments.
Definition: Logger.h:37
Standard output logger.
Definition: Logger.h:138
Logger writing messages to string stream.
Definition: Logger.h:145
static bool isTest
Definition: Assert.h:15
bool(* Handler)(const std::string &message)
Definition: Assert.h:20
static void fireParams(const char *message, const char *file, const char *func, const int line, const char *params="")
Definition: Assert.cpp:36
static bool throwAssertException
If true, assert throws an exception.
Definition: Assert.h:18
static Handler handler
Custom assert handler.
Definition: Assert.h:26
static void todo(const char *message, const char *func, const int line)
Definition: Assert.cpp:94