i have to make a project for school , making a newsserver and a client.
the thing is, i send messages between them to let the user know if everything is running ok. just some simple http messages as 201, etc.
as a extention id like to build in a log file maker.
what i want is: everything that is put on the output stream will also appear in a logfile.
i already found this:
Expand|Select|Wrap|Line Numbers
- ofstream out("logFile.txt");
- if(out) {
- streambuf *pStreambuf = cout.rdbuf(); // save cout streambuf
- cout.rdbuf(out.rdbuf()); // give cout file's streambuf
- // The following text will be written to LogFile.txt. You might
- // want to do something like this to give a user the option of
- // seeing output on-screen, or getting it off-line in a file.
- cout << "\n This output text will be displayed on cout, "
- << "\n then redirected to a log file using streambufs."
- << "\n Note that all output is written to cout."
- << "\n Redirection occurs by giving cout the streambuf"
- << "\n of the log file\n\n";
- cout.rdbuf(pStreambuf); // restore cout streambuf
- out.close();
I want it to appear on the user's screen as well as written in the file.
is there a way so i can simply specify that whats on cout should be coppied into this logfile? and so i dont have to add to each std::cout i have now:
ofstream file("logfile");
file << INPUTTEXT;
Thank you all for reading!