Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

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,?

like image 997
Yeo Avatar asked Aug 26 '16 07:08

Yeo


2 Answers

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

std::ofstream nullstream;
std::clog.rdbuf(nullstream.rdbuf());
like image 165
Wiki Wang Avatar answered Oct 07 '22 00:10

Wiki Wang


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.

like image 22
3 revs, 2 users 88% Avatar answered Oct 06 '22 23:10

3 revs, 2 users 88%