From 77089293c6374d50fcaa940e5416043ef511cfe0 Mon Sep 17 00:00:00 2001 From: Pete Johns Date: Sun, 26 Sep 2010 13:19:30 +1000 Subject: [PATCH] Extracted Error and Output redirection. Making reuse possible. --- test/redirect.h | 43 ++++++++++++++++++++++++++++++++++++++ test/testcmdlineparser.cpp | 39 +--------------------------------- 2 files changed, 44 insertions(+), 38 deletions(-) create mode 100644 test/redirect.h diff --git a/test/redirect.h b/test/redirect.h new file mode 100644 index 000000000..030952ba1 --- /dev/null +++ b/test/redirect.h @@ -0,0 +1,43 @@ +#ifndef REDIRECT_H +#define REDIRECT_H + +#include + +extern std::ostringstream errout; +extern std::ostringstream output; + +class RedirectInputOutput +{ +public: + RedirectInputOutput() + { + // flush all old output + std::cout.flush(); + std::cerr.flush(); + + _oldCout = std::cout.rdbuf(); // back up cout's streambuf + _oldCerr = std::cerr.rdbuf(); // back up cerr's streambuf + + std::cout.rdbuf(_out.rdbuf()); // assign streambuf to cout + std::cerr.rdbuf(_err.rdbuf()); // assign streambuf to cerr + } + + ~RedirectInputOutput() + { + std::cout.rdbuf(_oldCout); // restore cout's original streambuf + std::cerr.rdbuf(_oldCerr); // restore cerrs's original streambuf + + errout << _err.str(); + output << _out.str(); + } + +private: + std::stringstream _out; + std::stringstream _err; + std::streambuf* _oldCout; + std::streambuf *_oldCerr; +}; + +#define REDIRECT RedirectInputOutput redir; + +#endif diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 5ee6efed0..bf4cba11e 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -16,47 +16,10 @@ * along with this program. If not, see . */ -#include #include "testsuite.h" #include "cmdlineparser.h" #include "settings.h" - -extern std::ostringstream errout; -extern std::ostringstream output; - -class RedirectInputOutput -{ -public: - RedirectInputOutput() - { - // flush all old output - std::cout.flush(); - std::cerr.flush(); - - _oldCout = std::cout.rdbuf(); // back up cout's streambuf - _oldCerr = std::cerr.rdbuf(); // back up cerr's streambuf - - std::cout.rdbuf(_out.rdbuf()); // assign streambuf to cout - std::cerr.rdbuf(_err.rdbuf()); // assign streambuf to cerr - } - - ~RedirectInputOutput() - { - std::cout.rdbuf(_oldCout); // restore cout's original streambuf - std::cerr.rdbuf(_oldCerr); // restore cerrs's original streambuf - - errout << _err.str(); - output << _out.str(); - } - -private: - std::stringstream _out; - std::stringstream _err; - std::streambuf* _oldCout; - std::streambuf *_oldCerr; -}; - -#define REDIRECT RedirectInputOutput redir; +#include "redirect.h" class TestCmdlineParser : public TestFixture {