F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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: 0x%08" PRIx32 ":%" PRI_PlatformUIntType
14#else
15#define fileIdFs "Assert: \"%s:%" PRI_PlatformUIntType "\""
16#endif
17
18namespace 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 FwAssertArgType arg1,
30 FwAssertArgType arg2,
31 FwAssertArgType arg3,
32 FwAssertArgType arg4,
33 FwAssertArgType arg5,
34 FwAssertArgType 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(
45 destBuffer,
46 buffSize,
47 fileIdFs " %" PRI_FwAssertArgType,
48 file,
49 lineNo,
50 arg1
51 );
52 break;
53 case 2:
54 (void) snprintf(
55 destBuffer,
56 buffSize,
58 file,
59 lineNo,
60 arg1, arg2
61 );
62 break;
63 case 3:
64 (void) snprintf(
65 destBuffer,
66 buffSize,
69 file,
70 lineNo,
71 arg1, arg2, arg3
72 );
73 break;
74 case 4:
75 (void) snprintf(
76 destBuffer,
77 buffSize,
80 file,
81 lineNo,
82 arg1, arg2, arg3, arg4);
83 break;
84 case 5:
85 (void) snprintf(
86 destBuffer,
87 buffSize,
91 file,
92 lineNo,
93 arg1, arg2, arg3, arg4, arg5
94 );
95 break;
96 case 6:
97 (void) snprintf(
98 destBuffer,
99 buffSize,
103 file,
104 lineNo,
105 arg1, arg2, arg3, arg4, arg5, arg6
106 );
107 break;
108 default: // in an assert already, what can we do?
109 break;
110 }
111
112 // null terminate
113 destBuffer[buffSize-1] = 0;
114
115 }
116
117 void AssertHook::printAssert(const CHAR* msg) {
118 defaultPrintAssert(msg);
119 }
120
121 void AssertHook::reportAssert
122 (
123 FILE_NAME_ARG file,
124 NATIVE_UINT_TYPE lineNo,
125 NATIVE_UINT_TYPE numArgs,
126 FwAssertArgType arg1,
127 FwAssertArgType arg2,
128 FwAssertArgType arg3,
129 FwAssertArgType arg4,
130 FwAssertArgType arg5,
131 FwAssertArgType arg6
132 )
133 {
134 CHAR destBuffer[FW_ASSERT_DFL_MSG_LEN];
136 (
137 file,
138 lineNo,
139 numArgs,
140 arg1,
141 arg2,
142 arg3,
143 arg4,
144 arg5,
145 arg6,
146 destBuffer,
147 sizeof(destBuffer)
148 );
149 // print message
150 this->printAssert(destBuffer);
151 }
152
153 void AssertHook::doAssert() {
154 assert(0);
155 }
156
157 STATIC AssertHook* s_assertHook = nullptr;
158
159 void AssertHook::registerHook() {
160 this->previousHook = s_assertHook;
161 s_assertHook = this;
162 }
163
164 void AssertHook::deregisterHook() {
165 s_assertHook = this->previousHook;
166 }
167
168 NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo) {
169 if (nullptr == s_assertHook) {
170 CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
172 file,
173 lineNo,
174 0,
175 0,0,0,0,0,0,
176 assertMsg,sizeof(assertMsg));
177 // print message
178 defaultPrintAssert(assertMsg);
179 assert(0);
180 }
181 else {
182 s_assertHook->reportAssert(
183 file,
184 lineNo,
185 0,
186 0,0,0,0,0,0);
187 s_assertHook->doAssert();
188 }
189 return 0;
190 }
191
192 NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
193 FwAssertArgType arg1) {
194 if (nullptr == s_assertHook) {
195 CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
197 file,
198 lineNo,
199 1,
200 arg1,0,0,0,0,0,
201 assertMsg,sizeof(assertMsg));
202 // print message
203 defaultPrintAssert(assertMsg);
204 assert(0);
205 }
206 else {
207 s_assertHook->reportAssert(
208 file,
209 lineNo,
210 1,
211 arg1,0,0,0,0,0);
212 s_assertHook->doAssert();
213 }
214 return 0;
215 }
216
217 NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
218 FwAssertArgType arg1,
219 FwAssertArgType arg2) {
220 if (nullptr == s_assertHook) {
221 CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
223 file,
224 lineNo,
225 2,
226 arg1,arg2,0,0,0,0,
227 assertMsg,sizeof(assertMsg));
228 defaultPrintAssert(assertMsg);
229 assert(0);
230 }
231 else {
232 s_assertHook->reportAssert(
233 file,
234 lineNo,
235 2,
236 arg1,arg2,0,0,0,0);
237 s_assertHook->doAssert();
238 }
239 return 0;
240 }
241
242 NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
243 FwAssertArgType arg1,
244 FwAssertArgType arg2,
245 FwAssertArgType arg3) {
246 if (nullptr == s_assertHook) {
247 CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
249 file,
250 lineNo,
251 3,
252 arg1,arg2,arg3,0,0,0,
253 assertMsg,sizeof(assertMsg));
254 defaultPrintAssert(assertMsg);
255 assert(0);
256 }
257 else {
258 s_assertHook->reportAssert(
259 file,
260 lineNo,
261 3,
262 arg1,arg2,arg3,0,0,0);
263 s_assertHook->doAssert();
264 }
265 return 0;
266 }
267
268 NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
269 FwAssertArgType arg1,
270 FwAssertArgType arg2,
271 FwAssertArgType arg3,
272 FwAssertArgType arg4) {
273 if (nullptr == s_assertHook) {
274 CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
276 file,
277 lineNo,
278 4,
279 arg1,arg2,arg3,arg4,0,0,
280 assertMsg,sizeof(assertMsg));
281 defaultPrintAssert(assertMsg);
282 assert(0);
283 }
284 else {
285 s_assertHook->reportAssert(
286 file,
287 lineNo,
288 4,
289 arg1,arg2,arg3,arg4,0,0);
290 s_assertHook->doAssert();
291 }
292 return 0;
293 }
294
295 NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
296 FwAssertArgType arg1,
297 FwAssertArgType arg2,
298 FwAssertArgType arg3,
299 FwAssertArgType arg4,
300 FwAssertArgType arg5) {
301 if (nullptr == s_assertHook) {
302 CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
304 file,
305 lineNo,
306 5,
307 arg1,arg2,arg3,arg4,arg5,0,
308 assertMsg,sizeof(assertMsg));
309 defaultPrintAssert(assertMsg);
310 assert(0);
311 }
312 else {
313 s_assertHook->reportAssert(
314 file,
315 lineNo,
316 5,
317 arg1,arg2,arg3,arg4,arg5,0);
318 s_assertHook->doAssert();
319 }
320 return 0;
321 }
322
323 NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
324 FwAssertArgType arg1,
325 FwAssertArgType arg2,
326 FwAssertArgType arg3,
327 FwAssertArgType arg4,
328 FwAssertArgType arg5,
329 FwAssertArgType arg6) {
330 if (nullptr == s_assertHook) {
331 CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
333 file,
334 lineNo,
335 6,
336 arg1,arg2,arg3,arg4,arg5,arg6,
337 assertMsg,sizeof(assertMsg));
338 defaultPrintAssert(assertMsg);
339 assert(0);
340 }
341 else {
342 s_assertHook->reportAssert(
343 file,
344 lineNo,
345 6,
346 arg1,arg2,arg3,arg4,arg5,arg6);
347 s_assertHook->doAssert();
348 }
349 return 0;
350 }
351}
352
353// define C asserts.
354extern "C" {
355 NATIVE_INT_TYPE CAssert0(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo);
356}
357
358NATIVE_INT_TYPE CAssert0(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo) {
359 if (nullptr == Fw::s_assertHook) {
360 CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
362 file,
363 lineNo,
364 0,
365 0,0,0,0,0,0,
366 assertMsg,sizeof(assertMsg));
367 }
368 else {
369 Fw::s_assertHook->reportAssert(
370 file,
371 lineNo,
372 0,
373 0,0,0,0,0,0);
374 Fw::s_assertHook->doAssert();
375 }
376 return 0;
377}
378
379#endif // FW_NO_ASSERT
380
#define FW_ASSERT_DFL_MSG_LEN
Definition Assert.cpp:6
#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
#define FW_ASSERT_DFL_MSG_LEN
Maximum assert message length when using the default assert handler.
Definition FpConfig.h:161
C++-compatible configuration header for fprime configuration.
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)