F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Assert.cpp
Go to the documentation of this file.
1 #include <FpConfig.hpp>
2 #include <Fw/Types/Assert.hpp>
3 #include <cassert>
4 #include <cstdio>
5 
6 #define FW_ASSERT_DFL_MSG_LEN 256
7 
8 #if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
9 #define fileIdFs "Assert: 0x%08" PRIx32 ":%" PRI_PlatformUIntType
10 #else
11 #define fileIdFs "Assert: \"%s:%" PRI_PlatformUIntType "\""
12 #endif
13 
14 namespace Fw {
15 
16  void defaultPrintAssert(const CHAR* msg) {
17  (void)fprintf(stderr,"%s\n", msg);
18  }
19 
21  (
22  FILE_NAME_ARG file,
23  NATIVE_UINT_TYPE lineNo,
24  NATIVE_UINT_TYPE numArgs,
25  FwAssertArgType arg1,
26  FwAssertArgType arg2,
27  FwAssertArgType arg3,
28  FwAssertArgType arg4,
29  FwAssertArgType arg5,
30  FwAssertArgType arg6,
31  CHAR* destBuffer,
32  NATIVE_INT_TYPE buffSize
33  ) {
34 
35  switch (numArgs) {
36  case 0:
37  (void) snprintf(destBuffer, buffSize, fileIdFs, file, lineNo);
38  break;
39  case 1:
40  (void) snprintf(
41  destBuffer,
42  buffSize,
44  file,
45  lineNo,
46  arg1
47  );
48  break;
49  case 2:
50  (void) snprintf(
51  destBuffer,
52  buffSize,
54  file,
55  lineNo,
56  arg1, arg2
57  );
58  break;
59  case 3:
60  (void) snprintf(
61  destBuffer,
62  buffSize,
65  file,
66  lineNo,
67  arg1, arg2, arg3
68  );
69  break;
70  case 4:
71  (void) snprintf(
72  destBuffer,
73  buffSize,
76  file,
77  lineNo,
78  arg1, arg2, arg3, arg4);
79  break;
80  case 5:
81  (void) snprintf(
82  destBuffer,
83  buffSize,
87  file,
88  lineNo,
89  arg1, arg2, arg3, arg4, arg5
90  );
91  break;
92  case 6:
93  (void) snprintf(
94  destBuffer,
95  buffSize,
99  file,
100  lineNo,
101  arg1, arg2, arg3, arg4, arg5, arg6
102  );
103  break;
104  default: // in an assert already, what can we do?
105  break;
106  }
107 
108  // null terminate
109  destBuffer[buffSize-1] = 0;
110 
111  }
112 
113  void AssertHook::printAssert(const CHAR* msg) {
114  defaultPrintAssert(msg);
115  }
116 
118  (
119  FILE_NAME_ARG file,
120  NATIVE_UINT_TYPE lineNo,
121  NATIVE_UINT_TYPE numArgs,
122  FwAssertArgType arg1,
123  FwAssertArgType arg2,
124  FwAssertArgType arg3,
125  FwAssertArgType arg4,
126  FwAssertArgType arg5,
127  FwAssertArgType arg6
128  )
129  {
130  CHAR destBuffer[FW_ASSERT_DFL_MSG_LEN];
132  (
133  file,
134  lineNo,
135  numArgs,
136  arg1,
137  arg2,
138  arg3,
139  arg4,
140  arg5,
141  arg6,
142  destBuffer,
143  sizeof(destBuffer)
144  );
145  // print message
146  this->printAssert(destBuffer);
147  }
148 
150  assert(0);
151  }
152 
154 
156  this->previousHook = s_assertHook;
157  s_assertHook = this;
158  }
159 
161  s_assertHook = this->previousHook;
162  }
163 
164  // Default handler of SwAssert functions
166  FILE_NAME_ARG file,
167  NATIVE_UINT_TYPE lineNo,
168  NATIVE_UINT_TYPE numArgs,
169  FwAssertArgType arg1,
170  FwAssertArgType arg2,
171  FwAssertArgType arg3,
172  FwAssertArgType arg4,
173  FwAssertArgType arg5,
174  FwAssertArgType arg6) {
175  if (nullptr == s_assertHook) {
176  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
178  file,
179  lineNo,
180  numArgs,
181  arg1,arg2,arg3,arg4,arg5,arg6,
182  assertMsg,sizeof(assertMsg));
183  defaultPrintAssert(assertMsg);
184  assert(0);
185  }
186  else {
188  file,
189  lineNo,
190  numArgs,
191  arg1,arg2,arg3,arg4,arg5,arg6);
193  }
194  return 0;
195  }
196 
198  FILE_NAME_ARG file,
199  NATIVE_UINT_TYPE lineNo) {
200  return defaultSwAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
201  }
202 
204  FILE_NAME_ARG file,
205  FwAssertArgType arg1,
206  NATIVE_UINT_TYPE lineNo) {
207  return defaultSwAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
208  }
209 
211  FILE_NAME_ARG file,
212  FwAssertArgType arg1,
213  FwAssertArgType arg2,
214  NATIVE_UINT_TYPE lineNo) {
215  return defaultSwAssert(file, lineNo, 2, arg1, arg2, 0, 0, 0, 0);
216  }
217 
219  FILE_NAME_ARG file,
220  FwAssertArgType arg1,
221  FwAssertArgType arg2,
222  FwAssertArgType arg3,
223  NATIVE_UINT_TYPE lineNo) {
224  return defaultSwAssert(file, lineNo, 3, arg1, arg2, arg3, 0, 0, 0);
225  }
226 
228  FILE_NAME_ARG file,
229  FwAssertArgType arg1,
230  FwAssertArgType arg2,
231  FwAssertArgType arg3,
232  FwAssertArgType arg4,
233  NATIVE_UINT_TYPE lineNo) {
234  return defaultSwAssert(file, lineNo, 4, arg1, arg2, arg3, arg4, 0, 0);
235  }
236 
238  FILE_NAME_ARG file,
239  FwAssertArgType arg1,
240  FwAssertArgType arg2,
241  FwAssertArgType arg3,
242  FwAssertArgType arg4,
243  FwAssertArgType arg5,
244  NATIVE_UINT_TYPE lineNo) {
245  return defaultSwAssert(file, lineNo, 5, arg1, arg2, arg3, arg4, arg5, 0);
246  }
247 
249  FILE_NAME_ARG file,
250  FwAssertArgType arg1,
251  FwAssertArgType arg2,
252  FwAssertArgType arg3,
253  FwAssertArgType arg4,
254  FwAssertArgType arg5,
255  FwAssertArgType arg6,
256  NATIVE_UINT_TYPE lineNo) {
257  return defaultSwAssert(file, lineNo, 6, arg1, arg2, arg3, arg4, arg5, arg6);
258  }
259 }
260 
261 // define C asserts.
262 extern "C" {
264 }
265 
267  if (nullptr == Fw::s_assertHook) {
268  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
270  file,
271  lineNo,
272  0,
273  0,0,0,0,0,0,
274  assertMsg,sizeof(assertMsg));
275  }
276  else {
278  file,
279  lineNo,
280  0,
281  0,0,0,0,0,0);
283  }
284  return 0;
285 }
#define FW_ASSERT_DFL_MSG_LEN
Definition: Assert.cpp:6
#define fileIdFs
Definition: Assert.cpp:9
NATIVE_INT_TYPE CAssert0(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo)
Definition: Assert.cpp:266
#define FILE_NAME_ARG
Definition: Assert.hpp:16
#define STATIC
static for non unit-test code
Definition: BasicTypes.h:71
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
char CHAR
Definition: BasicTypes.h:28
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:52
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:21
#define PRI_FwAssertArgType
Definition: FpConfig.h:22
C++-compatible configuration header for fprime configuration.
virtual void reportAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, NATIVE_UINT_TYPE numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6)
destructor
Definition: Assert.cpp:118
void deregisterHook()
Definition: Assert.cpp:160
virtual void doAssert()
Definition: Assert.cpp:149
void registerHook()
Definition: Assert.cpp:155
virtual void printAssert(const CHAR *msg)
Definition: Assert.cpp:113
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo)
Assert with no arguments.
Definition: Assert.cpp:197
void defaultPrintAssert(const CHAR *msg)
Definition: Assert.cpp:16
NATIVE_INT_TYPE defaultSwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, NATIVE_UINT_TYPE numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6)
Definition: Assert.cpp:165
STATIC AssertHook * s_assertHook
Definition: Assert.cpp:153
void defaultReportAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, NATIVE_UINT_TYPE numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6, CHAR *destBuffer, NATIVE_INT_TYPE buffSize)
Definition: Assert.cpp:21