By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

Creating an automatic logger

P: n/a
I want to create an automatic log system that writes to a ofstream
(the logfile) all the output sent to screen.

I tried to make a class COutput with the operator << overloaded for
the different kind of data types, but a problem arrived when I tried
to use io manipulators such as setprecision.
I have this in class COutput:
COutput& operator<<(ios& (*fnc)(ios&)) {
(*fnc)(cout); // For the screen
(*fnc)(logfile); // For the logfile (an ofstream)
}

But it don't work. At least with the endl manipulator, it worked with:
COutput& operator<<(ostream& (*fnc)(ostream&)) {
(*fnc)(cout); // For the screen
(*fnc)(logfile); // For the logfile (an ofstream)
}

How I can make the 'automatic logger'?
What's the problem with COutput, that don't work setprecision?

Any comments are appreciated.

Federico

PD: sorry for my bad English.
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Federico" <ra***@infovia.com.ar> wrote in message
news:a2**************************@posting.google.c om...
I want to create an automatic log system that writes to a ofstream
(the logfile) all the output sent to screen.

I tried to make a class COutput with the operator << overloaded for
the different kind of data types, but a problem arrived when I tried
to use io manipulators such as setprecision.
I have this in class COutput:
COutput& operator<<(ios& (*fnc)(ios&)) {
(*fnc)(cout); // For the screen
(*fnc)(logfile); // For the logfile (an ofstream)
}

But it don't work. At least with the endl manipulator, it worked with:
COutput& operator<<(ostream& (*fnc)(ostream&)) {
(*fnc)(cout); // For the screen
(*fnc)(logfile); // For the logfile (an ofstream)
}

How I can make the 'automatic logger'?
I have seen a technique in a post to comp.lang.c++ which allows you to
(re-)use manipulators without any problems.

+++ code example from a post by Jim Fischer on 26 December 1999
(http://tinyurl.com/ah4l). +++

#include <iostream>
#include <fstream>
using namespace std ;

int main()
{
char const *const filename = "temp.txt" ;
streambuf* pcoutbuf = NULL ;
filebuf fbuf ;

// try to open the output file
fbuf.open( filename, ios::out );
if ( !fbuf.is_open() )
{
cerr << "ERROR: Unable to open file " << filename << '\n' ;
return 1 ;
}

// Redirect cout to the output file by telling cout
// to use the filebuf object instead of its stdout
// streambuf object. Also, save the address of cout's
// stdout streambuf object so that we can reload
// that object back into cin later on.

pcoutbuf = cout.rdbuf( &fbuf );

// send some text to the file
cout << "Hello, World!" << endl ;

// redirect cout to stdout
cout.rdbuf( pcoutbuf );

// send some text to stdout
cout << "Goodbye!" << endl ;

// close the file
fbuf.close();
}
What's the problem with COutput, that don't work setprecision?

Any comments are appreciated.

Federico

PD: sorry for my bad English.

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.