Skip to content

Commit cc13550

Browse files
committed
Allow overriding doAssert
1 parent 2d03f99 commit cc13550

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

Fw/Types/Assert.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <Fw/Types/Assert.hpp>
33
#include <Fw/Types/StringUtils.hpp>
44
#include <Fw/Types/assert_hook.hpp>
5-
#include <cassert>
65
#include <cstdio>
76
#include <cstdlib>
87

@@ -29,7 +28,7 @@ void AssertHook::reportAssert(FILE_NAME_ARG file,
2928
}
3029

3130
void AssertHook::doAssert() {
32-
assert(0);
31+
defaultDoAssert();
3332
}
3433

3534
AssertHook* AssertHook::s_assertHook = nullptr;
@@ -63,7 +62,7 @@ FW_ASSERT_NORETURN void defaultSwAssert(FILE_NAME_ARG file,
6362
defaultReportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6, assertMsg,
6463
static_cast<FwSizeType>(sizeof(assertMsg)));
6564
defaultPrintAssert(assertMsg);
66-
assert(0);
65+
defaultDoAssert();
6766
} else {
6867
registeredHook->reportAssert(file, lineNo, numArgs, arg1, arg2, arg3, arg4, arg5, arg6);
6968
registeredHook->doAssert();

Fw/Types/assert_hook.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020

2121
namespace Fw {
2222

23+
//! \brief handles an assertion
24+
//!
25+
//! Handle an assertion failure or FATAL. This function is called after the defaultPrintAssert has
26+
//! been called. The default implementation calls `assert(0)`.
27+
[[noreturn]] void defaultDoAssert();
28+
2329
//! \brief print an assert message
2430
//!
2531
//! Print a pre-formatted assert message. This function is called after the assert message has been formatted
26-
//! by defaultReportAssert. The default implementation uses fputs to write to stderr, but this can be overridden
27-
//! at compile time to provide custom behavior for deeply embedded systems.
32+
//! by defaultReportAssert. The default implementation uses fputs to write to stderr.
2833
//!
2934
//! \param msg: null-terminated string containing the formatted assert message
3035
void defaultPrintAssert(const CHAR* msg);

Fw/Types/fputs_assert_hook.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
// ======================================================================
66
#include <Fw/Types/assert_hook.hpp>
77
#include <Fw/Types/format.hpp>
8+
#include <cassert>
89
#include <cstdio>
10+
#include <cstdlib>
911

1012
#if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
1113
#define fileIdFs "Assert: 0x%08" PRIx32 ":%" PRI_FwSizeType ""
1214
#else
1315
#define fileIdFs "Assert: \"%s:%" PRI_FwSizeType "\""
1416
#endif
1517

18+
void Fw::defaultDoAssert() {
19+
assert(0);
20+
abort();
21+
}
22+
1623
void Fw::defaultPrintAssert(const CHAR* msg) {
1724
// Write to stderr w/o formatting
1825
(void)fputs(msg, stderr);

0 commit comments

Comments
 (0)