MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Logger< L, ASSERTS > Class Template Reference

See. More...

#include <Logger.h>

+ Inheritance diagram for Logger< L, ASSERTS >:

Public Member Functions

 Logger (const std::string name)
 constructor More...
 
 ~Logger ()=default
 destructor More...
 
template<Log LOGLEVEL, typename... Args>
std::enable_if<!((L< LOGLEVEL)&&(MERCURY_LOGLEVEL
< LOGLEVEL)), void >::type 
operator() (const LL< LOGLEVEL > log, const char *format UNUSED, Args &&...arg UNUSED)
 
template<Log LOGLEVEL, typename... Args>
std::enable_if< L< LOGLEVEL
&&MERCURY_LOGLEVEL< LOGLEVEL,
void >::type operator()(const
LL< LOGLEVEL > log, const char
*format UNUSED, Args &&...arg
UNUSED){}template< Log
LOGLEVEL, typename...Args >
void operator()(const LL
< LOGLEVEL > log, const
std::string &format UNUSED,
Args &&...arg UNUSED){(*this)(log,
format.c_str(), arg...);}template
< typename...Args > typename
std::enable_if<(ASSERTS)&&(sizeof...(Args) >
=0), void >::type assert(bool
assertion, const char *format,
Args &&...arg){assert_always(assertion,
format, arg...);}template
< typename...Args > typename
std::enable_if<!((ASSERTS)&&sizeof...(Args) >
=0), void >::type assert(bool
assertion, const char *format,
Args &&...arg){}template
< typename...Args > void
assert(bool assertion, const
std::string format, Args
&&...arg){assert(assertion,
format.c_str(), arg...);}template
< typename...Args > void
assert_always(bool assertion,
const char *format, Args
&&...arg){if(!assertion){std::stringstream
msgstream;createMessage(msgstream,
format, arg...);loggerOutput->
onFatal(module, msgstream.str());}}template
< typename...Args > void
assert_always(bool assertion,
const std::string format, Args
&&...arg){assert_always(assertion,
format.c_str(), arg...);}template
< typename...Args > void log(const
Log loglevel, const
std::string &format, Args
&&...arg){if(loglevel<=L||loglevel
<=MERCURY_LOGLEVEL){std::stringstream
msgstream;createMessage(msgstream,
format.c_str(), arg...);if(loglevel
<=Log::FATAL){loggerOutput-> 
onFatal (module, msgstream.str())
 
else if (loglevel<=Log::ERROR)
 
else if (loglevel<=Log::WARN)
 
else if (loglevel<=Log::INFO)
 
else if (loglevel<=Log::VERBOSE)
 

Public Attributes

 else
 

Private Member Functions

template<typename Arg1 , typename... Args>
void createMessage (std::stringstream &msg, const char *fmt, Arg1 &&arg, Args &&...args)
 Actual implementation to recursively replace all the '' signs by actual values. More...
 
template<typename Arg1 >
void createMessage (std::stringstream &msg, const char *fmt, Arg1 &&arg)
 Terminating case / argument call. More...
 
void createMessage (std::stringstream &msg, const char *message)
 Terminating case / no argument call. More...
 

Private Attributes

const std::string module
 The module name of this actual logger. More...
 

Detailed Description

template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
class Logger< L, ASSERTS >

See.

How to use the logger for details on how to use the logger.

  • L The log level. Messages of higher level are ignored

Usage: logger(FATAL, "Error in (here) because % < %!\n", var1, var2);

Define custom loggers by: #ifndef HG_LOGLEVEL_CUSTOMMOD #define HG_LOGLEVEL_CUSTOMMOD Log::Debug #endif Logger<HG_LOGLEVEL_CUSTOMMOD> customLogger;

Definition at line 215 of file Logger.h.

Constructor & Destructor Documentation

template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
Logger< L, ASSERTS >::Logger ( const std::string  name)
inlineexplicit

constructor

  • name The name in this module used in output messages.

Definition at line 340 of file Logger.h.

341  : module(name)
342  {
343  }
const std::string module
The module name of this actual logger.
Definition: Logger.h:332
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
Logger< L, ASSERTS >::~Logger ( )
default

destructor

Member Function Documentation

template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
template<typename Arg1 , typename... Args>
void Logger< L, ASSERTS >::createMessage ( std::stringstream &  msg,
const char *  fmt,
Arg1 &&  arg,
Args &&...  args 
)
inlineprivate

Actual implementation to recursively replace all the '' signs by actual values.

Todo:
TW add more format specifications for logger, e.g. how to plot a double with a certain width

Definition at line 510 of file Logger.h.

Referenced by Logger< L, ASSERTS >::operator()().

512  {
513  bool doSkipNext = false;
514  while (*fmt != '%' || doSkipNext)
515  {
516  //Make sure we're not running past the end of our formatting string.
517  if (*fmt == '\0')
518  return;
519 
520  if (*fmt == '\\' && !doSkipNext)
521  { //Escape for the %sign
522  doSkipNext = true;
523  fmt++;
524  }
525  else
526  {
527  msg << *fmt;
528  fmt++;
529  doSkipNext = false;
530  }
531  }
532 
533  fmt++; //Consume the % sign
535  msg << arg;
536  createMessage(msg, fmt, args...);//and recursively call ourselve / the method below.
537  }
void createMessage(std::stringstream &msg, const char *fmt, Arg1 &&arg, Args &&...args)
Actual implementation to recursively replace all the '' signs by actual values.
Definition: Logger.h:510
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
template<typename Arg1 >
void Logger< L, ASSERTS >::createMessage ( std::stringstream &  msg,
const char *  fmt,
Arg1 &&  arg 
)
inlineprivate

Terminating case / argument call.

Definition at line 543 of file Logger.h.

544  {
545  bool doSkipNext = false;
546  while (*fmt != '%' || doSkipNext)
547  {
548  if (*fmt == '\0') // End of string
549  return;
550 
551  if (*fmt == '\\' && !doSkipNext)
552  { //Escape for the %sign and the \sign
553  doSkipNext = true;
554  fmt++;
555  }
556  else
557  { //invoke the replacement
558  msg << *fmt;
559  fmt++;
560  doSkipNext = false;
561  }
562  }
563  fmt++; //Consume the % sign
564  msg << arg << fmt;
565  }
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
void Logger< L, ASSERTS >::createMessage ( std::stringstream &  msg,
const char *  message 
)
inlineprivate

Terminating case / no argument call.

Definition at line 570 of file Logger.h.

571  {
572  msg << message;
573  }
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
else Logger< L, ASSERTS >::if ( loglevel<=Log::ERROR  )
inline

Definition at line 481 of file Logger.h.

References Logger< L, ASSERTS >::module, and LoggerOutput::onError.

482  {
483  loggerOutput->onError(module, msgstream.str());
484  }
LoggerOutput * loggerOutput
Declaration of the output functions. If the output needs to be redirected, please swap the loggerOutp...
Definition: Logger.cc:168
std::function< void(std::string, std::string)> onError
Definition: Logger.h:196
const std::string module
The module name of this actual logger.
Definition: Logger.h:332
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
else Logger< L, ASSERTS >::if ( loglevel<=Log::WARN  )
inline

Definition at line 485 of file Logger.h.

References Logger< L, ASSERTS >::module, and LoggerOutput::onWarn.

486  {
487  loggerOutput->onWarn(module, msgstream.str());
488  }
LoggerOutput * loggerOutput
Declaration of the output functions. If the output needs to be redirected, please swap the loggerOutp...
Definition: Logger.cc:168
const std::string module
The module name of this actual logger.
Definition: Logger.h:332
std::function< void(std::string, std::string)> onWarn
Definition: Logger.h:197
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
else Logger< L, ASSERTS >::if ( loglevel<=Log::INFO  )
inline

Definition at line 489 of file Logger.h.

References Logger< L, ASSERTS >::module, and LoggerOutput::onInfo.

490  {
491  loggerOutput->onInfo(module, msgstream.str());
492  }
LoggerOutput * loggerOutput
Declaration of the output functions. If the output needs to be redirected, please swap the loggerOutp...
Definition: Logger.cc:168
const std::string module
The module name of this actual logger.
Definition: Logger.h:332
std::function< void(std::string, std::string)> onInfo
Definition: Logger.h:198
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
else Logger< L, ASSERTS >::if ( loglevel<=Log::VERBOSE  )
inline

Definition at line 493 of file Logger.h.

References Logger< L, ASSERTS >::module, and LoggerOutput::onVerbose.

494  {
495  loggerOutput->onVerbose(module, msgstream.str());
496  }
LoggerOutput * loggerOutput
Declaration of the output functions. If the output needs to be redirected, please swap the loggerOutp...
Definition: Logger.cc:168
const std::string module
The module name of this actual logger.
Definition: Logger.h:332
std::function< void(std::string, std::string)> onVerbose
Definition: Logger.h:199
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
template<Log LOGLEVEL, typename... Args>
std::enable_if<L < LOGLEVEL&& MERCURY_LOGLEVEL<LOGLEVEL, void>::type operator()(const LL<LOGLEVEL> log, const char* format UNUSED, Args&& ... arg UNUSED) { } template<Log LOGLEVEL, typename... Args> void operator()(const LL<LOGLEVEL> log, const std::string& format UNUSED, Args&& ... arg UNUSED) { (*this)(log, format.c_str(), arg...); } template<typename... Args> typename std::enable_if<(ASSERTS) && (sizeof...(Args) >= 0), void>::type assert(bool assertion, const char* format, Args&& ... arg) { assert_always(assertion, format, arg...); } template<typename... Args> typename std::enable_if<!((ASSERTS) && sizeof...(Args) >= 0), void>::type assert(bool assertion, const char* format, Args&& ... arg) { } template<typename... Args> void assert(bool assertion, const std::string format, Args&& ... arg) { assert(assertion, format.c_str(), arg...); } template<typename... Args> void assert_always(bool assertion, const char* format, Args&& ... arg) { if (!assertion) { std::stringstream msgstream; createMessage(msgstream, format, arg...); loggerOutput->onFatal(module, msgstream.str()); } } template<typename... Args> void assert_always(bool assertion, const std::string format, Args&& ... arg) { assert_always(assertion, format.c_str(), arg...); } template<typename... Args> void log(const Log loglevel, const std::string& format, Args&& ... arg) { if (loglevel <= L || loglevel <= MERCURY_LOGLEVEL) { std::stringstream msgstream; createMessage(msgstream, format.c_str(), arg...); if (loglevel <= Log::FATAL) { loggerOutput-> Logger< L, ASSERTS >::onFatal ( module  ,
msgstream.  str() 
)
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
template<Log LOGLEVEL, typename... Args>
std::enable_if<!((L < LOGLEVEL) && (MERCURY_LOGLEVEL < LOGLEVEL)), void>::type Logger< L, ASSERTS >::operator() ( const LL< LOGLEVEL >  log,
const char *format  UNUSED,
Args &&...arg  UNUSED 
)
inline

Definition at line 366 of file Logger.h.

References Logger< L, ASSERTS >::createMessage(), ERROR, FATAL, INFO, Logger< L, ASSERTS >::module, LoggerOutput::onDebug, LoggerOutput::onError, LoggerOutput::onFatal, LoggerOutput::onInfo, LoggerOutput::onVerbose, LoggerOutput::onWarn, VERBOSE, and WARN.

367  {
368  std::stringstream msgstream;
369  createMessage(msgstream, format, arg...);
370  if (LOGLEVEL <= Log::FATAL)
371  {
372  loggerOutput->onFatal(module, msgstream.str());
373  }
374  else if (LOGLEVEL <= Log::ERROR)
375  {
376  loggerOutput->onError(module, msgstream.str());
377  }
378  else if (LOGLEVEL <= Log::WARN)
379  {
380  loggerOutput->onWarn(module, msgstream.str());
381  }
382  else if (LOGLEVEL <= Log::INFO)
383  {
384  loggerOutput->onInfo(module, msgstream.str());
385  }
386  else if (LOGLEVEL <= Log::VERBOSE)
387  {
388  loggerOutput->onVerbose(module, msgstream.str());
389  }
390  else
391  {
392  loggerOutput->onDebug(module, msgstream.str());
393  }
394  }
LoggerOutput * loggerOutput
Declaration of the output functions. If the output needs to be redirected, please swap the loggerOutp...
Definition: Logger.cc:168
std::function< void(std::string, std::string)> onError
Definition: Logger.h:196
std::function< void(std::string, std::string)> onDebug
Definition: Logger.h:200
std::function< void(std::string, std::string)> onFatal
Definition: Logger.h:195
const std::string module
The module name of this actual logger.
Definition: Logger.h:332
std::function< void(std::string, std::string)> onVerbose
Definition: Logger.h:199
std::function< void(std::string, std::string)> onWarn
Definition: Logger.h:197
std::function< void(std::string, std::string)> onInfo
Definition: Logger.h:198
void createMessage(std::stringstream &msg, const char *fmt, Arg1 &&arg, Args &&...args)
Actual implementation to recursively replace all the '' signs by actual values.
Definition: Logger.h:510

Member Data Documentation

template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
Logger< L, ASSERTS >::else
Initial value:
{
loggerOutput->onDebug(module, msgstream.str())

Definition at line 498 of file Logger.h.

template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
const std::string Logger< L, ASSERTS >::module
private

The module name of this actual logger.

Definition at line 332 of file Logger.h.

Referenced by Logger< L, ASSERTS >::if(), and Logger< L, ASSERTS >::operator()().


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