Skip to content
Snippets Groups Projects
Commit 53d7e27b authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

Merge branch '6-random-failures' into 'master'

More robust cout/cerr redirection

Closes #6

See merge request !9
parents c7867eb0 035ff004
No related branches found
No related tags found
1 merge request!9More robust cout/cerr redirection
Pipeline #
...@@ -202,11 +202,42 @@ BOB_CATCH_FUNCTION("_log_message_mt", 0) ...@@ -202,11 +202,42 @@ BOB_CATCH_FUNCTION("_log_message_mt", 0)
} }
static void _test(const std::string& expected, const std::string& obtained, const std::string& step){ static void _test(const std::string& obtained, const std::string& expected, const std::string& step){
if (expected != obtained) if (expected != obtained)
throw std::runtime_error((boost::format("The string '%s' in step %swas not '%s' as expected") % obtained % step % expected).str()); throw std::runtime_error((boost::format("The string \"%s\" in step %s was not \"%s\" as expected") % obtained % step % expected).str());
} }
// Proper redirection of a stream. If exception or similar is thrown, correctly
// resets the modified standard stream.
// See: http://stackoverflow.com/questions/5419356/redirect-stdout-stderr-to-a-string
class _ostream_redirect {
public:
_ostream_redirect(std::ostream& s)
: m_stream(s), m_buffer(), m_old(m_stream.rdbuf(m_buffer.rdbuf()))
{ }
void str(const char* v) {
m_buffer.str(v);
}
std::string str() {
return m_buffer.str();
}
~_ostream_redirect() {
m_stream.rdbuf(m_old);
}
private:
std::ostream& m_stream;
std::stringstream m_buffer;
std::streambuf* m_old;
};
static auto _output_disable_doc = bob::extension::FunctionDoc( static auto _output_disable_doc = bob::extension::FunctionDoc(
"_test_output_disable", "_test_output_disable",
"Writes C++ messages with and without being visible and raises exceptions when an error occurs." "Writes C++ messages with and without being visible and raises exceptions when an error occurs."
...@@ -219,10 +250,8 @@ BOB_TRY ...@@ -219,10 +250,8 @@ BOB_TRY
if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist)) return 0; if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist)) return 0;
// redirect std::cout and std::cerr, see http://stackoverflow.com/a/5419388/3301902 _ostream_redirect out(std::cout);
std::stringstream out, err; _ostream_redirect err(std::cerr);
std::streambuf * oldout = std::cout.rdbuf(out.rdbuf());
std::streambuf * olderr = std::cerr.rdbuf(err.rdbuf());
bob::core::log_level(bob::core::DEBUG); bob::core::log_level(bob::core::DEBUG);
bob::core::debug << "This is a debug message" << std::endl; bob::core::debug << "This is a debug message" << std::endl;
...@@ -252,10 +281,6 @@ BOB_TRY ...@@ -252,10 +281,6 @@ BOB_TRY
_test(out.str(), "", "disable"); _test(out.str(), "", "disable");
_test(err.str(), "", "disable"); _test(err.str(), "", "disable");
// make sure that cout and cerr are redirected to their original streams
std::cout.rdbuf( oldout );
std::cerr.rdbuf( olderr );
bob::core::log_level(bob::core::DEBUG); bob::core::log_level(bob::core::DEBUG);
Py_RETURN_NONE; Py_RETURN_NONE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment