From bc524d70253a4ab2fe40c3ca3e5666e267c0a4d1 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 29 Jun 2025 19:25:29 +0100 Subject: import catch2 3.8.1 --- examples/231-Cfg-OutputStreams.cpp | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 examples/231-Cfg-OutputStreams.cpp (limited to 'examples/231-Cfg-OutputStreams.cpp') diff --git a/examples/231-Cfg-OutputStreams.cpp b/examples/231-Cfg-OutputStreams.cpp new file mode 100644 index 0000000..5aee38b --- /dev/null +++ b/examples/231-Cfg-OutputStreams.cpp @@ -0,0 +1,63 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +// 231-Cfg-OutputStreams.cpp +// Show how to replace the streams with a simple custom made streambuf. + +// Note that this reimplementation _does not_ follow `std::cerr` +// semantic, because it buffers the output. For most uses however, +// there is no important difference between having `std::cerr` buffered +// or unbuffered. +#include + +#include +#include + +class out_buff : public std::stringbuf { + std::FILE* m_stream; +public: + out_buff(std::FILE* stream):m_stream(stream) {} + ~out_buff() override; + int sync() override { + int ret = 0; + for (unsigned char c : str()) { + if (putc(c, m_stream) == EOF) { + ret = -1; + break; + } + } + // Reset the buffer to avoid printing it multiple times + str(""); + return ret; + } +}; + +out_buff::~out_buff() { pubsync(); } + +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wexit-time-destructors" // static variables in cout/cerr/clog +#endif + +namespace Catch { + std::ostream& cout() { + static std::ostream ret(new out_buff(stdout)); + return ret; + } + std::ostream& clog() { + static std::ostream ret(new out_buff(stderr)); + return ret; + } + std::ostream& cerr() { + return clog(); + } +} + + +TEST_CASE("This binary uses putc to write out output", "[compilation-only]") { + SUCCEED("Nothing to test."); +} -- cgit v1.2.3