Boost logo

Boost :

From: Jens Seidel (jensseidel_at_[hidden])
Date: 2008-03-03 07:37:05


On Sun, Mar 02, 2008 at 04:17:46AM +0200, John Torjo wrote:
> Hi Roland,
> > Surprisingly I was not able to find on the net
> > code that would give me a smart way to indent
> > a stream. So I wrote a little filter based on
> > the boost iostreams library.
> >
> >
> This looks pretty cool. One question : do you intend to use this for
> more than logging?
>
> I'm asking because Boost Logging v3 will certainly include such a feature.

I used with a Boost.Logging v1 wrapper:

namespace {
static int logIndentLevel = 0;

void prepend_spaces(const boost::logging::logging_types::string
&/*log_name*/, boost::logging::logging_types::string & msg) {
  msg = " " + msg;
}
}

void logging::addLogIndention()
{
  std::ostringstream str;
  ++logIndentLevel;
  str << "indentation" << logIndentLevel;
  using namespace boost::logging;
  // flush all old messages with previous modifier
  flush_log_cache();
  add_modifier("*", &prepend_spaces, str.str(), DEFAULT_INDEX+5);
}

void logging::restoreLogIndention()
{
  std::ostringstream str;
  str << "indentation" << logIndentLevel;
  --logIndentLevel;
  ASSERT(logIndentLevel>=0);
  using namespace boost::logging;
  // the modifier will be changed soon, sync!
  flush_log_cache();
  del_modifier("*", str.str());
}

Both addLogIndention() and restoreLogIndention() are also called in the
constructor/destructor of a helper class to ensure a proper reset.

Very simple ...

Jens


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk