| 12345678910111213141516171819202122232425262728293031 |
- //
- // scoped_buffer_capture.h
- // scoped_buffer_capture
- //
- // Created by Sam Jaffe on 6/2/18.
- //
- #pragma once
- class scoped_buffer_capture_t {
- public:
- scoped_buffer_capture_t(std::ostream & stream)
- : sbuf(stream.rdbuf())
- , out(stream) {
- out.rdbuf(buffer.rdbuf());
- }
-
- ~scoped_buffer_capture_t() {
- out.rdbuf(sbuf);
- }
-
- std::string str() const { return buffer.str(); }
- private:
- std::stringstream buffer{};
- std::streambuf * sbuf;
- std::ostream & out;
- };
- #define capture_stdout() scoped_buffer_capture_t cap_cout(std::cout)
- #define capture_stderr() scoped_buffer_capture_t cap_cerr(std::cerr)
|