How to disable std :: clog logging from source?

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

I can confidently comment on all my //std::clog , but is there a better way to disable the entire entry in my source file?

+7
c ++ c ++ 11
source share
2 answers

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

 std::ofstream nullstream; std::clog.rdbuf(nullstream.rdbuf()); 
+7
source share

Copied from the answer by Andreas Papadopoulos to a slightly different question - be sure to support him there!


Of course 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 due to the fact that transferring the stream to the fail state will cause it to silently discard any output until bit bit is cleared.

+1
source share

All Articles