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.