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

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

#include <Logger.h>

Public Member Functions

 Logger (const std::string name)
 constructor More...
 
 ~Logger ()
 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, Args &&...arg)
 
template<Log LOGLEVEL, typename... Args>
std::enable_if< L< LOGLEVEL||MERCURY_LOGLEVEL
< LOGLEVEL, void >::type
operator()(const LL< LOGLEVEL >
log UNUSED, const std::string
&format UNUSED, Args &&...arg
UNUSED){}template< Log
LOGLEVEL, typename...Args >
typename std::enable_if< L
< LOGLEVEL||MERCURY_LOGLEVEL
< LOGLEVEL, void >::type
operator()(const LL< LOGLEVEL >
log UNUSED, const char
*format UNUSED, Args &&...arg
UNUSED){}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){loggerOutput->
onError(module, msgstream.str());}else
if(loglevel<=Log::WARN){loggerOutput-> 
onWarn (module, msgstream.str())
 
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 204 of file Logger.h.

Constructor & Destructor Documentation

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

constructor

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

Definition at line 329 of file Logger.h.

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

destructor

Definition at line 336 of file Logger.h.

336 {}

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.

Definition at line 507 of file Logger.h.

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

509  {
510  bool doSkipNext = false;
511  while (*fmt != '%' || doSkipNext)
512  {
513  //Make sure we're not running past the end of our formatting string.
514  if (*fmt == '\0')
515  return;
516 
517  if (*fmt == '\\'&& !doSkipNext)
518  { //Escape for the %sign
519  doSkipNext = true;
520  fmt++;
521  }
522  else
523  {
524  msg << *fmt;
525  fmt++;
526  doSkipNext = false;
527  }
528  }
529 
530  fmt++; //Consume the % sign
531  msg << arg;
532  createMessage(msg, fmt, args...);//and recursively call ourselve / the method below.
533  }
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:507
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 539 of file Logger.h.

540  {
541  bool doSkipNext = false;
542  while (*fmt != '%' || doSkipNext)
543  {
544  if (*fmt == '\0') // End of string
545  return;
546 
547  if (*fmt == '\\' && !doSkipNext)
548  { //Escape for the %sign and the \sign
549  doSkipNext = true;
550  fmt++;
551  }
552  else
553  { //invoke the replacement
554  msg << *fmt;
555  fmt++;
556  doSkipNext = false;
557  }
558  }
559  fmt++; //Consume the % sign
560  msg << arg << fmt;
561  }
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 566 of file Logger.h.

567  {
568  msg << message;
569  }
template<Log L = Log::DEFAULT, bool ASSERTS = MERCURY_ASSERTS>
else Logger< L, ASSERTS >::if ( loglevel<=Log::INFO  )
inline

Definition at line 487 of file Logger.h.

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

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

Definition at line 491 of file Logger.h.

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

492  {
493  loggerOutput->onVerbose(module, msgstream.str());
494  }
LoggerOutput * loggerOutput
Declaration of the output functions. If the output needs to be redirected, please swap the loggerOutp...
Definition: Logger.cc:143
const std::string module
The module name of this actual logger.
Definition: Logger.h:321
std::function< void(std::string, std::string)> onVerbose
Definition: Logger.h:189
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 UNUSED, const std::string& format UNUSED, Args&&... arg UNUSED) { } template<Log LOGLEVEL, typename... Args> typename std::enable_if<L < LOGLEVEL || MERCURY_LOGLEVEL < LOGLEVEL, void>::type operator()(const LL<LOGLEVEL> log UNUSED, const char * format UNUSED, Args&&... arg UNUSED) { } 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) { loggerOutput->onError(module, msgstream.str()); } else if (loglevel <= Log::WARN) { loggerOutput-> Logger< L, ASSERTS >::onWarn ( module  ,
msgstream.  str() 
)
Todo:
The two functions below are MercuryDPM loggers, however the commented functions underneath are the hpGEM ones, I can't seem to get those working
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,
Args &&...  arg 
)
inline

Definition at line 353 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.

354  {
355  std::stringstream msgstream;
356  createMessage(msgstream, format, arg...);
357  if (LOGLEVEL <= Log::FATAL)
358  {
359  loggerOutput->onFatal(module, msgstream.str());
360  }
361  else if (LOGLEVEL <= Log::ERROR)
362  {
363  loggerOutput->onError(module, msgstream.str());
364  }
365  else if (LOGLEVEL <= Log::WARN)
366  {
367  loggerOutput->onWarn(module, msgstream.str());
368  }
369  else if (LOGLEVEL <= Log::INFO)
370  {
371  loggerOutput->onInfo(module, msgstream.str());
372  }
373  else if (LOGLEVEL <= Log::VERBOSE)
374  {
375  loggerOutput->onVerbose(module, msgstream.str());
376  }
377  else
378  {
379  loggerOutput->onDebug(module, msgstream.str());
380  }
381  }
LoggerOutput * loggerOutput
Declaration of the output functions. If the output needs to be redirected, please swap the loggerOutp...
Definition: Logger.cc:143
std::function< void(std::string, std::string)> onError
Definition: Logger.h:186
std::function< void(std::string, std::string)> onDebug
Definition: Logger.h:190
std::function< void(std::string, std::string)> onFatal
Definition: Logger.h:185
const std::string module
The module name of this actual logger.
Definition: Logger.h:321
std::function< void(std::string, std::string)> onVerbose
Definition: Logger.h:189
std::function< void(std::string, std::string)> onWarn
Definition: Logger.h:187
std::function< void(std::string, std::string)> onInfo
Definition: Logger.h:188
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:507

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 496 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 321 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: