[ create a new paste ] login | about

Link: http://codepad.org/Le84Gkbi    [ raw code | fork ]

C++, pasted on Nov 29:
class Log
{ //small class that saves std::clog's streambuffer, replaces it with a user defined filename.
  //when the object goes out of scope, std::clog is returned its original streambuff.
private:
  std::streambuf* clog_save;
  std::ofstream ofs;
public:
  Log(const std::string &LogFilename)
  {
    clog_save = std::clog.rdbuf();
    ofs.open(LogFilename.c_str(), std::ios_base::app | std::ios_base::out);
    std::clog.rdbuf(ofs.rdbuf());
  }
  ~Log()
  {
    //reset the buffer
    std::clog.rdbuf(clog_save);
    ofs.close();
  }
};

int main()
{
  Log mylog("./Log.txt");
  std::clog << "This is the log" << std::endl;

}


Output:
No errors or program output.


Create a new paste based on this one


Comments: