F´ Flight Software - C/C++ Documentation  NASA-v1.6.0
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_NO_ASSERT
9 
10 #else
11 
12 #if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
13 #define fileIdFs "Assert file ID 0x%08X: Line: %d "
14 #else
15 #define fileIdFs "Assert file \"%s\": Line: %d "
16 #endif
17 
18 namespace Fw {
19 
20  void defaultPrintAssert(const CHAR* msg) {
21  (void)fprintf(stderr,"%s\n", msg);
22  }
23 
25  (
26  FILE_NAME_ARG file,
27  NATIVE_UINT_TYPE lineNo,
28  NATIVE_UINT_TYPE numArgs,
29  AssertArg arg1,
30  AssertArg arg2,
31  AssertArg arg3,
32  AssertArg arg4,
33  AssertArg arg5,
34  AssertArg arg6,
35  CHAR* destBuffer,
36  NATIVE_INT_TYPE buffSize
37  ) {
38 
39  switch (numArgs) {
40  case 0:
41  (void)snprintf(destBuffer,buffSize,fileIdFs,file,lineNo);
42  break;
43  case 1:
44  (void)snprintf(destBuffer,buffSize,fileIdFs "%d",file,lineNo,
45  arg1);
46  break;
47  case 2:
48  (void)snprintf(destBuffer,buffSize,fileIdFs "%d %d",file,lineNo,
49  arg1,arg2);
50  break;
51  case 3:
52  (void)snprintf(destBuffer,buffSize,fileIdFs "%d %d %d",file,lineNo,
53  arg1,arg2,arg3);
54  break;
55  case 4:
56  (void)snprintf(destBuffer,buffSize,fileIdFs "%d %d %d %d",file,lineNo,
57  arg1,arg2,arg3,arg4);
58  break;
59  case 5:
60  (void)snprintf(destBuffer,buffSize,fileIdFs "%d %d %d %d %d",file,lineNo,
61  arg1,arg2,arg3,arg4,arg5);
62  break;
63  case 6:
64  (void)snprintf(destBuffer,buffSize,fileIdFs "%d %d %d %d %d %d",file,lineNo,
65  arg1,arg2,arg3,arg4,arg5,arg6);
66  break;
67  default: // in an assert already, what can we do?
68  break;
69  }
70 
71  // null terminate
72  destBuffer[buffSize-1] = 0;
73 
74  }
75 
76  void AssertHook::printAssert(const CHAR* msg) {
77  defaultPrintAssert(msg);
78  }
79 
80  void AssertHook::reportAssert
81  (
82  FILE_NAME_ARG file,
83  NATIVE_UINT_TYPE lineNo,
84  NATIVE_UINT_TYPE numArgs,
85  AssertArg arg1,
86  AssertArg arg2,
87  AssertArg arg3,
88  AssertArg arg4,
89  AssertArg arg5,
90  AssertArg arg6
91  )
92  {
93  CHAR destBuffer[FW_ASSERT_DFL_MSG_LEN];
95  (
96  file,
97  lineNo,
98  numArgs,
99  arg1,
100  arg2,
101  arg3,
102  arg4,
103  arg5,
104  arg6,
105  destBuffer,
106  sizeof(destBuffer)
107  );
108  // print message
109  this->printAssert(destBuffer);
110  }
111 
112  void AssertHook::doAssert() {
113  assert(0);
114  }
115 
116  STATIC AssertHook* s_assertHook = nullptr;
117 
118  void AssertHook::registerHook() {
119  this->previousHook = s_assertHook;
120  s_assertHook = this;
121  }
122 
123  void AssertHook::deregisterHook() {
124  s_assertHook = this->previousHook;
125  }
126 
127  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo) {
128  if (nullptr == s_assertHook) {
129  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
131  file,
132  lineNo,
133  0,
134  0,0,0,0,0,0,
135  assertMsg,sizeof(assertMsg));
136  // print message
137  defaultPrintAssert(assertMsg);
138  assert(0);
139  }
140  else {
141  s_assertHook->reportAssert(
142  file,
143  lineNo,
144  0,
145  0,0,0,0,0,0);
146  s_assertHook->doAssert();
147  }
148  return 0;
149  }
150 
151  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
152  AssertArg arg1) {
153  if (nullptr == s_assertHook) {
154  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
156  file,
157  lineNo,
158  1,
159  arg1,0,0,0,0,0,
160  assertMsg,sizeof(assertMsg));
161  // print message
162  defaultPrintAssert(assertMsg);
163  assert(0);
164  }
165  else {
166  s_assertHook->reportAssert(
167  file,
168  lineNo,
169  1,
170  arg1,0,0,0,0,0);
171  s_assertHook->doAssert();
172  }
173  return 0;
174  }
175 
176  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
177  AssertArg arg1,
178  AssertArg arg2) {
179  if (nullptr == s_assertHook) {
180  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
182  file,
183  lineNo,
184  2,
185  arg1,arg2,0,0,0,0,
186  assertMsg,sizeof(assertMsg));
187  defaultPrintAssert(assertMsg);
188  assert(0);
189  }
190  else {
191  s_assertHook->reportAssert(
192  file,
193  lineNo,
194  2,
195  arg1,arg2,0,0,0,0);
196  s_assertHook->doAssert();
197  }
198  return 0;
199  }
200 
201  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
202  AssertArg arg1,
203  AssertArg arg2,
204  AssertArg arg3) {
205  if (nullptr == s_assertHook) {
206  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
208  file,
209  lineNo,
210  3,
211  arg1,arg2,arg3,0,0,0,
212  assertMsg,sizeof(assertMsg));
213  defaultPrintAssert(assertMsg);
214  assert(0);
215  }
216  else {
217  s_assertHook->reportAssert(
218  file,
219  lineNo,
220  3,
221  arg1,arg2,arg3,0,0,0);
222  s_assertHook->doAssert();
223  }
224  return 0;
225  }
226 
227  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
228  AssertArg arg1,
229  AssertArg arg2,
230  AssertArg arg3,
231  AssertArg arg4) {
232  if (nullptr == s_assertHook) {
233  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
235  file,
236  lineNo,
237  4,
238  arg1,arg2,arg3,arg4,0,0,
239  assertMsg,sizeof(assertMsg));
240  defaultPrintAssert(assertMsg);
241  assert(0);
242  }
243  else {
244  s_assertHook->reportAssert(
245  file,
246  lineNo,
247  4,
248  arg1,arg2,arg3,arg4,0,0);
249  s_assertHook->doAssert();
250  }
251  return 0;
252  }
253 
254  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
255  AssertArg arg1,
256  AssertArg arg2,
257  AssertArg arg3,
258  AssertArg arg4,
259  AssertArg arg5) {
260  if (nullptr == s_assertHook) {
261  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
263  file,
264  lineNo,
265  5,
266  arg1,arg2,arg3,arg4,arg5,0,
267  assertMsg,sizeof(assertMsg));
268  defaultPrintAssert(assertMsg);
269  assert(0);
270  }
271  else {
272  s_assertHook->reportAssert(
273  file,
274  lineNo,
275  5,
276  arg1,arg2,arg3,arg4,arg5,0);
277  s_assertHook->doAssert();
278  }
279  return 0;
280  }
281 
282  NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
283  AssertArg arg1,
284  AssertArg arg2,
285  AssertArg arg3,
286  AssertArg arg4,
287  AssertArg arg5,
288  AssertArg arg6) {
289  if (nullptr == s_assertHook) {
290  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
292  file,
293  lineNo,
294  6,
295  arg1,arg2,arg3,arg4,arg5,arg6,
296  assertMsg,sizeof(assertMsg));
297  defaultPrintAssert(assertMsg);
298  assert(0);
299  }
300  else {
301  s_assertHook->reportAssert(
302  file,
303  lineNo,
304  6,
305  arg1,arg2,arg3,arg4,arg5,arg6);
306  s_assertHook->doAssert();
307  }
308  return 0;
309  }
310 }
311 
312 // define C asserts.
313 extern "C" {
314  NATIVE_INT_TYPE CAssert0(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo);
315 }
316 
317 NATIVE_INT_TYPE CAssert0(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo) {
318  if (nullptr == Fw::s_assertHook) {
319  CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
321  file,
322  lineNo,
323  0,
324  0,0,0,0,0,0,
325  assertMsg,sizeof(assertMsg));
326  }
327  else {
328  Fw::s_assertHook->reportAssert(
329  file,
330  lineNo,
331  0,
332  0,0,0,0,0,0);
333  Fw::s_assertHook->doAssert();
334  }
335  return 0;
336 }
337 
338 #endif // FW_NO_ASSERT
339 
FW_ASSERT_DFL_MSG_LEN
#define FW_ASSERT_DFL_MSG_LEN
Definition: Assert.cpp:6
CHAR
char CHAR
Definition: BasicTypes.hpp:99
Fw::defaultReportAssert
void defaultReportAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, NATIVE_UINT_TYPE numArgs, AssertArg arg1, AssertArg arg2, AssertArg arg3, AssertArg arg4, AssertArg arg5, AssertArg arg6, CHAR *destBuffer, NATIVE_INT_TYPE buffSize)
Assert.hpp
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:30
FpConfig.hpp
ISF configuration file.
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29
AssertArg
#define AssertArg
Definition: FpConfig.hpp:47
Fw
Definition: Buffer.cpp:21
STATIC
#define STATIC
static for non unit-test code
Definition: BasicTypes.hpp:108