简体   繁体   中英

Redirect std:cout to std::ofstream , getting an error

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM