//compile with g++ #include <iostream> #include <sstream> #include <string> using namespace std; class Streams { public: static Streams& instance() { static Streams* self = NULL; if(self == NULL) { self = new Streams(); } return *self; } static inline std::ostream& out() { return *(instance().out_); } static inline std::ostream& err() { return *(instance().err_); } void setNormal() { out_ = &std::cout; err_ = &std::cerr; } void setRedirected() { if(test) { delete test; } test = new std::ostringstream(); out_ = test; err_ = test; } private: Streams() { test = NULL; setNormal(); } ~Streams() { if(test) delete test; } std::ostream* test; std::ostream* out_; std::ostream* err_; }; int main() { Streams::out() << "bah bah bah" << endl; Streams::err() << "err erh ber" << endl; Streams::instance().setRedirected(); Streams::out() << "bah bah bah" << endl; Streams::err() << "err erh ber" << endl; }
Thursday, October 21, 2010
Stream wrappers for cerr and cout
Its always convient to have a quick wrapper class at your finger tips to turn on or off all output from you system in one foul swoop. Here is a class that adds a layer of indirection to output and allows you to go into a "testing" mode where all output is suppressed
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment