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.
1 #include <FpConfig.hpp>
2 #include <Fw/Types/Assert.hpp>
3 #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_PlatformUIntType
10 #else
11 #define fileIdFs "Assert: \"%s:%" PRI_PlatformUIntType "\""
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,
39  fileIdFs " %" PRI_FwAssertArgType, file, lineNo, arg1);
40  break;
41  case 2:
42  (void)stringFormat(destBuffer, buffSize,
43  fileIdFs " %" PRI_FwAssertArgType " %" PRI_FwAssertArgType, file, lineNo, arg1, arg2);
44  break;
45  case 3:
46  (void)stringFormat(destBuffer, buffSize,
48  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];
87  defaultReportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, destBuffer, static_cast<FwSizeType>(sizeof(destBuffer)));
88  // print message
89  this->printAssert(destBuffer);
90 }
91 
93  assert(0);
94 }
95 
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, static_cast<FwSizeType>(sizeof(assertMsg)));
120  defaultPrintAssert(assertMsg);
121  assert(0);
122  } else {
123  s_assertHook->reportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6);
125  }
126  return 0;
127 }
128 
130  return defaultSwAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
131 }
132 
134  return defaultSwAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
135 }
136 
138  return defaultSwAssert(file, lineNo, 2, arg1, arg2, 0, 0, 0, 0);
139 }
140 
142  FwAssertArgType arg1,
143  FwAssertArgType arg2,
144  FwAssertArgType arg3,
145  FwSizeType lineNo) {
146  return defaultSwAssert(file, lineNo, 3, arg1, arg2, arg3, 0, 0, 0);
147 }
148 
150  FwAssertArgType arg1,
151  FwAssertArgType arg2,
152  FwAssertArgType arg3,
153  FwAssertArgType arg4,
154  FwSizeType lineNo) {
155  return defaultSwAssert(file, lineNo, 4, arg1, arg2, arg3, arg4, 0, 0);
156 }
157 
159  FwAssertArgType arg1,
160  FwAssertArgType arg2,
161  FwAssertArgType arg3,
162  FwAssertArgType arg4,
163  FwAssertArgType arg5,
164  FwSizeType lineNo) {
165  return defaultSwAssert(file, lineNo, 5, arg1, arg2, arg3, arg4, arg5, 0);
166 }
167 
169  FwAssertArgType arg1,
170  FwAssertArgType arg2,
171  FwAssertArgType arg3,
172  FwAssertArgType arg4,
173  FwAssertArgType arg5,
174  FwAssertArgType arg6,
175  FwSizeType lineNo) {
176  return defaultSwAssert(file, lineNo, 6, arg1, arg2, arg3, arg4, arg5, arg6);
177 }
178 } // namespace Fw
179 
180 // define C asserts with C linkage
181 extern "C" {
182 I8 CAssert0(FILE_NAME_ARG file, FwSizeType lineNo);
183 }
184 
186  if (nullptr == Fw::s_assertHook) {
187  CHAR assertMsg[FW_ASSERT_TEXT_SIZE];
188  Fw::defaultReportAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0, assertMsg, static_cast<FwSizeType>(sizeof(assertMsg)));
189  } else {
190  Fw::s_assertHook->reportAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
192  }
193  return 0;
194 }
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:39
I8 SwAssert(FILE_NAME_ARG file, FwSizeType lineNo)
Assert with no arguments.
Definition: Assert.cpp:129
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:29
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
char CHAR
Definition: BasicTypes.h:38
STATIC AssertHook * s_assertHook
Definition: Assert.cpp:96
#define FW_ASSERT_TEXT_SIZE
Size of string used to store assert description.
Definition: FpConfig.hpp:19
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
Definition: FpConfig.h:40
#define FILE_NAME_ARG
Definition: Assert.hpp:16
#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:108
C++-compatible configuration header for fprime configuration.
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
#define STATIC
static for non unit-test code
Definition: BasicTypes.h:90
void registerHook()
Definition: Assert.cpp:98
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:185