C ++ custom class cout - console output and log file

I am working on a program that heavily uses "cout <strSomething"; to register information on the console. I need to change the program so that all console output goes to both the console and the file. Although I can modify "cout <, there are several large third-party libraries in our code that also use" cout <"; these libraries cannot be changed due to their licenses - so changing all references to" cout <is not a solution. In addition, using "wtee.exe" is not possible due to the way the command lines are executed.

I am using Visual Studio 2008. I saw a post in Google Groups: redirecting cout to a file that seems to do EXACTLY what I want to do. The only problem is that the code will not compile. I get C2248 errors “cannot access protected member” when calling → overflow () and → sync () method.

Does anyone know how to get this code to compile? Or an alternative way to redirect cout to the console and file at the same time?

+7
source share
6 answers

pubsync calls can be replaced with pubsync . As for the overflow call, I think it could be a typo. as it looks as if it should be a sputc call.

+1
source

boost::iostreams::tee_device for this

 #include <boost/iostreams/stream.hpp> #include <boost/iostreams/tee.hpp> #include <fstream> #include <iostream> int main() { typedef boost::iostreams::tee_device<std::ostream, std::ofstream> Tee; typedef boost::iostreams::stream<Tee> TeeStream; std::ofstream file( "foo.out" ); Tee tee( std::cout, file ); TeeStream both( tee ); both << "this goes to both std::cout and foo.out" << std::endl; return 0; } 

Sample call:

 samm$ ./a.out this goes to both std::cout and foo.out samm$ cat foo.out this goes to both std::cout and foo.out samm$ 
+12
source

if you are desperate:

 #define protected public #include <iostream> #undef protected 

This is a gross hack, but it usually works.

+2
source

What you can do is grab std::cout.rdbuf() pointer to std::streambuf , then I think you should be able to write all the outputs in std::cout to some file.

+1
source

you can just use a wrapper class to do this, something like this

 #include <iostream> #include <fstream> ... class streamoutput { std::ofstream fileoutput; public: streamoutput(char*filename){ fileoutput.open(filename); } ~streamoutput(){ fileoutput.close(); } template<class TOut> streamoutput& operator <<(const TOut& data) { fileoutput << data; std::cout << data; return this; } }; extern streamoutput cout("logfile.log"); 

declare cout this way and just change all your #include <iostream> to include this shell (remeber cout is an external variable, so you need to specify it in one of your source codes too).

0
source

Sorry to warm it up so late, but it should be a solution with redirecting cout to teebuffer based on the Dietmar Kühl solution in Google groups.

Use is simple

 GetSetLog log("myfile.log"); 

During the lifetime of the "log" object, everything will be written both in cout / cerr and in the file

https://sourceforge.net/p/getset/code/ci/master/tree/GetSet/GetSetLog.hxx

0
source

All Articles