libmoost
|
handles verbose gracefully. It behaves like ostringstream and prints the verbose at the end of the scope. Example. More...
#include <scoped_verbose.hpp>
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_verbose & | operator<< (const T &str) |
template<typename T > | |
void | addTrivial (const T &str) |
template<typename T > | |
void | add (const T &str, eVerboseLevel lvl) |
scoped_verbose & | flush () |
scoped_verbose & | operator<< (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 |
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;
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
str | the name of the expensive op |
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
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 }
Definition at line 159 of file scoped_verbose.hpp.
Definition at line 163 of file scoped_verbose.hpp.
moost::scoped_verbose::scoped_verbose | ( | const std::string & | header, |
eVerboseLevel | vl = VL_WARNING_PRIORITY , |
||
std::ostream & | out = std::cout |
||
) | [inline] |
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] |
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] |
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] |
moost::scoped_verbose::~scoped_verbose | ( | ) | [inline] |
The destructor. It will print the buffer with a timestamp.
Definition at line 233 of file scoped_verbose.hpp.
void moost::scoped_verbose::add | ( | const T & | str, |
eVerboseLevel | lvl | ||
) | [inline] |
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
str | the name of the expensive op |
Definition at line 466 of file scoped_verbose.hpp.
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.
str | The string to be added |
addErroHeader | if true it will add "ERROR: " before the message |
addNewLine | if true it will set a new line before adding the message to the buffer |
Definition at line 338 of file scoped_verbose.hpp.
void moost::scoped_verbose::addToStream | ( | const T & | str, |
eVerboseLevel | toLevel | ||
) | [inline, protected] |
void moost::scoped_verbose::addTrivial | ( | const T & | str | ) | [inline] |
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.
str | The string to be added |
addWarningHeader | if true it will add "WARNING: " before the message |
addNewLine | if true it will set a new line before adding the message to the buffer |
Definition at line 356 of file scoped_verbose.hpp.
void moost::scoped_verbose::changeVerboseLevel | ( | eVerboseLevel | vl = VL_EVERYTHING | ) | [inline] |
Change the level of verbosity.
vl | the new verbosity level. |
Definition at line 481 of file scoped_verbose.hpp.
scoped_verbose& moost::scoped_verbose::flush | ( | ) | [inline] |
eVerboseLevel moost::scoped_verbose::getVerboseLevel | ( | ) | const [inline] |
Returns the current verbose level
Definition at line 487 of file scoped_verbose.hpp.
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.
the | string to be stored in the stream buffer. |
Definition at line 375 of file scoped_verbose.hpp.
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] |
static std::string moost::scoped_verbose::stringize | ( | eVerboseLevel | lvl | ) | [inline, static] |
Return a string with a description of the passed verbosity level
lvl | the 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.
std::ostringstream moost::scoped_verbose::m_buf [protected] |
Definition at line 518 of file scoped_verbose.hpp.
eVerboseLevel moost::scoped_verbose::m_currLevel [protected] |
Definition at line 529 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.
std::string moost::scoped_verbose::m_subTimingsIndentation [protected] |
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.
std::string moost::scoped_verbose::m_totTimingsHeader [protected] |
Definition at line 524 of file scoped_verbose.hpp.
eVerboseLevel moost::scoped_verbose::m_vl [protected] |
Definition at line 515 of file scoped_verbose.hpp.