Extracted Error and Output redirection.

Making reuse possible.
This commit is contained in:
Pete Johns 2010-09-26 13:19:30 +10:00
parent ff7a99ad37
commit 77089293c6
2 changed files with 44 additions and 38 deletions

43
test/redirect.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef REDIRECT_H
#define REDIRECT_H
#include <iostream>
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

View File

@ -16,47 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <iostream>
#include "testsuite.h" #include "testsuite.h"
#include "cmdlineparser.h" #include "cmdlineparser.h"
#include "settings.h" #include "settings.h"
#include "redirect.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;
class TestCmdlineParser : public TestFixture class TestCmdlineParser : public TestFixture
{ {