From 291c98bf0b1a40489877e4e59e49d229dfc0e20b Mon Sep 17 00:00:00 2001 From: Taylor Foxhall Date: Thu, 23 Jan 2025 12:31:01 -0500 Subject: [PATCH] Flush output on test and contract failures Signed-off-by: Taylor Foxhall --- src/groups/bmq/bmqtst/bmqtst_testhelper.h | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/groups/bmq/bmqtst/bmqtst_testhelper.h b/src/groups/bmq/bmqtst/bmqtst_testhelper.h index 40b76b7b6c..4a652992c1 100644 --- a/src/groups/bmq/bmqtst/bmqtst_testhelper.h +++ b/src/groups/bmq/bmqtst/bmqtst_testhelper.h @@ -791,7 +791,12 @@ static inline void _assert(bool result, const char* expression, const char* file, int line) { if (!result) { - printf("Error %s(%d): %s (failed)\n", file, line, expression); + bsl::fprintf(stdout, + "Error %s(%d): %s (failed)\n", + file, + line, + expression); + bsl::fflush(stdout); if (bmqtst::TestHelperUtil::testStatus() >= 0 && bmqtst::TestHelperUtil::testStatus() <= 100) { ++bmqtst::TestHelperUtil::testStatus(); @@ -806,10 +811,17 @@ BSLS_ANNOTATION_NORETURN static inline void _assertViolationHandler(const bsls::AssertViolation& violation) { - printf("Error %s(%d): %s (failed)\n", - violation.fileName(), - violation.lineNumber(), - violation.comment()); + // Since we're handling a contract failure (as opposed to a test + // assertion), we want the program to die immediately. For this reason, we + // use stderr instead of stdout. + bsl::fprintf(stderr, + "Error %s(%d): %s (failed)\n", + violation.fileName(), + violation.lineNumber(), + violation.comment()); + + // Ensure the error message is printed before terminating + bsl::fflush(stderr); bsls::AssertTest::failTestDriver(violation); }