How to disable cout output in the runtime?
Asked Answered
J

6

34

I often use cout for debugging purpose in many different places in my code, and then I get frustrated and comment all of them manually.

Is there a way to suppress cout output in the runtime?

And more importantly, let's say I want to suppress all cout outputs, but I still want to see 1 specific output (let's say the final output of the program) in the terminal.

Is it possible to use an ""other way"" of printing to the terminal for showing the program output, and then when suppressing cout still see things that are printed using this ""other way""?

Juggle answered 12/5, 2015 at 7:58 Comment(0)
V
22

Don't use cout for debugging purposes, but define a different object (or function, or macro) that calls through to it, then you can disable that function or macro in one single place.

Villainous answered 12/5, 2015 at 8:2 Comment(0)
P
99

Sure, you can (example here):

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

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

    std::cout.clear();
    std::cout << "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.

Protostele answered 12/5, 2015 at 8:3 Comment(0)
H
30

To supress output, you can disconnect the underlying buffer from cout.

#include <iostream>

using namespace std;

int main(){

    // get underlying buffer
    streambuf* orig_buf = cout.rdbuf();

    // set null
    cout.rdbuf(NULL);

    cout << "this will not be displayed." << endl;

    // restore buffer
    cout.rdbuf(orig_buf);

    cout << "this will be dispalyed." << endl;

    return 0;
}
Hamrick answered 12/5, 2015 at 10:59 Comment(1)
Does it have runtime overhead?Gallo
V
22

Don't use cout for debugging purposes, but define a different object (or function, or macro) that calls through to it, then you can disable that function or macro in one single place.

Villainous answered 12/5, 2015 at 8:2 Comment(0)
P
5

You can user cerr - standard output stream for errors for your debug purposes.

Also, there is clog - standard output stream for logging.

Typically, they both behave like a cout.

Example:

cerr << 74 << endl;

Details: http://www.cplusplus.com/reference/iostream/cerr/

http://www.cplusplus.com/reference/iostream/clog/

Preserve answered 12/5, 2015 at 8:2 Comment(0)
W
1

If you include files which involve cout you may want to write the code at the start (outside of main), which can be done like this:

struct Clearer {
    Clearer() { std::cout.setstate(std::ios::failbit); }
} output_clearer;
Woodbine answered 5/3, 2021 at 21:51 Comment(0)
A
0

It seems you print debug messages. You could use TRACE within Visual C++/MFC or you just might want to create a Debug() function which takes care of it. You can implement it to turn on only if a distinct flag is set. A lot of programs use a command line parameter called verbose or -v for instance, to control the behavior of their log and debug messages.

Anchusin answered 12/5, 2015 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.