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 
8 #if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
9 #define fileIdFs "Assert: 0x%08" PRIx32 ":%" PRI_FwSizeType ""
10 #else
11 #define fileIdFs "Assert: \"%s:%" PRI_FwSizeType "\""
12 #endif
13 
14 namespace Fw {
15 
16 void defaultPrintAssert(const CHAR* msg) {
17  // Write to stderr w/o formatting
18  (void)fputs(msg, stderr);
19  (void)fputs("\n", stderr);
20 }
21 
23  FwSizeType lineNo,
24  FwSizeType numArgs,
25  FwAssertArgType arg1,
26  FwAssertArgType arg2,
27  FwAssertArgType arg3,
28  FwAssertArgType arg4,
29  FwAssertArgType arg5,
30  FwAssertArgType arg6,
31  CHAR* destBuffer,
32  FwSizeType buffSize) {
33  switch (numArgs) {
34  case 0:
35  (void)stringFormat(destBuffer, buffSize, fileIdFs, file, lineNo);
36  break;
37  case 1:
38  (void)stringFormat(destBuffer, buffSize, fileIdFs " %" PRI_FwAssertArgType, file, lineNo, arg1);
39  break;
40  case 2:
41  (void)stringFormat(destBuffer, buffSize, fileIdFs " %" PRI_FwAssertArgType " %" PRI_FwAssertArgType, file,
42  lineNo, arg1, arg2);
43  break;
44  case 3:
45  (void)stringFormat(destBuffer, buffSize,
47  file, lineNo, arg1, arg2, arg3);
48  break;
49  case 4:
50  (void)stringFormat(destBuffer, buffSize,
53  file, lineNo, arg1, arg2, arg3, arg4);
54  break;
55  case 5:
56  (void)stringFormat(destBuffer, buffSize,
59  file, lineNo, arg1, arg2, arg3, arg4, arg5);
60  break;
61  case 6:
62  (void)stringFormat(destBuffer, buffSize,
65  file, lineNo, arg1, arg2, arg3, arg4, arg5, arg6);
66  break;
67  default: // in an assert already, what can we do?
68  break;
69  }
70 }
71 
72 void AssertHook::printAssert(const CHAR* msg) {
73  defaultPrintAssert(msg);
74 }
75 
77  FwSizeType lineNo,
78  FwSizeType numArgs,
79  FwAssertArgType arg1,
80  FwAssertArgType arg2,
81  FwAssertArgType arg3,
82  FwAssertArgType arg4,
83  FwAssertArgType arg5,
84  FwAssertArgType arg6) {
85  CHAR destBuffer[FW_ASSERT_TEXT_SIZE] = {0};
86  defaultReportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, destBuffer,
87  static_cast<FwSizeType>(sizeof(destBuffer)));
88  // print message
89  this->printAssert(destBuffer);
90 }
91 
93  assert(0);
94 }
95 
96 AssertHook* AssertHook::s_assertHook = nullptr;
97 
99  this->previousHook = s_assertHook;
100  s_assertHook = this;
101 }
102 
104  s_assertHook = this->previousHook;
105 }
106 
108  return s_assertHook;
109 }
110 
111 // Default handler of SwAssert functions
113  FwSizeType lineNo,
114  FwSizeType numArgs,
115  FwAssertArgType arg1,
116  FwAssertArgType arg2,
117  FwAssertArgType arg3,
118  FwAssertArgType arg4,
119  FwAssertArgType arg5,
120  FwAssertArgType arg6) {
121  Fw::AssertHook* const registeredHook = Fw::AssertHook::getRegisteredHook();
122  if (nullptr == registeredHook) {
123  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
124  defaultReportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, assertMsg,
125  static_cast<FwSizeType>(sizeof(assertMsg)));
126  defaultPrintAssert(assertMsg);
127  assert(0);
128  } else {
129  registeredHook->reportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6);
130  registeredHook->doAssert();
131  }
132  return 0;
133 }
134 
136  return defaultSwAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
137 }
138 
140  return defaultSwAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
141 }
142 
144  return defaultSwAssert(file, lineNo, 2, arg1, arg2, 0, 0, 0, 0);
145 }
146 
148  return defaultSwAssert(file, lineNo, 3, arg1, arg2, arg3, 0, 0, 0);
149 }
150 
152  FwAssertArgType arg1,
153  FwAssertArgType arg2,
154  FwAssertArgType arg3,
155  FwAssertArgType arg4,
156  FwSizeType lineNo) {
157  return defaultSwAssert(file, lineNo, 4, arg1, arg2, arg3, arg4, 0, 0);
158 }
159 
161  FwAssertArgType arg1,
162  FwAssertArgType arg2,
163  FwAssertArgType arg3,
164  FwAssertArgType arg4,
165  FwAssertArgType arg5,
166  FwSizeType lineNo) {
167  return defaultSwAssert(file, lineNo, 5, arg1, arg2, arg3, arg4, arg5, 0);
168 }
169 
171  FwAssertArgType arg1,
172  FwAssertArgType arg2,
173  FwAssertArgType arg3,
174  FwAssertArgType arg4,
175  FwAssertArgType arg5,
176  FwAssertArgType arg6,
177  FwSizeType lineNo) {
178  return defaultSwAssert(file, lineNo, 6, arg1, arg2, arg3, arg4, arg5, arg6);
179 }
180 } // namespace Fw
181 
182 // define C asserts with C linkage
183 extern "C" {
184 I8 CAssert0(FILE_NAME_ARG file, FwSizeType lineNo);
186 }
187 
189  Fw::AssertHook* const registeredHook = Fw::AssertHook::getRegisteredHook();
190  if (nullptr == registeredHook) {
191  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
192  Fw::defaultReportAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0, assertMsg,
193  static_cast<FwSizeType>(sizeof(assertMsg)));
194  } else {
195  registeredHook->reportAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
196  registeredHook->doAssert();
197  }
198  return 0;
199 }
200 
202  Fw::AssertHook* const registeredHook = Fw::AssertHook::getRegisteredHook();
203  if (nullptr == registeredHook) {
204  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
205  Fw::defaultReportAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0, assertMsg,
206  static_cast<FwSizeType>(sizeof(assertMsg)));
207  } else {
208  registeredHook->reportAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
209  registeredHook->doAssert();
210  }
211  return 0;
212 }
I8 CAssert1(FILE_NAME_ARG file, FwAssertArgType arg1, FwSizeType lineNo)
Definition: Assert.cpp:201
PlatformSizeType FwSizeType
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:22
#define PRI_FwAssertArgType
I8 SwAssert(FILE_NAME_ARG file, FwSizeType lineNo)
Assert with no arguments.
Definition: Assert.cpp:135
#define FILE_NAME_ARG
Definition: Assert.hpp:16
static AssertHook * getRegisteredHook()
Definition: Assert.cpp:107
#define fileIdFs
Definition: Assert.cpp:9
void defaultPrintAssert(const CHAR *msg)
Definition: Assert.cpp:16
void deregisterHook()
Definition: Assert.cpp:103
virtual void doAssert()
Definition: Assert.cpp:92
I8 defaultSwAssert(FILE_NAME_ARG file, FwSizeType lineNo, FwSizeType numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6)
Definition: Assert.cpp:112
#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:76
void registerHook()
Definition: Assert.cpp:98
virtual void printAssert(const CHAR *msg)
Definition: Assert.cpp:72
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:188
Implementation of malloc based allocator.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.