Creating an automatic logger

F

Federico

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.
 
K

Klaus Eichner

Federico said:
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::eek:ut );
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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top