| // 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 |
| #define CATCH_CONFIG_NOSTDOUT |
| #define CATCH_CONFIG_MAIN |
| #include <catch2/catch.hpp> |
| class out_buff : public std::stringbuf { |
| out_buff(std::FILE* stream):m_stream(stream) {} |
| for (unsigned char c : str()) { |
| if (putc(c, m_stream) == EOF) { |
| // Reset the buffer to avoid printing it multiple times |
| out_buff::~out_buff() { pubsync(); } |
| #pragma clang diagnostic ignored "-Wexit-time-destructors" // static variables in cout/cerr/clog |
| static std::ostream ret(new out_buff(stdout)); |
| static std::ostream ret(new out_buff(stderr)); |
| TEST_CASE("This binary uses putc to write out output", "[compilation-only]") { |
| SUCCEED("Nothing to test."); |