8000 TRI_ASSERT Log Stream (#14676) · arangodb/arangodb@1e970ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 1e970ee

Browse files
author
Lars Maier
authored
TRI_ASSERT Log Stream (#14676)
1 parent f31afe1 commit 1e970ee

File tree

5 files changed

+60
-17
lines changed

5 files changed

+60
-17
lines changed

arangod/Replication2/ReplicatedLog/LogLeader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ auto replicated_log::LogLeader::GuardedLeaderData::updateCommitIndexLeader(
481481
<< "updating commit index to " << newIndex << "with quorum " << quorum->quorum;
482482
auto oldIndex = _commitIndex;
483483

484-
TRI_ASSERT(_commitIndex < newIndex);
484+
TRI_ASSERT(_commitIndex < newIndex)
485+
<< "_commitIndex == " << _commitIndex << ", newIndex == " << newIndex;
485486
_commitIndex = newIndex;
486487
_lastQuorum = quorum;
487488

lib/Basics/CrashHandler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ void CrashHandler::crash(char const* context) {
584584
}
585585

586586
/// @brief logs an assertion failure and crashes the program
587-
void CrashHandler::assertionFailure(char const* file, int line, char const* func, char const* context) {
587+
void CrashHandler::assertionFailure(char const* file, int line, char const* func, char const* context, const char *message) {
588588
// assemble an "assertion failured in file:line: message" string
589589
char buffer[4096];
590590
memset(&buffer[0], 0, sizeof(buffer));
@@ -601,6 +601,10 @@ void CrashHandler::assertionFailure(char const* file, int line, char const* func
601601
}
602602
appendNullTerminatedString(": ", p);
603603
appendNullTerminatedString(context, 256, p);
604+
if (message != nullptr) {
605+
appendNullTerminatedString(" ; ", p);
606+
appendNullTerminatedString(message, p);
607+
}
604608

605609
crash(&buffer[0]);
606610
}

lib/Basics/CrashHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CrashHandler {
3333
[[noreturn]] static void crash(char const* context);
3434

3535
/// @brief logs an assertion failure and crashes the program
36-
[[noreturn]] static void assertionFailure(char const* file, int line, char const* func, char const* context);
36+
[[noreturn]] static void assertionFailure(char const* file, int line, char const* func, char const* context, const char* message);
3737

3838
/// @brief set flag to kill process hard using SIGKILL, in order to circumvent core
3939
/// file generation etc.

lib/Basics/debugging.h

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stdlib.h>
2828
#include <ostream>
29+
#include <sstream>
2930
#include <string>
3031
#include <type_traits>
3132
#include <utility>
@@ -199,31 +200,60 @@ enable_if_t<is_container<T>::value, std::ostream&> operator<<(std::ostream& o, T
199200
return o;
200201
}
201202

203+
namespace debug {
204+
struct NoOpStream {
205+
template <typename T>
206+
auto operator<<(T const&) noexcept -> NoOpStream& {
207+
return *this;
208+
}
209+
};
210+
211+
struct AssertionLogger {
212+
#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
213+
void operator&(std::ostringstream const& stream) const {
214+
std::string message = stream.str();
215+
arangodb::CrashHandler::assertionFailure(file, line, function, expr,
216+
message.empty() ? nullptr : message.c_str());
217+
}
218+
// can be removed in C++20 because of LWG 1203
219+
void operator&(std::ostream const& stream) const {
220+
operator&(static_cast<std::ostringstream const&>(stream));
221+
}
222+
223+
const char* file;
224+
int line;
225+
const char* function;
226+
const char* expr;
227+
#endif
228+
void operator&(NoOpStream const&) const noexcept {}
229+
static auto getOutputStream() -> std::ostringstream&& {
230+
static thread_local std::ostringstream stream;
231+
return std::move(stream);
232+
}
233+
};
234+
235+
} // namespace debug
236+
202237
} // namespace arangodb
203238

204239
/// @brief assert
205240
#ifndef TRI_ASSERT
206241

207242
#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
208243

209-
#define TRI_ASSERT(expr) /*GCOVR_EXCL_LINE*/ \
210-
do { \
211-
if (!(ADB_LIKELY(expr))) { \
212-
arangodb::CrashHandler::assertionFailure(__FILE__, __LINE__, __FUNCTION__, #expr); \
213-
} else { \
214-
} \
215-
} while (false)
244+
#define TRI_ASSERT(expr) /*GCOVR_EXCL_LINE*/ \
245+
(ADB_LIKELY(expr)) \
246+
? (void)nullptr \
247+
: ::arangodb::debug::AssertionLogger{__FILE__, __LINE__, \
248+
ARANGODB_PRETTY_FUNCTION, #expr} & \
249+
::arangodb::debug::AssertionLogger::getOutputStream()
216250

217251
#else
218252

219-
#define TRI_ASSERT(expr) /*GCOVR_EXCL_LINE*/ \
220-
do { \
221-
if (false) { \
222-
(void)(expr); \
223-
} \
224-
} while (false)
253+
#define TRI_ASSERT(expr) /*GCOVR_EXCL_LINE*/ \
254+
(true) ? ((false) ? (void)(expr) : (void)nullptr) \
255+
: ::arangodb::debug::AssertionLogger{} & ::arangodb::debug::NoOpStream {}
225256

226257
#endif // #ifdef ARANGODB_ENABLE_MAINTAINER_MODE
227258

228259
#endif // #ifndef TRI_ASSERT
229-

lib/Basics/system-compiler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,11 @@
7474
#endif
7575
#endif
7676

77+
// pretty function name macro
78+
#if defined(__clang__) || defined(__GNUC__)
79+
#define ARANGODB_PRETTY_FUNCTION __PRETTY_FUNCTION__
80+
#elif defined(_MSC_VER)
81+
#define ARANGODB_PRETTY_FUNCTION __FUNCSIG__
82+
#else
83+
#define ARANGODB_PRETTY_FUNCTION __func__
84+
#endif

0 commit comments

Comments
 (0)
0