scoped_buffer_capture.h 623 B

12345678910111213141516171819202122232425262728293031
  1. //
  2. // scoped_buffer_capture.h
  3. // scoped_buffer_capture
  4. //
  5. // Created by Sam Jaffe on 6/2/18.
  6. //
  7. #pragma once
  8. class scoped_buffer_capture_t {
  9. public:
  10. scoped_buffer_capture_t(std::ostream & stream)
  11. : sbuf(stream.rdbuf())
  12. , out(stream) {
  13. out.rdbuf(buffer.rdbuf());
  14. }
  15. ~scoped_buffer_capture_t() {
  16. out.rdbuf(sbuf);
  17. }
  18. std::string str() const { return buffer.str(); }
  19. private:
  20. std::stringstream buffer{};
  21. std::streambuf * sbuf;
  22. std::ostream & out;
  23. };
  24. #define capture_stdout() scoped_buffer_capture_t cap_cout(std::cout)
  25. #define capture_stderr() scoped_buffer_capture_t cap_cerr(std::cerr)