Logger< L, ASSERTS > Class Template Reference

the Logger class is the main class of the logger implementation. It holds all the functions which invoke certain methods to create messages based on input parameter deductions. More...

#include <Logger.h>

Public Member Functions

 Logger (const std::string name)
 constructor More...
 
 ~Logger ()=default
 destructor More...
 
template<Log LOGLEVEL, typename ... Args>
std::enable_if<!((L< LOGLEVEL) &&(MERCURYDPM_LOGLEVEL< LOGLEVEL)), void >::type operator() (const LL< LOGLEVEL > log, const char *format UNUSED, Args &&... arg UNUSED)
 Log implementation of this function. More...
 
template<Log LOGLEVEL, typename... Args>
std::enable_if< L< LOGLEVEL &&MERCURYDPM_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_debug(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_debug(bool assertion, const char *format, Args &&... arg) { } template< typename... Args > void assert_debug(bool assertion, const std::string format, Args &&... arg) { assert_debug(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(), doFlush_);} } template< typename... Args > void assert_always(bool assertion, const std::string format, Args &&... arg) { assert_always(assertion, format.c_str(), arg...);} template< typename... Args > MERCURYDPM_DEPRECATED void log(const Log loglevel, const std::string &format, Args &&... arg) { if(loglevel<=L||loglevel<=MERCURYDPM_LOGLEVEL) { std::stringstream msgstream;createMessage(msgstream, format.c_str(), arg...);if(loglevel<=Log::FATAL) { loggerOutput-> onFatal (module, msgstream.str(), doFlush_)
 Empty body function utilized to suppress logger messages above a certain user defined loglevel L. More...
 
else if (loglevel<=Log::ERROR)
 
else if (loglevel<=Log::WARN)
 
else if (loglevel<=Log::INFO)
 
else if (loglevel<=Log::VERBOSE)
 

Public Attributes

 else
 
 doFlush_ = Flusher::FLUSH
 

Private Member Functions

template<typename Arg1 , typename... Args>
void createMessage (std::stringstream &msg, const char *fmt, Arg1 &&arg, Args &&... args)
 Edits the message to a certain format and writes it to a stringstream by recursively replacing all % characters with the arguments values. More...
 
template<typename... Args>
void createMessage (std::stringstream &msg, const char *fmt, Flusher arg, Args &&... args)
 Overloaded version of createMessage to catch arguments of Flusher and suppress input flushing via std::endl. If there is an argument which should be catched from the logger, overloading the function is the way to go. More...
 
template<typename Arg1 >
void createMessage (std::stringstream &msg, const char *fmt, Arg1 &&arg)
 Terminating case / Argument call. Overloaded function for a logger message with only one argument or where only one argument is left. More...
 
void createMessage (std::stringstream &msg, const char *message)
 Terminating case / no argument call Overloaded function for a logger message without arguments. More...
 

Private Attributes

const std::string module
 The module name of this actual logger. More...
 
Flusher doFlush_ = Flusher::FLUSH
 Can prevent the logger from flushing the buffer via std::endl. doFlush_ is set automatically based on build and loglevel settings. More...
 

Detailed Description

template<Log L, bool ASSERTS>
class Logger< L, ASSERTS >

the Logger class is the main class of the logger implementation. It holds all the functions which invoke certain methods to create messages based on input parameter deductions.

Template Parameters
LThe log level defined in cMake configuration. Messages of higher level than L are ignored.

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

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

Constructor & Destructor Documentation

◆ Logger()

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

constructor

Parameters
[in]nameThe name in this module used in output messages.
305  : module(name)
306  {
307  }
const std::string module
The module name of this actual logger.
Definition: Logger.h:290
string name
Definition: plotDoE.py:33

◆ ~Logger()

template<Log L, bool ASSERTS>
Logger< L, ASSERTS >::~Logger ( )
default

destructor

Member Function Documentation

◆ createMessage() [1/4]

template<Log L, bool ASSERTS>
template<typename Arg1 >
void Logger< L, ASSERTS >::createMessage ( std::stringstream &  msg,
const char fmt,
Arg1 &&  arg 
)
inlineprivate

Terminating case / Argument call. Overloaded function for a logger message with only one argument or where only one argument is left.

Parameters
[in]msgstringstream which represents the output message.
[in]fmtchar array of the yet unformatted message.
[in]argargument to replace the next % character.
632  {
633  bool doSkipNext = false;
634  while (*fmt != '%' || doSkipNext)
635  {
636  if (*fmt == '\0') // End of string
637  return;
638 
639  if (*fmt == '\\' && !doSkipNext)
640  { //Escape for the % character and the \ character
641  doSkipNext = true;
642  fmt++;
643  }
644  else
645  { //invoke the replacement
646  msg << *fmt;
647  fmt++;
648  doSkipNext = false;
649  }
650  }
651  fmt++; //Consume the % character
652  int precision = 0;
653  int width = 0;
654  // if precision and width or only precision is defined
655  if (isdigit(*fmt))
656  {
657  precision = std::atoi(fmt);
658  while (isdigit(*fmt))
659  {
660  fmt++;
661  }
662  if (std::ispunct(*fmt))
663  {
664  fmt++;
665  if (std::isdigit(*fmt))
666  {
667  width = std::atoi(fmt);
668  while (isdigit(*fmt))
669  {
670  fmt++;
671  }
672  }
673  // else the char is a real full stop so set the pointer back to full stop.
674  else
675  {
676  fmt--;
677  }
678  }
679  }
680  // if only a width and no precision defined
681  else if (std::ispunct(*fmt))
682  {
683  fmt++;
684  if (std::isdigit(*fmt))
685  {
686  width = std::atoi(fmt);
687  while (isdigit(*fmt))
688  {
689  fmt++;
690  }
691  }
692  // else the char is a real full stop so set the pointer back to full stop.
693  else
694  {
695  fmt--;
696  }
697  }
698  if (width != 0 && precision != 0)
699  {
700  msg << std::setprecision(precision) << std::left << std::setw(width) << arg << fmt;
701  }
702  else if (precision != 0)
703  {
704  msg << std::setprecision(precision) << arg << fmt;
705  }
706  else if (width != 0)
707  {
708  msg << std::left << std::setw(width) << arg << fmt;
709  }
710  else
711  {
712  msg << arg << fmt;
713  }
714  }

◆ createMessage() [2/4]

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

Edits the message to a certain format and writes it to a stringstream by recursively replacing all % characters with the arguments values.

The creation of messages is divided into three different overloaded functions. the function createMessage is recursively called and each of the functions below is called for a certain case dependent on the amount and type of parameters.

Parameters
[in]msgstringstream which represents the output message.
[in]fmtchar array of the yet unformatted message.
[in]argargument to replace the next % character.
[in]argsparameter pack of the remaining arguments.
505  {
506  bool doSkipNext = false;
507  while (*fmt != '%' || doSkipNext)
508  {
509  //Make sure we're not running past the end of our formatting string.
510  if (*fmt == '\0')
511  return;
512 
513  if (*fmt == '\\' && !doSkipNext)
514  { //Escape for the % character
515  doSkipNext = true;
516  fmt++;
517  }
518  else
519  {
520  msg << *fmt;
521  fmt++;
522  doSkipNext = false;
523  }
524  }
525  fmt++; //Consume the % character
526  int precision = 0;
527  int width = 0;
528  // if precision and width or only precision is defined
529  if (isdigit(*fmt))
530  {
531  precision = std::atoi(fmt);
532  while (isdigit(*fmt))
533  {
534  fmt++;
535  }
536  if (std::ispunct(*fmt))
537  {
538  fmt++;
539  if (std::isdigit(*fmt))
540  {
541  width = std::atoi(fmt);
542  while (isdigit(*fmt))
543  {
544  fmt++;
545  }
546  }
547  // else the char is a real full stop so set the pointer back to full stop.
548  else
549  {
550  fmt--;
551  }
552  }
553  }
554  // if only a width and no precision defined
555  else if (std::ispunct(*fmt))
556  {
557  fmt++;
558  if (std::isdigit(*fmt))
559  {
560  width = std::atoi(fmt);
561  while (isdigit(*fmt))
562  {
563  fmt++;
564  }
565  }
566  // else the char is a real full stop so set the pointer back to full stop.
567  else
568  {
569  fmt--;
570  }
571  }
572  if (width != 0 && precision != 0)
573  {
574  msg << std::setprecision(precision) << std::left << std::setw(width) << arg;
575  }
576  else if (precision != 0)
577  {
578  msg << std::setprecision(precision) << arg;
579  }
580  else if (width != 0)
581  {
582  msg << std::left << std::setw(width) << arg;
583  }
584  else
585  {
586  msg << arg;
587  } //include args somehow..
588  createMessage(msg, fmt, args...);//and recursively call ourselve / the method below.
589  }
void createMessage(std::stringstream &msg, const char *fmt, Arg1 &&arg, Args &&... args)
Edits the message to a certain format and writes it to a stringstream by recursively replacing all % ...
Definition: Logger.h:503
args
Definition: compute_granudrum_aor.py:143

References compute_granudrum_aor::args.

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

◆ createMessage() [3/4]

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

Overloaded version of createMessage to catch arguments of Flusher and suppress input flushing via std::endl. If there is an argument which should be catched from the logger, overloading the function is the way to go.

Parameters
[in]msgstringstream which represents the output message.
[in]fmtchar array of the yet unformatted message.
[in]argargument of type Flusher which will be skipped and does not replace the next % character.
[in]argsparameter pack of the remaining parameters.
607  {
608  // only suppress flushing if Mercury is not in CMAKE_BUILD_TYPE "Debug" and if the user defined loglevel from
609  // cMake is below VERBOSE/DEBUG (<=5)
610 #ifndef MERCURYDPM_DEBUG
612  {
614  }
615 #endif
616  // skip this argument by recursively calling this function again
617  createMessage(msg, fmt, args...);
618  }
@ VERBOSE
#define MERCURYDPM_LOGLEVEL
Definition: Logger.h:17
Flusher doFlush_
Can prevent the logger from flushing the buffer via std::endl. doFlush_ is set automatically based on...
Definition: Logger.h:295

References compute_granudrum_aor::args, Logger< L, ASSERTS >::createMessage(), Logger< L, ASSERTS >::doFlush_, FLUSH, MERCURYDPM_LOGLEVEL, NO_FLUSH, and VERBOSE.

◆ createMessage() [4/4]

template<Log L, bool ASSERTS>
void Logger< L, ASSERTS >::createMessage ( std::stringstream &  msg,
const char message 
)
inlineprivate

Terminating case / no argument call Overloaded function for a logger message without arguments.

Parameters
[in]msgstringstream which represents the output message.
[in]messagechar array of the message.
725  {
726  msg << message;
727  }

◆ if() [1/4]

template<Log L, bool ASSERTS>
else Logger< L, ASSERTS >::if ( loglevel<=Log::ERROR  )
inline
460  {
461  loggerOutput->onError(module, msgstream.str(), doFlush_);
462  }
LoggerOutput * loggerOutput
Declaration of the output functions.
Definition: Logger.cc:262
std::function< void(std::string, std::string, Flusher)> onError
Definition: Logger.h:139

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

◆ if() [2/4]

template<Log L, bool ASSERTS>
else Logger< L, ASSERTS >::if ( loglevel<=Log::INFO  )
inline
469  {
470  loggerOutput->onInfo(module, msgstream.str(), doFlush_);
472  }
std::function< void(std::string, std::string, Flusher)> onInfo
Definition: Logger.h:141

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

◆ if() [3/4]

template<Log L, bool ASSERTS>
else Logger< L, ASSERTS >::if ( loglevel<=Log::VERBOSE  )
inline
474  {
475  loggerOutput->onVerbose(module, msgstream.str(), doFlush_);
477  }
std::function< void(std::string, std::string, Flusher)> onVerbose
Definition: Logger.h:142

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

◆ if() [4/4]

template<Log L, bool ASSERTS>
else Logger< L, ASSERTS >::if ( loglevel<=Log::WARN  )
inline
464  {
465  loggerOutput->onWarn(module, msgstream.str(), doFlush_);
467  }
std::function< void(std::string, std::string, Flusher)> onWarn
Definition: Logger.h:140

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

◆ onFatal()

template<Log L, bool ASSERTS>
template<Log LOGLEVEL, typename... Args>
std::enable_if<L < LOGLEVEL && MERCURYDPM_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_debug(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_debug(bool assertion, const char* format, Args&& ... arg) { } template<typename... Args> void assert_debug(bool assertion, const std::string format, Args&& ... arg) { assert_debug(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(), doFlush_); } } template<typename... Args> void assert_always(bool assertion, const std::string format, Args&& ... arg) { assert_always(assertion, format.c_str(), arg...); } template<typename... Args> MERCURYDPM_DEPRECATED void log(const Log loglevel, const std::string& format, Args&& ... arg) { if (loglevel <= L || loglevel <= MERCURYDPM_LOGLEVEL) { std::stringstream msgstream; createMessage(msgstream, format.c_str(), arg...); if (loglevel <= Log::FATAL) { loggerOutput-> Logger< L, ASSERTS >::onFatal ( module  ,
msgstream.  str(),
doFlush_   
)

Empty body function utilized to suppress logger messages above a certain user defined loglevel L.

◆ operator()()

template<Log L, bool ASSERTS>
template<Log LOGLEVEL, typename ... Args>
std::enable_if<!((L < LOGLEVEL) && (MERCURYDPM_LOGLEVEL < LOGLEVEL)), void>::type Logger< L, ASSERTS >::operator() ( const LL< LOGLEVEL >  log,
const char *format  UNUSED,
Args &&... arg  UNUSED 
)
inline

Log implementation of this function.

Actual implementation of the log function. If the user defined loglevel L is lower than the called LOGLEVEL it will evaluate to an empty body function below. If L is greater than the called LOGLEVEL it will invoke this function.

Parameters
[in]logLoglevel, either FATAL, ERROR, WARN, INFO, VERBOSE, DEBUG
[in]formatMessage format, where % can be used as a placeholder for arguments.
[in]argAny arguments which replace all the % characters.
331  {
332  std::stringstream msgstream;
333  createMessage(msgstream, format, arg...);
334  if (LOGLEVEL <= Log::FATAL)
335  {
336  loggerOutput->onFatal(module, msgstream.str(), doFlush_);
337  }
338  else if (LOGLEVEL <= Log::ERROR)
339  {
340  loggerOutput->onError(module, msgstream.str(), doFlush_);
341  }
342  else if (LOGLEVEL <= Log::WARN)
343  {
344  loggerOutput->onWarn(module, msgstream.str(), doFlush_);
346  }
347  else if (LOGLEVEL <= Log::INFO)
348  {
349  loggerOutput->onInfo(module, msgstream.str(), doFlush_);
351  }
352  else if (LOGLEVEL <= Log::VERBOSE)
353  {
354  loggerOutput->onVerbose(module, msgstream.str(), doFlush_);
356  }
357  else
358  {
359  loggerOutput->onDebug(module, msgstream.str(), doFlush_ = Flusher::FLUSH);
361  }
362  }
@ FATAL
@ WARN
@ INFO
@ ERROR
std::function< void(std::string, std::string, Flusher)> onFatal
Definition: Logger.h:138
std::function< void(std::string, std::string, Flusher)> onDebug
Definition: Logger.h:143
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:217

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

Member Data Documentation

◆ doFlush_ [1/2]

template<Log L, bool ASSERTS>
Flusher Logger< L, ASSERTS >::doFlush_ = Flusher::FLUSH
private

Can prevent the logger from flushing the buffer via std::endl. doFlush_ is set automatically based on build and loglevel settings.

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

◆ doFlush_ [2/2]

template<Log L, bool ASSERTS>
Logger< L, ASSERTS >::doFlush_ = Flusher::FLUSH

◆ else

template<Log L, bool ASSERTS>
Logger< L, ASSERTS >::else
Initial value:
{
LoggerOutput * loggerOutput
Declaration of the output functions.
Definition: Logger.cc:262

◆ module

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

The module name of this actual logger.

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


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