Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is good practice for generating verbose output?

Tags:

c++

verbosity

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?

like image 954
andreas-h Avatar asked Aug 10 '09 15:08

andreas-h


People also ask

What is meant by verbose output?

The verbose option specifies that you want to display detailed processing information on your screen. This is the default. When you run the incremental, selective, or archive commands, information is displayed about each file that is backed up. Use the quiet option if you do not want to display this information.

What is verbose log output?

Verbose logging is a type of computer logging method that involves more information than the standard or typical logging process. Typically, users can turn on verbose logging features to get more information about a system.

How do you run a verbose?

There are several ways to enable Verbose Mode. During startup, the screen may display which key(s) to press on the keyboard to enable Verbose Mode. Usually, users would press the Esc (escape) key for Linux, or the keyboard shortcut Ctrl + V for Microsoft Windows, and Command + V for macOS.

What is verbose method?

Verbose is an option that can be used on the Java command to enable debug output for class loading. By default, this debug output is not enabled. This option can be used to determine what location the JDBC driver classes (and any/all other classes) are being loaded from.


4 Answers

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.

like image 171
Kirill V. Lyadvinsky Avatar answered Nov 10 '22 06:11

Kirill V. Lyadvinsky


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;
}
like image 34
Jonas Byström Avatar answered Nov 10 '22 06:11

Jonas Byström


You could use log4cpp

like image 22
Glen Avatar answered Nov 10 '22 07:11

Glen


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"
like image 27
ezpz Avatar answered Nov 10 '22 07:11

ezpz