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