fork(2) download
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. struct Logger
  6. {
  7. Logger(std::ostream& os) : m_log(os.rdbuf()) { }
  8.  
  9. std::streambuf* reset(std::ostream& os)
  10. {
  11. return m_log.rdbuf(os.rdbuf());
  12. }
  13.  
  14. template <typename T> friend Logger& operator<<(Logger& os, const T& t)
  15. { os.m_log << t; return os; }
  16.  
  17. friend Logger& operator<<(Logger& os, std::ostream& ( *pf )(std::ostream&))
  18. { os.m_log << pf; return os; }
  19.  
  20. private:
  21. std::ostream m_log;
  22. };
  23.  
  24. int main(int argc, const char *argv[])
  25. {
  26. Logger logto(std::cout);
  27.  
  28. logto << "Hello world" << std::endl;
  29.  
  30. logto.reset(std::cerr);
  31. logto << "Error world" << std::endl;
  32.  
  33. return 0;
  34. }
  35.  
  36.  
  37.  
Success #stdin #stdout 0s 2724KB
stdin
Standard input is empty
stdout
Hello world