what is good practice for generating verbose output? currently, i have a function
bool verbose;
int setVerbose(bool v)
{
errormsg = "";
verbose = v;
if (verbose == v)
return 0;
else
return -1;
}
and whenever i want to generate output, i do something like
if (debug)
std::cout << "deleting interp" << std::endl;
however, i don't think that's very elegant. so i wonder what would be a good way to implement this verbosity switch?
You can wrap your functionality in a class that supports the << operator which allows you to do something like
Then you can do something like
You could use log4cpp
1. If you are using g++ you could use the -D flag, this allows the compilator to define a macro of your choice.
Defining the
For instance :
2. I agree this isn't elegant either, so to make it a bit nicer :
That you could use like printf :
3. A third solution easier, and more C++ and Unix like is to pass an argument to your program that is going to be used - as the macro earlier - to initialize a particular variable (that could be a global const).
Example : $ ./myprogram -v
The simplest way is to create small class as follows(here is Unicode version, but you can easily change it to single-byte version):
Helper function
log
was made template to get nice call syntax. Then it could be used in the following way:You could change verbosity level at runtime by changing global
GLOBAL_LEVEL
variable.