libmoost
moost::scoped_verbose Class Reference

handles verbose gracefully. It behaves like ostringstream and prints the verbose at the end of the scope. Example. More...

#include <scoped_verbose.hpp>

List of all members.

Classes

class  recurrurring_bookmark
class  scoped_bookmark

Public Types

enum  eVerboseLevel {
  VL_HIGH_PRIORITY, VL_WARNING_PRIORITY, VL_LOW_PRIORITY, VL_EVERYTHING,
  VL_MAX = VL_EVERYTHING
}

Public Member Functions

 scoped_verbose (const std::string &header, eVerboseLevel vl=VL_WARNING_PRIORITY, std::ostream &out=std::cout)
 scoped_verbose (const std::string &header, moost::timer::scoped_time &sc, eVerboseLevel vl=VL_WARNING_PRIORITY, std::ostream &out=std::cout)
 scoped_verbose (const std::string &header, moost::multi_timer::scoped_time &sc, eVerboseLevel vl=VL_WARNING_PRIORITY, std::ostream &out=std::cout)
 scoped_verbose (const std::string &header, const boost::posix_time::ptime &time, eVerboseLevel vl=VL_WARNING_PRIORITY, std::ostream &out=std::cout)
 ~scoped_verbose ()
template<typename T >
void addError (const T &str, bool addErrorHeader=false, bool addNewLine=true)
template<typename T >
void addWarning (const T &str, bool addWarningHeader=false, bool addNewLine=true)
template<typename T >
scoped_verboseoperator<< (const T &str)
template<typename T >
void addTrivial (const T &str)
template<typename T >
void add (const T &str, eVerboseLevel lvl)
scoped_verboseflush ()
scoped_verboseoperator<< (scoped_verbose &(*modifier)(scoped_verbose &))
void setHeader (const std::string &header)
std::string stringize ()
void addBookmark (const std::string &str)
 Add a time bookmark. If verbose is set to everything it will take the bookmark and display it in order making a diff from the previous one (with the first one using the time in the constructor). This allows an easy way of timing differents parts of the code. i.e.
void changeVerboseLevel (eVerboseLevel vl=VL_EVERYTHING)
eVerboseLevel getVerboseLevel () const
void setTimingsHeader (const std::string &totTimingsHeader=" Overall time: ", const std::string &subTimingsIndentation=" -> ")

Static Public Member Functions

static std::string stringize (eVerboseLevel lvl)

Protected Member Functions

template<typename T >
void addToStream (const T &str, eVerboseLevel toLevel)

Protected Attributes

std::string m_header
eVerboseLevel m_vl
std::ostream & m_out
std::ostringstream m_buf
std::vector< std::pair
< eVerboseLevel, std::string > > 
m_entries
boost::posix_time::ptime m_startTime
std::string m_totTimingsHeader
std::string m_subTimingsIndentation
std::vector< std::pair
< boost::posix_time::ptime,
std::string > > 
m_timeBookmarks
eVerboseLevel m_currLevel

Detailed Description

handles verbose gracefully. It behaves like ostringstream and prints the verbose at the end of the scope. Example.

void myFunction()
{
  moost::scoped_verbose v("myFunction");
  v << "Something.."; // that's optional
  // ..
}

here it will print to cout:

[2009-Jun-18 16:38:09|myFunction] Something..

It is also possible to wrap the scoped verbose inside a class that will always output at the same level. For instance if you are in a function that has only trivial verbose instead of using .addTrivial all the time, just do

void trivialFunc(moost::scoped_verbose& v)
{
   moost::trivial_scoped_verbose tv(v);
   tv << "the answer to the question is " << 42;
}

Same is true for error_scoped_verbose and warning_scoped_verbose;

Note:
For efficiency reasons if you try to add something to a lower priority than the current level, the buffer will not be updated, but if you start low then switch the priority to high, only the higher will be output, i.e.
void foo()
{
   moost::scoped_verbose v("myFunction", moost::scoped_verbose::VL_WARNING_PRIORITY); // starting medium high verbose
   v << "<hi>"; // this will NOT be recorded since default is VL_LOW_PRIORITY
   v.addWarning("<a warning>"); // this is going to be recorded
   v.changeVerboseLevel(moost::scoped_verbose::VL_HIGH_PRIORITY); // now "<a warning>" will not be printed
   v.addError("<error!>"); // this is the only one at this point that will be output
   v.addWarning("<another warning>"); // not recorded as the current level is VL_HIGH_PRIORITY

   v.changeVerboseLevel(moost::scoped_verbose::VL_LOW_PRIORITY); // we changed our mind again, but what's lost is lost!
} // at the end of the scope it will print: "<a warning><error!>"

If verbose is set to everything it will take the bookmark and display it in order making a diff from the previous one (with the first one using the time in the constructor). This allows an easy way of timing differents parts of the code. i.e.

void myFunction()
{
moost::scoped_verbose v("myFunction");
// do something expensive
v.addBookmark("expensive_op1");
// do something else expensive
v.addBookmark("expensive_op2");
// finishing up..
}

will result in:

[2009-Jun-18 16:38:09 | myFunction]
expensive_op1: 12ms
expensive_op2: 53ms
total: 68ms
Parameters:
strthe name of the expensive op
Note:
this will work only and only if verbose is set to VL_EVERYTHING
See also:
~scoped_verbose It is possible to use the timer for only a specific are of the code by using the scoped_bookmark, i.e.
moost::scoped_verbose v("foo", moost::scoped_verbose::VL_EVERYTHING); // if not it will not record bookmarks
v << "<hi>";
{ // start a scope
   moost::scoped_verbose::scoped_bookmark sb(sv, "inTheScope"); // timer of the bookmark starts here
   // do stuff
} // end of scope, the timer is stopped and "inTheScope" is assigned
If one wants to sum up the times of a recurring event it's possible to do so by using a recurrurring_bookmark:
moost::scoped_verbose v("foo", moost::scoped_verbose::VL_EVERYTHING); // if not it will not record bookmarks
for (i = 0; i < 10; ++i)
{
   // something here we do not want to measure
   { // start scope for the thing we want to measure and it's repeated
      moost::scoped_verbose::recurring_bookmark sb(sv, "recurring_event"); // timer of the bookmark starts here
      // do something
   }// end of scope, the time measured in the scope is added
}
The recurring bookmark will take 10 measures for the operation in the scope and average it out before printing out.
Note:
Important: scoped_verbose is not thread safe.

Definition at line 159 of file scoped_verbose.hpp.


Member Enumeration Documentation

Enumerator:
VL_HIGH_PRIORITY 
VL_WARNING_PRIORITY 
VL_LOW_PRIORITY 
VL_EVERYTHING 
VL_MAX 

Definition at line 163 of file scoped_verbose.hpp.


Constructor & Destructor Documentation

moost::scoped_verbose::scoped_verbose ( const std::string &  header,
eVerboseLevel  vl = VL_WARNING_PRIORITY,
std::ostream &  out = std::cout 
) [inline]

Definition at line 213 of file scoped_verbose.hpp.

Here is the call graph for this function:

moost::scoped_verbose::scoped_verbose ( const std::string &  header,
moost::timer::scoped_time sc,
eVerboseLevel  vl = VL_WARNING_PRIORITY,
std::ostream &  out = std::cout 
) [inline]

Definition at line 218 of file scoped_verbose.hpp.

Here is the call graph for this function:

moost::scoped_verbose::scoped_verbose ( const std::string &  header,
moost::multi_timer::scoped_time sc,
eVerboseLevel  vl = VL_WARNING_PRIORITY,
std::ostream &  out = std::cout 
) [inline]

Definition at line 222 of file scoped_verbose.hpp.

Here is the call graph for this function:

moost::scoped_verbose::scoped_verbose ( const std::string &  header,
const boost::posix_time::ptime &  time,
eVerboseLevel  vl = VL_WARNING_PRIORITY,
std::ostream &  out = std::cout 
) [inline]

Definition at line 226 of file scoped_verbose.hpp.

Here is the call graph for this function:

The destructor. It will print the buffer with a timestamp.

Definition at line 233 of file scoped_verbose.hpp.

Here is the call graph for this function:


Member Function Documentation

template<typename T >
void moost::scoped_verbose::add ( const T &  str,
eVerboseLevel  lvl 
) [inline]

Definition at line 390 of file scoped_verbose.hpp.

Here is the call graph for this function:

void moost::scoped_verbose::addBookmark ( const std::string &  str) [inline]

Add a time bookmark. If verbose is set to everything it will take the bookmark and display it in order making a diff from the previous one (with the first one using the time in the constructor). This allows an easy way of timing differents parts of the code. i.e.

   void myFunction()
   {
      moost::scoped_verbose v("myFunction");
      // do something expensive
      v.addBookmark("expensive_op1");
      // do something else expensive
      v.addBookmark("expensive_op2");
      // finishing up..
   }

will result in:

[2009-Jun-18 16:38:09 | myFunction]
   expensive_op1: 12ms
   expensive_op2: 53ms
   total: 68ms
Parameters:
strthe name of the expensive op
Note:
this will work only and only if verbose is set to VL_EVERYTHING
See also:
~scoped_verbose

Definition at line 466 of file scoped_verbose.hpp.

Here is the caller graph for this function:

template<typename T >
void moost::scoped_verbose::addError ( const T &  str,
bool  addErrorHeader = false,
bool  addNewLine = true 
) [inline]

Add an error message to the string buffer. This will be printed no matter what.

Parameters:
strThe string to be added
addErroHeaderif true it will add "ERROR: " before the message
addNewLineif true it will set a new line before adding the message to the buffer

Definition at line 338 of file scoped_verbose.hpp.

Here is the call graph for this function:

template<typename T >
void moost::scoped_verbose::addToStream ( const T &  str,
eVerboseLevel  toLevel 
) [inline, protected]

Definition at line 499 of file scoped_verbose.hpp.

Here is the caller graph for this function:

template<typename T >
void moost::scoped_verbose::addTrivial ( const T &  str) [inline]

Definition at line 383 of file scoped_verbose.hpp.

Here is the call graph for this function:

template<typename T >
void moost::scoped_verbose::addWarning ( const T &  str,
bool  addWarningHeader = false,
bool  addNewLine = true 
) [inline]

Add a warning message to the string buffer. This will be printed if verbose level is set to VL_WARNING_PRIORITY or higher.

Parameters:
strThe string to be added
addWarningHeaderif true it will add "WARNING: " before the message
addNewLineif true it will set a new line before adding the message to the buffer

Definition at line 356 of file scoped_verbose.hpp.

Here is the call graph for this function:

Change the level of verbosity.

Parameters:
vlthe new verbosity level.

Definition at line 481 of file scoped_verbose.hpp.

Definition at line 396 of file scoped_verbose.hpp.

Here is the caller graph for this function:

Returns the current verbose level

Definition at line 487 of file scoped_verbose.hpp.

template<typename T >
scoped_verbose& moost::scoped_verbose::operator<< ( const T &  str) [inline]

The standard way of adding a string to scoped_verbose is via the << operator. Just use it the same way as cout.

Parameters:
thestring to be stored in the stream buffer.

Definition at line 375 of file scoped_verbose.hpp.

Here is the call graph for this function:

scoped_verbose& moost::scoped_verbose::operator<< ( scoped_verbose &(*)(scoped_verbose &)  modifier) [inline]

Definition at line 399 of file scoped_verbose.hpp.

void moost::scoped_verbose::setHeader ( const std::string &  header) [inline]

Set the header that will be printed when going out of scope. Will override the one set on construction.

Definition at line 406 of file scoped_verbose.hpp.

void moost::scoped_verbose::setTimingsHeader ( const std::string &  totTimingsHeader = "  Overall time: ",
const std::string &  subTimingsIndentation = "    -> " 
) [inline]

Definition at line 490 of file scoped_verbose.hpp.

Here is the caller graph for this function:

static std::string moost::scoped_verbose::stringize ( eVerboseLevel  lvl) [inline, static]

Return a string with a description of the passed verbosity level

Parameters:
lvlthe passed verbosity level

Definition at line 413 of file scoped_verbose.hpp.

std::string moost::scoped_verbose::stringize ( ) [inline]

Return a string with the description of the current verbosity level.

Definition at line 432 of file scoped_verbose.hpp.


Member Data Documentation

std::ostringstream moost::scoped_verbose::m_buf [protected]

Definition at line 518 of file scoped_verbose.hpp.

std::vector< std::pair<eVerboseLevel, std::string> > moost::scoped_verbose::m_entries [protected]

Definition at line 520 of file scoped_verbose.hpp.

std::string moost::scoped_verbose::m_header [protected]

Definition at line 513 of file scoped_verbose.hpp.

std::ostream& moost::scoped_verbose::m_out [protected]

Definition at line 516 of file scoped_verbose.hpp.

boost::posix_time::ptime moost::scoped_verbose::m_startTime [protected]

Definition at line 522 of file scoped_verbose.hpp.

Definition at line 525 of file scoped_verbose.hpp.

std::vector< std::pair<boost::posix_time::ptime, std::string> > moost::scoped_verbose::m_timeBookmarks [protected]

Definition at line 527 of file scoped_verbose.hpp.

Definition at line 524 of file scoped_verbose.hpp.

Definition at line 515 of file scoped_verbose.hpp.


The documentation for this class was generated from the following file: