简体   繁体   中英

How to disable std::clog logging from source code?

When developing code, I have many console logging ( std::clog ) and some console output ( std::cout ). But now, I wanted to do online submission of my source code and I want to disable all the console logging ( clog ) but keep the console output ( cout )

I can surely comment all of my //std::clog , but is there a better way to disable all logging inside my source file,?

You can redirect clog, create your own ofstream and use rdbuf function.

std::ofstream nullstream;
std::clog.rdbuf(nullstream.rdbuf());

Copied from Andreas Papadopoulos' answer to a slightly different question -- be sure to upvote him there!


Sure, you can ( example here ):

int main() {
    std::clog << "First message" << std::endl;

    std::clog.setstate(std::ios_base::failbit);
    std::clog << "Second message" << std::endl;

    std::clog.clear();
    std::clog << "Last message" << std::endl;

    return 0;
}

Outputs:

First message
Last message

This is because putting the stream in fail state will make it silently discard any output, until the failbit is cleared.

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