1

I have output in a C++ program that I only want to see if a "verbose" option is specified at runtime. I've found lots of methods to use preprocessor #define statements to control debugging output when compiling, but I can't find any ways to do this at runtime short of wrapping every cout in if(verbose).

In pseudocode, I'd like to transform:

if(verbose) cout << "Some text: " << variable << endl; ... if(verbose) cout << "Other text: " << var << endl; 

Into:

if(verbose) //block cout cout << "Some text: " << variable << endl; cout << "Other text: " << var << endl; 

Is there some way to optionally redefine cout at runtime so those lines silently print nothing? Better yet would be a more flexible approach that allows some output while blocking others.

11
  • cout is character out, distinguished from wcout (wide-character out). It is not "console". Do not conflate stdout with a tty. Commented Dec 11, 2014 at 15:15
  • stackoverflow.com/questions/7721026/programatically-ignore-cout :-) Commented Dec 11, 2014 at 15:16
  • @WilliamPursell I don't fully understand the distinction you're making. This is a program that runs in a terminal and any cout statements appear in the terminal (which I understand to be stdout). Should the question begin "I have tty output..."? Commented Dec 11, 2014 at 15:19
  • Your question talks about "console output". But there is nothing related to a 'console' anywhere in your code unless you mistakenly assume that stdout is a console. I am pointing out that it is a mistake to think that stdout is a console. Commented Dec 11, 2014 at 15:20
  • @jake It depends on the system. At least under Unix and Windows, regardless of where the program runs, there is no assurance that std::cout goes to the console; both systems support pipes and redirection. Commented Dec 11, 2014 at 15:35

2 Answers 2

3

You can simply bind a stream object reference to different streams depending on verbose:

ostream& vout = (verbose? cout : nullstream); 

Then use vout for all the output that should only appear when verbose.

Of course you need to define nullstream first; that involves defining a streambuf derived class.

Sign up to request clarification or add additional context in comments.

2 Comments

This looks like exactly what I need, but I have to digest the information about defining nullstream.
@jake: actually James Kanze has answered that (I didn't notice when I wrote the above), and there's also a Ready To Use™ implementation in Boost.
0

I'm not sure whether you're in the process of writing a program, or whether you already have a program you want to disable output for. Generally, when you're writing software with debugging output, you'll want to create some sort of logging class to help you control when output occurs and where it goes (e.g., to a file, to stdout, to stderr, etc.). An example of a simple one would be:

#include <iostream> class Logger { public: Logger(bool enabled) : enabled(enabled) { } void setEnabled(bool enabled) { this->enabled = enabled; } template<typename T> Logger& operator<<(T const& t) { if (enabled) std::cout << t; return *this; } // If you'd like manipulator support (e.g., for std::endl) Logger& operator<<(std::ostream& (*manipulator)(std::ostream&)) { if (enabled) std::cout << manipulator; return *this; } private: bool enabled; }; int main() { Logger log(true); log << "Hello, " << "World!" << 123; // Prints Hello, World!123 log.setEnabled(false); log << "Hello, " << "World!" << 123; // Prints nothing } 

Alternatively, you could redirect cout, as others have mentioned, or even just simply redirect your program's output to /dev/null on the command line:

./myProgram > /dev/null 

1 Comment

Thanks for fixing that @Cheersandhth.-Alf. For sure; looks good! Just going to add a comment indicating what that part is for, in case OP or any new readers aren't familiar with it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.