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 #if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
7 #define fileIdFs "Assert: 0x%08" PRIx32 ":%" PRI_PlatformUIntType
8 #else
9 #define fileIdFs "Assert: \"%s:%" PRI_PlatformUIntType "\""
10 #endif
11 
12 namespace Fw {
13 
14 void defaultPrintAssert(const CHAR* msg) {
15  (void)fprintf(stderr, "%s\n", msg);
16 }
17 
19  NATIVE_UINT_TYPE lineNo,
20  NATIVE_UINT_TYPE numArgs,
21  FwAssertArgType arg1,
22  FwAssertArgType arg2,
23  FwAssertArgType arg3,
24  FwAssertArgType arg4,
25  FwAssertArgType arg5,
26  FwAssertArgType arg6,
27  CHAR* destBuffer,
28  NATIVE_INT_TYPE buffSize) {
29  switch (numArgs) {
30  case 0:
31  (void)snprintf(destBuffer, static_cast<size_t>(buffSize), fileIdFs, file, lineNo);
32  break;
33  case 1:
34  (void)snprintf(destBuffer, static_cast<size_t>(buffSize), fileIdFs " %" PRI_FwAssertArgType, file, lineNo, arg1);
35  break;
36  case 2:
37  (void)snprintf(destBuffer, static_cast<size_t>(buffSize), fileIdFs " %" PRI_FwAssertArgType " %" PRI_FwAssertArgType, file,
38  lineNo, arg1, arg2);
39  break;
40  case 3:
41  (void)snprintf(destBuffer, static_cast<size_t>(buffSize),
43  lineNo, arg1, arg2, arg3);
44  break;
45  case 4:
46  (void)snprintf(destBuffer, static_cast<size_t>(buffSize),
49  file, lineNo, arg1, arg2, arg3, arg4);
50  break;
51  case 5:
52  (void)snprintf(destBuffer, static_cast<size_t>(buffSize),
55  file, lineNo, arg1, arg2, arg3, arg4, arg5);
56  break;
57  case 6:
58  (void)snprintf(destBuffer, static_cast<size_t>(buffSize),
61  file, lineNo, arg1, arg2, arg3, arg4, arg5, arg6);
62  break;
63  default: // in an assert already, what can we do?
64  break;
65  }
66 
67  // null terminate
68  destBuffer[buffSize - 1] = 0;
69 }
70 
71 void AssertHook::printAssert(const CHAR* msg) {
72  defaultPrintAssert(msg);
73 }
74 
76  NATIVE_UINT_TYPE lineNo,
77  NATIVE_UINT_TYPE numArgs,
78  FwAssertArgType arg1,
79  FwAssertArgType arg2,
80  FwAssertArgType arg3,
81  FwAssertArgType arg4,
82  FwAssertArgType arg5,
83  FwAssertArgType arg6) {
84  CHAR destBuffer[FW_ASSERT_TEXT_SIZE];
85  defaultReportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, destBuffer, sizeof(destBuffer));
86  // print message
87  this->printAssert(destBuffer);
88 }
89 
91  assert(0);
92 }
93 
95 
97  this->previousHook = s_assertHook;
98  s_assertHook = this;
99 }
100 
102  s_assertHook = this->previousHook;
103 }
104 
105 // Default handler of SwAssert functions
107  NATIVE_UINT_TYPE lineNo,
108  NATIVE_UINT_TYPE numArgs,
109  FwAssertArgType arg1,
110  FwAssertArgType arg2,
111  FwAssertArgType arg3,
112  FwAssertArgType arg4,
113  FwAssertArgType arg5,
114  FwAssertArgType arg6) {
115  if (nullptr == s_assertHook) {
116  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
117  defaultReportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, assertMsg, sizeof(assertMsg));
118  defaultPrintAssert(assertMsg);
119  assert(0);
120  } else {
121  s_assertHook->reportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6);
123  }
124  return 0;
125 }
126 
128  return defaultSwAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
129 }
130 
132  return defaultSwAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
133 }
134 
136  return defaultSwAssert(file, lineNo, 2, arg1, arg2, 0, 0, 0, 0);
137 }
138 
140  FwAssertArgType arg1,
141  FwAssertArgType arg2,
142  FwAssertArgType arg3,
143  NATIVE_UINT_TYPE lineNo) {
144  return defaultSwAssert(file, lineNo, 3, arg1, arg2, arg3, 0, 0, 0);
145 }
146 
148  FwAssertArgType arg1,
149  FwAssertArgType arg2,
150  FwAssertArgType arg3,
151  FwAssertArgType arg4,
152  NATIVE_UINT_TYPE lineNo) {
153  return defaultSwAssert(file, lineNo, 4, arg1, arg2, arg3, arg4, 0, 0);
154 }
155 
157  FwAssertArgType arg1,
158  FwAssertArgType arg2,
159  FwAssertArgType arg3,
160  FwAssertArgType arg4,
161  FwAssertArgType arg5,
162  NATIVE_UINT_TYPE lineNo) {
163  return defaultSwAssert(file, lineNo, 5, arg1, arg2, arg3, arg4, arg5, 0);
164 }
165 
167  FwAssertArgType arg1,
168  FwAssertArgType arg2,
169  FwAssertArgType arg3,
170  FwAssertArgType arg4,
171  FwAssertArgType arg5,
172  FwAssertArgType arg6,
173  NATIVE_UINT_TYPE lineNo) {
174  return defaultSwAssert(file, lineNo, 6, arg1, arg2, arg3, arg4, arg5, arg6);
175 }
176 } // namespace Fw
177 
178 // define C asserts.
179 extern "C" {
181 }
182 
184  if (nullptr == Fw::s_assertHook) {
185  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
186  Fw::defaultReportAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0, assertMsg, sizeof(assertMsg));
187  } else {
188  Fw::s_assertHook->reportAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
190  }
191  return 0;
192 }
#define fileIdFs
Definition: Assert.cpp:7
NATIVE_INT_TYPE CAssert0(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo)
Definition: Assert.cpp:183
#define FILE_NAME_ARG
Definition: Assert.hpp:16
#define STATIC
static for non unit-test code
Definition: BasicTypes.h:75
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
char CHAR
Definition: BasicTypes.h:32
PlatformUIntType NATIVE_UINT_TYPE
Definition: BasicTypes.h:56
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:39
#define PRI_FwAssertArgType
Definition: FpConfig.h:40
C++-compatible configuration header for fprime configuration.
#define FW_ASSERT_TEXT_SIZE
Size of string used to store assert description.
Definition: FpConfig.hpp:19
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:75
void deregisterHook()
Definition: Assert.cpp:101
virtual void doAssert()
Definition: Assert.cpp:90
void registerHook()
Definition: Assert.cpp:96
virtual void printAssert(const CHAR *msg)
Definition: Assert.cpp:71
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo)
Assert with no arguments.
Definition: Assert.cpp:127
void defaultPrintAssert(const CHAR *msg)
Definition: Assert.cpp:14
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:106
STATIC AssertHook * s_assertHook
Definition: Assert.cpp:94
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:18