clstaudt
clstaudt

Reputation: 22438

How to make C++ logging more concise

I am using log4cxx for logging in C++ projects. Currently, my log calls look like

LOG4CXX_INFO(log4cxx::Logger::getRootLogger(), "parsed " << lc << " lines");

This is still too verbose and hard to remember. Ideally, a logging statement would look similar to

log.debug("parsed %d lines", lc)

a) What can I do to get more concise logging statements? b) Is it possible to use a printf-like syntax?

Upvotes: 0

Views: 539

Answers (1)

chill
chill

Reputation: 16888

You can use the Boost.Format library http://www.boost.org/doc/libs/1_52_0/libs/format/doc/format.html

PS. C++11 convenience functions example:

#include <boost/format.hpp>
#include <iostream>

void
log (const boost::format &fmt)
{
  std::cout << fmt;
}

template<typename T, typename... Args>
void
log (boost::format &fmt, const T &v, Args... args)
{
  log (boost::format (fmt) % v, args ...);
}

template<typename... Args>
void
log (const char *fmt, Args... args)
{
  boost::format f (fmt);
  log (f, args ...);
}

int
main ()
{
  log ("some number %1% and some other %2%", 1, 3.5f);
}

Upvotes: 2

Related Questions