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];
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 static AssertHook* s_assertHook = nullptr;
97 
99  this->previousHook = s_assertHook;
100  s_assertHook = this;
101 }
102 
104  s_assertHook = this->previousHook;
105 }
106 
107 // Default handler of SwAssert functions
109  FwSizeType lineNo,
110  FwSizeType numArgs,
111  FwAssertArgType arg1,
112  FwAssertArgType arg2,
113  FwAssertArgType arg3,
114  FwAssertArgType arg4,
115  FwAssertArgType arg5,
116  FwAssertArgType arg6) {
117  if (nullptr == s_assertHook) {
118  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
119  defaultReportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, assertMsg,
120  static_cast<FwSizeType>(sizeof(assertMsg)));
121  defaultPrintAssert(assertMsg);
122  assert(0);
123  } else {
124  s_assertHook->reportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6);
126  }
127  return 0;
128 }
129 
131  return defaultSwAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
132 }
133 
135  return defaultSwAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
136 }
137 
139  return defaultSwAssert(file, lineNo, 2, arg1, arg2, 0, 0, 0, 0);
140 }
141 
143  return defaultSwAssert(file, lineNo, 3, arg1, arg2, arg3, 0, 0, 0);
144 }
145 
147  FwAssertArgType arg1,
148  FwAssertArgType arg2,
149  FwAssertArgType arg3,
150  FwAssertArgType arg4,
151  FwSizeType lineNo) {
152  return defaultSwAssert(file, lineNo, 4, arg1, arg2, arg3, arg4, 0, 0);
153 }
154 
156  FwAssertArgType arg1,
157  FwAssertArgType arg2,
158  FwAssertArgType arg3,
159  FwAssertArgType arg4,
160  FwAssertArgType arg5,
161  FwSizeType lineNo) {
162  return defaultSwAssert(file, lineNo, 5, arg1, arg2, arg3, arg4, arg5, 0);
163 }
164 
166  FwAssertArgType arg1,
167  FwAssertArgType arg2,
168  FwAssertArgType arg3,
169  FwAssertArgType arg4,
170  FwAssertArgType arg5,
171  FwAssertArgType arg6,
172  FwSizeType lineNo) {
173  return defaultSwAssert(file, lineNo, 6, arg1, arg2, arg3, arg4, arg5, arg6);
174 }
175 } // namespace Fw
176 
177 // define C asserts with C linkage
178 extern "C" {
179 I8 CAssert0(FILE_NAME_ARG file, FwSizeType lineNo);
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,
187  static_cast<FwSizeType>(sizeof(assertMsg)));
188  } else {
189  Fw::s_assertHook->reportAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
191  }
192  return 0;
193 }
194 
196  if (nullptr == Fw::s_assertHook) {
197  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
198  Fw::defaultReportAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0, assertMsg,
199  static_cast<FwSizeType>(sizeof(assertMsg)));
200  } else {
201  Fw::s_assertHook->reportAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
203  }
204  return 0;
205 }
I8 CAssert1(FILE_NAME_ARG file, FwAssertArgType arg1, FwSizeType lineNo)
Definition: Assert.cpp:195
PlatformSizeType FwSizeType
I8 SwAssert(FILE_NAME_ARG file, FwSizeType lineNo)
Assert with no arguments.
Definition: Assert.cpp:130
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
#define FILE_NAME_ARG
Definition: Assert.hpp:16
#define fileIdFs
Definition: Assert.cpp:9
void defaultPrintAssert(const CHAR *msg)
Definition: Assert.cpp:16
static AssertHook * s_assertHook
Definition: Assert.cpp:96
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:108
#define FW_ASSERT_TEXT_SIZE
Size of string used to store assert description.
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:183
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.