F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
AssertFatalAdapterComponentImpl.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title AssertFatalAdapterImpl.cpp
3 // \author tcanham
4 // \brief cpp file for AssertFatalAdapter component implementation class
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include <Fw/FPrimeBasicTypes.hpp>
14 #include <Fw/Logger/Logger.hpp>
15 #include <Fw/Types/Assert.hpp>
16 #include <Fw/Types/StringUtils.hpp>
17 #include <Fw/Types/assert_hook.hpp>
19 #include <algorithm>
20 #include <cassert>
21 #include <cstdio>
22 #include <limits>
23 
24 namespace Svc {
25 
26 // ----------------------------------------------------------------------
27 // Construction, initialization, and destruction
28 // ----------------------------------------------------------------------
29 
32  // register component with adapter
33  this->m_adapter.regAssertReporter(this);
34  // register adapter
35  this->m_adapter.registerHook();
36  // Initialize the assert counter
37  this->m_assertCount = 0;
38 }
39 
41 
42 void AssertFatalAdapterComponentImpl::AssertFatalAdapter::reportAssert(FILE_NAME_ARG file,
43  FwSizeType lineNo,
44  FwSizeType numArgs,
45  FwAssertArgType arg1,
46  FwAssertArgType arg2,
47  FwAssertArgType arg3,
48  FwAssertArgType arg4,
49  FwAssertArgType arg5,
50  FwAssertArgType arg6) {
51  if (m_compPtr) {
52  m_compPtr->reportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6);
53  } else {
54  // Can't assert, what else can we do? Maybe somebody will see it.
55  Fw::Logger::log("Svc::AssertFatalAdapter not registered!\n");
56  assert(false);
57  }
58 }
59 
60 void AssertFatalAdapterComponentImpl::AssertFatalAdapter::regAssertReporter(AssertFatalAdapterComponentImpl* compPtr) {
61  this->m_compPtr = compPtr;
62 }
63 
65 
66 AssertFatalAdapterComponentImpl::AssertFatalAdapter::~AssertFatalAdapter() {}
67 
68 void AssertFatalAdapterComponentImpl::AssertFatalAdapter::doAssert() {
69  // do nothing since there will be a FATAL
70 }
71 
73  FwSizeType lineNo,
74  FwSizeType numArgs,
75  FwAssertArgType arg1,
76  FwAssertArgType arg2,
77  FwAssertArgType arg3,
78  FwAssertArgType arg4,
79  FwAssertArgType arg5,
80  FwAssertArgType arg6) {
81  // Get file arg for events
82 #if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
83  Fw::LogStringArg fileArg;
84  fileArg.format("0x%08" PRIX32, file);
85 #else
86  const FwSizeType outputSize = std::min(static_cast<FwSizeType>(AssertFatalAdapterEventFileSize),
87  static_cast<FwSizeType>(FW_LOG_STRING_MAX_SIZE));
88  // File name argument is derived from a compiler string literal. Per the C++ standard, string literals are
89  // guaranteed to be null-terminated and whose maximum length is from U16_MAX to available system memory size. Thus,
90  // we choose a bound of the lowest allowed maximum literal length.
91  const CHAR* start = Fw::StringUtils::string_last_n(file, outputSize, std::numeric_limits<U16>::max());
92 
93  Fw::LogStringArg fileArg(start);
94 #endif
95 
97  Fw::formatAssertMessage(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, msg,
98  static_cast<FwSizeType>(sizeof(msg)));
99  Fw::Logger::log("%s\n", msg);
100 
101  // Increment active assert counter
102  this->m_assertCount++;
103 
104  // Handle the case where the ports aren't connected yet or we've surpassed the maximum cascading FW_ASSERT failures
105  if (not this->isConnected_Log_OutputPort(0) || this->m_assertCount > FW_ASSERT_COUNT_MAX) {
106  // assert() is a no-op when NDEBUG is defined, so this counter must be released here as it
107  // is on the normal path below. Otherwise unreported asserts accumulate until the count
108  // exceeds FW_ASSERT_COUNT_MAX permanently and no further assert is ever reported.
109  this->m_assertCount--;
110  assert(false);
111  return;
112  }
113 
114  switch (numArgs) {
115  case 0:
116  this->log_FATAL_AF_ASSERT_0(fileArg, static_cast<U32>(lineNo));
117  break;
118  case 1:
119  this->log_FATAL_AF_ASSERT_1(fileArg, static_cast<U32>(lineNo), static_cast<U32>(arg1));
120  break;
121  case 2:
122  this->log_FATAL_AF_ASSERT_2(fileArg, static_cast<U32>(lineNo), static_cast<U32>(arg1),
123  static_cast<U32>(arg2));
124  break;
125  case 3:
126  this->log_FATAL_AF_ASSERT_3(fileArg, static_cast<U32>(lineNo), static_cast<U32>(arg1),
127  static_cast<U32>(arg2), static_cast<U32>(arg3));
128  break;
129  case 4:
130  this->log_FATAL_AF_ASSERT_4(fileArg, static_cast<U32>(lineNo), static_cast<U32>(arg1),
131  static_cast<U32>(arg2), static_cast<U32>(arg3), static_cast<U32>(arg4));
132  break;
133  case 5:
134  this->log_FATAL_AF_ASSERT_5(fileArg, static_cast<U32>(lineNo), static_cast<U32>(arg1),
135  static_cast<U32>(arg2), static_cast<U32>(arg3), static_cast<U32>(arg4),
136  static_cast<U32>(arg5));
137  break;
138  case 6:
139  this->log_FATAL_AF_ASSERT_6(fileArg, static_cast<U32>(lineNo), static_cast<U32>(arg1),
140  static_cast<U32>(arg2), static_cast<U32>(arg3), static_cast<U32>(arg4),
141  static_cast<U32>(arg5), static_cast<U32>(arg6));
142  break;
143  default:
144  this->log_FATAL_AF_UNEXPECTED_ASSERT(fileArg, static_cast<U32>(lineNo), static_cast<U32>(numArgs));
145  break;
146  }
147  // Assert processing complete, decrement active assert counter
148  this->m_assertCount--;
149 }
150 } // end namespace Svc
void formatAssertMessage(FILE_NAME_ARG file, FwSizeType lineNo, FwSizeType numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6, CHAR *destBuffer, FwSizeType buffSize)
format an assert message
void log_FATAL_AF_UNEXPECTED_ASSERT(const Fw::StringBase &file, U32 line, U32 numArgs) const
void log_FATAL_AF_ASSERT_1(const Fw::StringBase &file, U32 line, U32 arg1) const
PlatformSizeType FwSizeType
void log_FATAL_AF_ASSERT_3(const Fw::StringBase &file, U32 line, U32 arg1, U32 arg2, U32 arg3) const
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
char CHAR
Definition: BasicTypes.h:60
void log_FATAL_AF_ASSERT_2(const Fw::StringBase &file, U32 line, U32 arg1, U32 arg2) const
const char * string_last_n(const char *source, const FwSizeType n, const FwSizeType buffer_size)
get a pointer to the last N characters of a string
Definition: StringUtils.cpp:24
void log_FATAL_AF_ASSERT_5(const Fw::StringBase &file, U32 line, U32 arg1, U32 arg2, U32 arg3, U32 arg4, U32 arg5) const
#define FILE_NAME_ARG
Definition: Assert.hpp:16
void log_FATAL_AF_ASSERT_4(const Fw::StringBase &file, U32 line, U32 arg1, U32 arg2, U32 arg3, U32 arg4) const
bool isConnected_Log_OutputPort(FwIndexType portNum) const
void log_FATAL_AF_ASSERT_0(const Fw::StringBase &file, U32 line) const
void reportAssert(FILE_NAME_ARG file, FwSizeType lineNo, FwSizeType numArgs, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6)
Report the assert as a FATAL.
FormatStatus format(const CHAR *formatString,...)
write formatted string to buffer
Definition: StringBase.cpp:58
#define FW_ASSERT_TEXT_SIZE
Size of string used to store assert description // NO_CODESONAR LANG.PREPROC.MACROSTART/END.
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
RateGroupDivider component implementation.
static constexpr SizeType BUFFER_SIZE(SizeType maxLength)
Get the size of a null-terminated string buffer.
AssertFatalAdapterComponentImpl AssertFatalAdapter
Auto-generated base for AssertFatalAdapter component.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
void log_FATAL_AF_ASSERT_6(const Fw::StringBase &file, U32 line, U32 arg1, U32 arg2, U32 arg3, U32 arg4, U32 arg5, U32 arg6) const