Difference between revisions of "CPP/Logging"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==Logging.cpp== <source lang="cpp"> #include <iostream> #include <fstream> using namespace std; void ChangeCoutDestination(); void LogToScreen(); void LogToFile(); int main() {…')
 
(No difference)

Latest revision as of 08:35, 23 June 2010

Logging.cpp

#include <iostream>
#include <fstream>
using namespace std;
 
void ChangeCoutDestination();
void LogToScreen();
void LogToFile();
 
int main()
{
  //LogToFile();
  LogToScreen();
  return 0;
}
 
void ChangeCoutDestination()
{
  ofstream ofs("file.out");
  cout.rdbuf(ofs.rdbuf());
  cout << "Output." << endl;
  ofs.close();
}
 
void LogToScreen()
{
  clog << "Logged." << endl;
  cout << "Test" << endl;
}
 
void LogToFile()
{
  //save the original buffer
  streambuf *clog_save = clog.rdbuf();
  ofstream ofs("file.log");
  clog.rdbuf(ofs.rdbuf());
  clog << "Logged." << endl;
  cout << "Test" << endl;
  //reset the buffer
  clog.rdbuf(clog_save);
  ofs.close();
}