F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Assert.cpp
Go to the documentation of this file.
2 #include <Fw/Types/Assert.hpp>
4 #include <Fw/Types/format.hpp>
5 #include <cassert>
6 #include <cstdio>
7 #include <cstdlib>
8 
9 #if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
10 #define fileIdFs "Assert: 0x%08" PRIx32 ":%" PRI_FwSizeType ""
11 #else
12 #define fileIdFs "Assert: \"%s:%" PRI_FwSizeType "\""
13 #endif
14 
15 namespace Fw {
16 
17 void defaultPrintAssert(const CHAR* msg) {
18  // Write to stderr w/o formatting
19  (void)fputs(msg, stderr);
20  (void)fputs("\n", stderr);
21 }
22 
24  FwSizeType lineNo,
25  FwSizeType numArgs,
26  FwAssertArgType arg1,
27  FwAssertArgType arg2,
28  FwAssertArgType arg3,
29  FwAssertArgType arg4,
30  FwAssertArgType arg5,
31  FwAssertArgType arg6,
32  CHAR* destBuffer,
33  FwSizeType buffSize) {
34  switch (numArgs) {
35  case 0:
36  (void)stringFormat(destBuffer, buffSize, fileIdFs, file, lineNo);
37  break;
38  case 1:
39  (void)stringFormat(destBuffer, buffSize, fileIdFs " %" PRI_FwAssertArgType, file, lineNo, arg1);
40  break;
41  case 2:
42  (void)stringFormat(destBuffer, buffSize, fileIdFs " %" PRI_FwAssertArgType " %" PRI_FwAssertArgType, file,
43  lineNo, arg1, arg2);
44  break;
45  case 3:
46  (void)stringFormat(destBuffer, buffSize,
48  file, lineNo, arg1, arg2, arg3);
49  break;
50  case 4:
51  (void)stringFormat(destBuffer, buffSize,
54  file, lineNo, arg1, arg2, arg3, arg4);
55  break;
56  case 5:
57  (void)stringFormat(destBuffer, buffSize,
60  file, lineNo, arg1, arg2, arg3, arg4, arg5);
61  break;
62  case 6:
63  (void)stringFormat(destBuffer, buffSize,
66  file, lineNo, arg1, arg2, arg3, arg4, arg5, arg6);
67  break;
68  default: // in an assert already, what can we do?
69  break;
70  }
71 }
72 
73 void AssertHook::printAssert(const CHAR* msg) {
74  defaultPrintAssert(msg);
75 }
76 
78  FwSizeType lineNo,
79  FwSizeType numArgs,
80  FwAssertArgType arg1,
81  FwAssertArgType arg2,
82  FwAssertArgType arg3,
83  FwAssertArgType arg4,
84  FwAssertArgType arg5,
85  FwAssertArgType arg6) {
86  CHAR destBuffer[FW_ASSERT_TEXT_SIZE] = {0};
87  defaultReportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, destBuffer,
88  static_cast<FwSizeType>(sizeof(destBuffer)));
89  // print message
90  this->printAssert(destBuffer);
91 }
92 
94  assert(0);
95 }
96 
97 AssertHook* AssertHook::s_assertHook = nullptr;
98 
100  this->previousHook = s_assertHook;
101  s_assertHook = this;
102 }
103 
105  s_assertHook = this->previousHook;
106 }
107 
109  return s_assertHook;
110 }
111 
112 // Default handler of SwAssert functions
114  FwSizeType lineNo,
115  FwSizeType numArgs,
116  FwAssertArgType arg1,
117  FwAssertArgType arg2,
118  FwAssertArgType arg3,
119  FwAssertArgType arg4,
120  FwAssertArgType arg5,
121  FwAssertArgType arg6) {
122  Fw::AssertHook* const registeredHook = Fw::AssertHook::getRegisteredHook();
123  if (nullptr == registeredHook) {
124  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
125  defaultReportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, assertMsg,
126  static_cast<FwSizeType>(sizeof(assertMsg)));
127  defaultPrintAssert(assertMsg);
128  assert(0);
129  } else {
130  registeredHook->reportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6);
131  registeredHook->doAssert();
132  }
133 #if FW_ASSERTIONS_ALWAYS_ABORT
134  abort();
135 #endif
136 }
137 
138 void SwAssert(FILE_NAME_ARG file, FwSizeType lineNo) {
139  defaultSwAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
140 }
141 
143  defaultSwAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
144 }
145 
147  defaultSwAssert(file, lineNo, 2, arg1, arg2, 0, 0, 0, 0);
148 }
149 
151  defaultSwAssert(file, lineNo, 3, arg1, arg2, arg3, 0, 0, 0);
152 }
153 
155  FwAssertArgType arg1,
156  FwAssertArgType arg2,
157  FwAssertArgType arg3,
158  FwAssertArgType arg4,
159  FwSizeType lineNo) {
160  defaultSwAssert(file, lineNo, 4, arg1, arg2, arg3, arg4, 0, 0);
161 }
162 
164  FwAssertArgType arg1,
165  FwAssertArgType arg2,
166  FwAssertArgType arg3,
167  FwAssertArgType arg4,
168  FwAssertArgType arg5,
169  FwSizeType lineNo) {
170  defaultSwAssert(file, lineNo, 5, arg1, arg2, arg3, arg4, arg5, 0);
171 }
172 
174  FwAssertArgType arg1,
175  FwAssertArgType arg2,
176  FwAssertArgType arg3,
177  FwAssertArgType arg4,
178  FwAssertArgType arg5,
179  FwAssertArgType arg6,
180  FwSizeType lineNo) {
181  defaultSwAssert(file, lineNo, 6, arg1, arg2, arg3, arg4, arg5, arg6);
182 }
183 } // namespace Fw
184 
185 // define C asserts with C linkage
186 extern "C" {
187 I8 CAssert0(FILE_NAME_ARG file, FwSizeType lineNo);
189 }
190 
192  Fw::AssertHook* const registeredHook = Fw::AssertHook::getRegisteredHook();
193  if (nullptr == registeredHook) {
194  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
195  Fw::defaultReportAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0, assertMsg,
196  static_cast<FwSizeType>(sizeof(assertMsg)));
197  } else {
198  registeredHook->reportAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
199  registeredHook->doAssert();
200  }
201  return 0;
202 }
203 
205  Fw::AssertHook* const registeredHook = Fw::AssertHook::getRegisteredHook();
206  if (nullptr == registeredHook) {
207  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
208  Fw::defaultReportAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0, assertMsg,
209  static_cast<FwSizeType>(sizeof(assertMsg)));
210  } else {
211  registeredHook->reportAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
212  registeredHook->doAssert();
213  }
214  return 0;
215 }
I8 CAssert1(FILE_NAME_ARG file, FwAssertArgType arg1, FwSizeType lineNo)
Definition: Assert.cpp:204
PlatformSizeType FwSizeType
FW_ASSERT_NORETURN void defaultSwAssert(FILE_NAME_ARG file, FwSizeType lineNo, FwSizeType numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6)
Definition: Assert.cpp:113
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:50
char CHAR
Definition: BasicTypes.h:59
void defaultReportAssert(FILE_NAME_ARG file, FwSizeType lineNo, FwSizeType numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6, CHAR *destBuffer, FwSizeType buffSize)
Definition: Assert.cpp:23
#define PRI_FwAssertArgType
#define FILE_NAME_ARG
Definition: Assert.hpp:16
static AssertHook * getRegisteredHook()
Definition: Assert.cpp:108
void SwAssert(FILE_NAME_ARG file, FwSizeType lineNo)
Assert with no arguments.
Definition: Assert.cpp:138
#define fileIdFs
Definition: Assert.cpp:10
void defaultPrintAssert(const CHAR *msg)
Definition: Assert.cpp:17
void deregisterHook()
Definition: Assert.cpp:104
virtual void doAssert()
Definition: Assert.cpp:93
#define FW_ASSERT_TEXT_SIZE
Size of string used to store assert description // NO_CODESONAR LANG.PREPROC.MACROSTART/END.
virtual void reportAssert(FILE_NAME_ARG file, FwSizeType lineNo, FwSizeType numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6)
destructor
Definition: Assert.cpp:77
void registerHook()
Definition: Assert.cpp:99
virtual void printAssert(const CHAR *msg)
Definition: Assert.cpp:73
FormatStatus stringFormat(char *destination, const FwSizeType maximumSize, const char *formatString,...)
format a c-string
I8 CAssert0(FILE_NAME_ARG file, FwSizeType lineNo)
Definition: Assert.cpp:191
Implementation of malloc based allocator.
#define FW_ASSERT_NORETURN
Definition: Assert.hpp:65
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.