Reputation: 715
I am redirecting my standard output data to a std::ofstream buffer, to write the data in a file. The following code implement this,
if ( isActive ){
try{
std::string traceFileName = "traceLog"+getTime()+".log";
std::ofstream out(traceFileName.c_str());
std::streambuf *countbuf = std::cout.rdbuf();
std::cout.rdbuf ( out.rdbuf() ) ;
std::cout<<"buffer pointed\n"<<std::endl;
}
when i am implementing the above code in a single .cpp, itgeting executed and dada is geting write in the file.But for the following scenarion,
A( bool isActive){
if ( isActive ){
try{
std::string traceFileName = "traceLog"+getTime()+".log";
std::ofstream out(traceFileName.c_str());
std::streambuf *countbuf = std::cout.rdbuf();
std::cout.rdbuf ( out.rdbuf() ) ;
std::cout<<"buffer pointed\n"<<std::endl;
}
And from a different .cpp the function A( true ) is getting called, and I want that after A() called then in my whole c++ project when ever std::cout<"printing data\n"; is there the data will be printed into the file. But my code is compiling fine, during RUN time throwing the following error,
(process:10365): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
(process:10365): GConf-WARNING **: Client failed to connect to the D-BUS daemon: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
(process:10365): GConf-WARNING **: Client failed to connect to the D-BUS daemon: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken. QGtkStyle was unable to detect the current GTK+ theme. Qt: Session management error: None of the authentication protocols specified are supported
But if I am placing the following code at main,
std::string traceFileName = "traceLog"+getTime()+".log";
std::ofstream out(traceFileName.c_str());
std::streambuf *countbuf = std::cout.rdbuf();
std::cout.rdbuf ( out.rdbuf() ) ;
the run time all the std::cout data is geting write in the file. ut throwing the following error. How can I solve this error.
enter code here(process:10777): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
(process:10777): GConf-WARNING **: Client failed to connect to the D-BUS daemon: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
(process:10777): GConf-WARNING **: Client failed to connect to the D-BUS daemon: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken. QGtkStyle was unable to detect the current GTK+ theme. Qt: Session management error: None of the authentication protocols specified are supported
Upvotes: 0
Views: 615
Reputation: 96845
The problem is that your buffer is not being restored before leaving the scope in which the file stream was created. std::cout
will still hold a pointer to the buffer that it eventually tries to write to.
I believe one option is to make out
static
, and the other is to create a class which reassigns the original buffer to std::cout
when the scope is being left:
class buffer_restore
{
public:
buffer_restore(std::ios& _ios)
: ios(_ios)
, sbuf(_ios.rdbuf())
{ }
~buffer_restore()
{
ios.rdbuf(sbuf);
}
private:
std::ios& ios;
std::streambuf* sbuf;
};
Here's an example of how it would be used:
{
buffer_restore br(std::cout);
std::ofstream out(traceFileName.c_str());
std::streambuf* countbuf = std::cout.rdbuf(out.rdbuf());
} // std::cout's buffer is restored to the original when br was constructed
Upvotes: 0