views:

566

answers:

4

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?

+3  A: 

You could use log4cpp

Glen
Does that have asynchronous logging capabilities?
windfinder
I don't know if it does or not. sorry.
Glen
I agree that using a proper logging library is the answer rather than hand-coding something, although Log4cpp appears to be moribund. http://logging.apache.org/log4cxx/index.html on the other hand is alive and kicking. According to the web site it does have support for asynchronous logging, though i don't think that's relevant to this question.
jon hanson
+1 for suggesting log4cxx, I'd forgotten about that one.
Glen
+3  A: 

You can wrap your functionality in a class that supports the << operator which allows you to do something like

class Trace {
   public:
      enum { Enable, Disable } state;
   // ...
   operator<<(...)
};

Then you can do something like

trace << Trace::Enable;
trace << "deleting interp"
ezpz
I like the idea of wrapping an error-logging system in a class. You can store things like location of output file and whatnot in the class, too.
Brian
just so i understand you correctly: i would create an instance of Trace class called trace before? and then another question: why is Trace::Enable enough to set the state to Enable? i'm still fairly new to c++, and am a bit confused. perhaps some three lines more in your idea would help me understand better what's going on. but i definitely like your idea!
andreash
Yes, you would need to create an instance of teh Trace class; this could be statically or locally to each component that wished to use it. The Trace::Enable would toggle an internal flag within the Trace class (an instance of the state enum). This would act very much like std::hex allows std::cout to output in hex format instead of decimal. Since you will be writing operator<< you can support Trace::Enable (or any other state specifiers such as verbosity and such)
ezpz
Andreash, `Trace::Enable` would be enough to set the state to `Enable` because you're going to write code for the `<<` operator that makes your class know how to handle that flag. There's no magic here. Personally, I'd just make an ordinary member function and say `trace.enable()`.
Rob Kennedy
+3  A: 
int threshold = 3;
class mystreambuf: public std::streambuf
{
};
mystreambuf nostreambuf;
std::ostream nocout(&nostreambuf);
#define log(x) ((x >= threshold)? std::cout : nocout)

int main()
{
    log(1) << "No hello?" << std::endl;     // Not printed on console, too low log level.
    log(5) << "Hello world!" << std::endl;  // Will print.
    return 0;
}
Jonas Byström
would there be a possibility to 'include' the std::endl into the log() definition?
andreash
Define `log(x)` can be easily replaced with inline function. Is there reason to use defines here?
Kirill V. Lyadvinsky
Regarding std::endl: none that I know of. If you want less code, you could always use typedef, such as _e. Regardig #define/inline: no reason.
Jonas Byström
+2  A: 

The simplest way is to create small class as follows(here is Unicode version, but you can easily change it to single-byte version):

#include <sstream>
#include <boost/format.hpp>
#include <iostream>
using namespace std;

enum log_level_t {
    LOG_NOTHING,
    LOG_CRITICAL,
    LOG_ERROR,
    LOG_WARNING,
    LOG_INFO,
    LOG_DEBUG
};

namespace log_impl {
class formatted_log_t {
public:
    formatted_log_t( log_level_t level, const wchar_t* msg ) : fmt(msg), level(level) {}
    ~formatted_log_t() {
        // GLOBAL_LEVEL is a global variable and could be changed at runtime
        // Any customization could be here
        if ( level <= GLOBAL_LEVEL ) wcout << level << L" " << fmt << endl;
    }        
    template <typename T> 
    formatted_log_t& operator %(T value) {
        fmt % value;
        return *this;
    }    
protected:
    log_level_t  level;
    boost::wformat   fmt;
};
}//namespace log_impl
// Helper function. Class formatted_log_t will not be used directly.
template <log_level_t level>
log_impl::formatted_log_t log(const wchar_t* msg) {
    return log_impl::formatted_log_t( level, msg );
}

Helper function log was made template to get nice call syntax. Then it could be used in the following way:

int main ()
{
    // Log level is clearly separated from the log message
    log<LOG_DEBUG>(L"TEST %3% %2% %1%") % 5 % 10 % L"privet";
    return 0;
}

You could change verbosity level at runtime by changing global GLOBAL_LEVEL variable.

Kirill V. Lyadvinsky
i like this answer. i really only need this very simple stdout logging, no file/network stuff ever needed (i hope). so i will try to keep the number of external dependencies low and go for my own implementation.
andreash