Logger.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef LOGGER_H
27 #define LOGGER_H
28 
29 #include <string>
30 #include <sstream>
31 #include <functional>
32 #include <type_traits>
33 #include <iomanip>
34 #include "GeneralDefine.h"
35 #include <iostream>
36 
37 #ifndef MERCURYDPM_LOGLEVEL
38 #define MERCURYDPM_LOGLEVEL Log::DEFAULT
39 #endif
40 
41 #ifndef CG_LOGLEVEL
42 #define CG_LOGLEVEL Log::DEFAULT
43 #endif
44 
45 #ifdef assert
46 #undef assert
47 //#error You included assert before the logger. Please use logger.assert_debug() instead.
48 #endif
49 
50 #ifdef MERCURYDPM_FORCE_ASSERTS
51 #define MERCURYDPM_ASSERTS true
52 #else
53 #ifdef MERCURYDPM_NO_ASSERTS
54 #define MERCURYDPM_ASSERTS false
55 #else
56 #ifdef NDEBUG
57 #define MERCURYDPM_ASSERTS false
58 #else
59 #define MERCURYDPM_ASSERTS true
60 #endif
61 #endif
62 #endif
63 
114 enum class Flusher
115 {
116  FLUSH,
117  NO_FLUSH
118 };
119 
128 enum class Log
129  : signed char
130 {
131  FATAL = -20, ERROR = -15, WARN = -10, INFO = -5, DEFAULT = 0, VERBOSE = 5, DEBUG = 10
132 };
133 
140 constexpr bool operator<=(const Log rhs, const Log lhs)
141 {
142  return ((static_cast<signed char>(rhs)) <= (static_cast<signed char>(lhs)));
143 }
144 
157 {
158 public:
159  std::function<void(std::string, std::string, Flusher)> onFatal;
160  std::function<void(std::string, std::string, Flusher)> onError;
161  std::function<void(std::string, std::string, Flusher)> onWarn;
162  std::function<void(std::string, std::string, Flusher)> onInfo;
163  std::function<void(std::string, std::string, Flusher)> onVerbose;
164  std::function<void(std::string, std::string, Flusher)> onDebug;
165 };
166 
175 extern LoggerOutput* loggerOutput;
176 
177 // Forward declaration..
178 template<Log L = Log::DEFAULT, bool ASSERTS = MERCURYDPM_ASSERTS>
179 class Logger;
180 
193 template<Log Level>
194 class LL
195 {
196 public:
197 };
198 
215 extern LL<Log::FATAL> FATAL;
216 
229 extern LL<Log::ERROR> ERROR;
230 
242 extern LL<Log::WARN> WARN;
243 
254 extern LL<Log::INFO> INFO;
255 
262 
274 
285 extern LL<Log::DEBUG> DEBUG;
286 
303 template<Log L, bool ASSERTS>
304 class Logger
305 {
306 
307 private:
311  const std::string module;
317 
318 public:
319 
325  explicit Logger(const std::string name)
326  : module(name)
327  {
328  }
329 
334  = default;
335 
349  template<Log LOGLEVEL, typename ... Args>
350  typename std::enable_if<!((L < LOGLEVEL) && (MERCURYDPM_LOGLEVEL < LOGLEVEL)), void>::type
351  operator()(const LL<LOGLEVEL> log, const char* format UNUSED, Args&& ... arg UNUSED)
352  {
353  std::stringstream msgstream;
354  createMessage(msgstream, format, arg...);
355  if (LOGLEVEL <= Log::FATAL)
356  {
357  loggerOutput->onFatal(module, msgstream.str(), doFlush_);
358  }
359  else if (LOGLEVEL <= Log::ERROR)
360  {
361  loggerOutput->onError(module, msgstream.str(), doFlush_);
362  }
363  else if (LOGLEVEL <= Log::WARN)
364  {
365  loggerOutput->onWarn(module, msgstream.str(), doFlush_);
367  }
368  else if (LOGLEVEL <= Log::INFO)
369  {
370  loggerOutput->onInfo(module, msgstream.str(), doFlush_);
372  }
373  else if (LOGLEVEL <= Log::VERBOSE)
374  {
375  loggerOutput->onVerbose(module, msgstream.str(), doFlush_);
377  }
378  else
379  {
380  loggerOutput->onDebug(module, msgstream.str(), doFlush_ = Flusher::FLUSH);
382  }
383  }
384 
388  template<Log LOGLEVEL, typename... Args>
389  typename std::enable_if<L < LOGLEVEL && MERCURYDPM_LOGLEVEL < LOGLEVEL, void>::type
390  operator()(const LL<LOGLEVEL> log, const char* format UNUSED, Args&& ... arg UNUSED)
391  {
392  }
393 
397  //std::string is sometimes convenient, but always slow, so where possible, don't convert the const char* to a string
398  //before converting it back
399  template<Log LOGLEVEL, typename... Args>
400  void operator()(const LL<LOGLEVEL> log, const std::string& format UNUSED, Args&& ... arg
401  UNUSED)
402  {
403  (*this)(log, format.c_str(), arg...);
404  }
405 
420  template<typename... Args>
421  typename std::enable_if<(ASSERTS) && (sizeof...(Args) >= 0), void>::type
422  assert_debug(bool assertion, const char* format, Args&& ... arg)
423  {
424  assert_always(assertion, format, arg...);
425  }
426 
427  template<typename... Args>
428  typename std::enable_if<!((ASSERTS) && sizeof...(Args) >= 0), void>::type
429  assert_debug(bool assertion, const char* format, Args&& ... arg)
430  {
431  }
432 
436  //the conversion from "" to a std::string is so slow, it takes 50% of the total run time for a release build...
437  template<typename... Args>
438  void assert_debug(bool assertion, const std::string format, Args&& ... arg)
439  {
440  assert_debug(assertion, format.c_str(), arg...);
441  }
442 
443  template<typename... Args>
444  void assert_always(bool assertion, const char* format, Args&& ... arg)
445  {
446  if (!assertion)
447  {
448  std::stringstream msgstream;
449  createMessage(msgstream, format, arg...);
450  loggerOutput->onFatal(module, msgstream.str(), doFlush_);
451  }
452 
453  }
454 
458  template<typename... Args>
459  void assert_always(bool assertion, const std::string format, Args&& ... arg)
460  {
461  assert_always(assertion, format.c_str(), arg...);
462  }
463 
468  template<typename... Args>
470  void log(const Log loglevel, const std::string& format, Args&& ... arg)
471  {
472  if (loglevel <= L || loglevel <= MERCURYDPM_LOGLEVEL)
473  {
474  std::stringstream msgstream;
475  createMessage(msgstream, format.c_str(), arg...);
476  if (loglevel <= Log::FATAL)
477  {
478  loggerOutput->onFatal(module, msgstream.str(), doFlush_);
479  }
480  else if (loglevel <= Log::ERROR)
481  {
482  loggerOutput->onError(module, msgstream.str(), doFlush_);
483  }
484  else if (loglevel <= Log::WARN)
485  {
486  loggerOutput->onWarn(module, msgstream.str(), doFlush_);
488  }
489  else if (loglevel <= Log::INFO)
490  {
491  loggerOutput->onInfo(module, msgstream.str(), doFlush_);
493  }
494  else if (loglevel <= Log::VERBOSE)
495  {
496  loggerOutput->onVerbose(module, msgstream.str(), doFlush_);
498  }
499  else
500  {
501  loggerOutput->onDebug(module, msgstream.str(), doFlush_ = Flusher::FLUSH);
503  }
504  }
505  }
506 
507 private:
508 
523  template<typename Arg1, typename... Args>
524  void createMessage(std::stringstream& msg, const char* fmt,
525  Arg1&& arg, Args&& ... args)
526  {
527  bool doSkipNext = false;
528  while (*fmt != '%' || doSkipNext)
529  {
530  //Make sure we're not running past the end of our formatting string.
531  if (*fmt == '\0')
532  return;
533 
534  if (*fmt == '\\' && !doSkipNext)
535  { //Escape for the % character
536  doSkipNext = true;
537  fmt++;
538  }
539  else
540  {
541  msg << *fmt;
542  fmt++;
543  doSkipNext = false;
544  }
545  }
546  fmt++; //Consume the % character
547  int precision = 0;
548  int width = 0;
549  // if precision and width or only precision is defined
550  if (isdigit(*fmt))
551  {
552  precision = std::atoi(fmt);
553  while (isdigit(*fmt))
554  {
555  fmt++;
556  }
557  if (std::ispunct(*fmt))
558  {
559  fmt++;
560  if (std::isdigit(*fmt))
561  {
562  width = std::atoi(fmt);
563  while (isdigit(*fmt))
564  {
565  fmt++;
566  }
567  }
568  // else the char is a real full stop so set the pointer back to full stop.
569  else
570  {
571  fmt--;
572  }
573  }
574  }
575  // if only a width and no precision defined
576  else if (std::ispunct(*fmt))
577  {
578  fmt++;
579  if (std::isdigit(*fmt))
580  {
581  width = std::atoi(fmt);
582  while (isdigit(*fmt))
583  {
584  fmt++;
585  }
586  }
587  // else the char is a real full stop so set the pointer back to full stop.
588  else
589  {
590  fmt--;
591  }
592  }
593  if (width != 0 && precision != 0)
594  {
595  msg << std::setprecision(precision) << std::left << std::setw(width) << arg;
596  }
597  else if (precision != 0)
598  {
599  msg << std::setprecision(precision) << arg;
600  }
601  else if (width != 0)
602  {
603  msg << std::left << std::setw(width) << arg;
604  }
605  else
606  {
607  msg << arg;
608  } //include args somehow..
609  createMessage(msg, fmt, args...);//and recursively call ourselve / the method below.
610  }
611 
612 
623  //terminating case for Flusher not needed. This function is also called when the parameter pack Args&& args is
624  // empty
625  template<typename... Args>
626  void createMessage(std::stringstream& msg, const char* fmt,
627  Flusher arg, Args&& ... args)
628  {
629  // only suppress flushing if Mercury is not in CMAKE_BUILD_TYPE "Debug" and if the user defined loglevel from
630  // cMake is below VERBOSE/DEBUG (<=5)
631 #ifndef MERCURYDPM_DEBUG
633  {
635  }
636 #endif
637  // skip this argument by recursively calling this function again
638  createMessage(msg, fmt, args...);
639  }
640 
641 
650  // faster than above function and non recursive that is why it is good to have it
651  template<typename Arg1>
652  void createMessage(std::stringstream& msg, const char* fmt, Arg1&& arg)
653  {
654  bool doSkipNext = false;
655  while (*fmt != '%' || doSkipNext)
656  {
657  if (*fmt == '\0') // End of string
658  return;
659 
660  if (*fmt == '\\' && !doSkipNext)
661  { //Escape for the % character and the \ character
662  doSkipNext = true;
663  fmt++;
664  }
665  else
666  { //invoke the replacement
667  msg << *fmt;
668  fmt++;
669  doSkipNext = false;
670  }
671  }
672  fmt++; //Consume the % character
673  int precision = 0;
674  int width = 0;
675  // if precision and width or only precision is defined
676  if (isdigit(*fmt))
677  {
678  precision = std::atoi(fmt);
679  while (isdigit(*fmt))
680  {
681  fmt++;
682  }
683  if (std::ispunct(*fmt))
684  {
685  fmt++;
686  if (std::isdigit(*fmt))
687  {
688  width = std::atoi(fmt);
689  while (isdigit(*fmt))
690  {
691  fmt++;
692  }
693  }
694  // else the char is a real full stop so set the pointer back to full stop.
695  else
696  {
697  fmt--;
698  }
699  }
700  }
701  // if only a width and no precision defined
702  else if (std::ispunct(*fmt))
703  {
704  fmt++;
705  if (std::isdigit(*fmt))
706  {
707  width = std::atoi(fmt);
708  while (isdigit(*fmt))
709  {
710  fmt++;
711  }
712  }
713  // else the char is a real full stop so set the pointer back to full stop.
714  else
715  {
716  fmt--;
717  }
718  }
719  if (width != 0 && precision != 0)
720  {
721  msg << std::setprecision(precision) << std::left << std::setw(width) << arg << fmt;
722  }
723  else if (precision != 0)
724  {
725  msg << std::setprecision(precision) << arg << fmt;
726  }
727  else if (width != 0)
728  {
729  msg << std::left << std::setw(width) << arg << fmt;
730  }
731  else
732  {
733  msg << arg << fmt;
734  }
735  }
736 
737 
745  void createMessage(std::stringstream& msg, const char* message)
746  {
747  msg << message;
748  }
749 };
750 
760 
762 
763 //just emptying the functions is not sufficiently aggressive in disabling the actual (costly) comparison
764 #if !MERCURYDPM_ASSERTS
765 #define assert(e,...) assert(true,"")
766 #endif
767 
768 #ifdef MERCURYDPM_USE_MPI
769 #include "MpiContainer.h"
770 #endif
771 
772 #endif
#define MERCURYDPM_DEPRECATED
Definition: GeneralDefine.h:37
#define UNUSED
Definition: GeneralDefine.h:39
Logger< CG_LOGLEVEL > cgLogger
LL< Log::VERBOSE > VERBOSE
Verbose information.
Definition: Logger.cc:57
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
LL< Log::DEBUG > DEBUG
Debug information.
Definition: Logger.cc:58
Log
The different loglevels.
Definition: Logger.h:130
@ FATAL
@ WARN
@ INFO
@ DEFAULT
@ ERROR
@ DEBUG
@ VERBOSE
LL< Log::FATAL > FATAL
Fatal log level.
Definition: Logger.cc:52
Flusher
The Logger class provides ability to write log messages in your own customized format.
Definition: Logger.h:115
#define MERCURYDPM_LOGLEVEL
Definition: Logger.h:38
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:53
LoggerOutput * loggerOutput
Declaration of the output functions.
Definition: Logger.cc:283
Logger< MERCURYDPM_LOGLEVEL > logger
constexpr bool operator<=(const Log rhs, const Log lhs)
Internally used to filter on loglevel. Do not edit, as this is required for an optimised logger.
Definition: Logger.h:140
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
LL< Log::DEFAULT > DEFAULT
Default log level.
Definition: Logger.cc:56
Tag for template metaprogramming.
Definition: Logger.h:195
Default functions for output generation.
Definition: Logger.h:157
std::function< void(std::string, std::string, Flusher)> onVerbose
Definition: Logger.h:163
std::function< void(std::string, std::string, Flusher)> onFatal
Definition: Logger.h:159
std::function< void(std::string, std::string, Flusher)> onWarn
Definition: Logger.h:161
std::function< void(std::string, std::string, Flusher)> onError
Definition: Logger.h:160
std::function< void(std::string, std::string, Flusher)> onInfo
Definition: Logger.h:162
std::function< void(std::string, std::string, Flusher)> onDebug
Definition: Logger.h:164
the Logger class is the main class of the logger implementation. It holds all the functions which inv...
Definition: Logger.h:305
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...
Definition: Logger.h:626
const std::string module
The module name of this actual logger.
Definition: Logger.h:311
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 ...
Definition: Logger.h:652
void createMessage(std::stringstream &msg, const char *message)
Terminating case / no argument call Overloaded function for a logger message without arguments.
Definition: Logger.h:745
Logger(const std::string name)
constructor
Definition: Logger.h:325
~Logger()=default
destructor
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.
Definition: Logger.h:351
Flusher doFlush_
Can prevent the logger from flushing the buffer via std::endl. doFlush_ is set automatically based on...
Definition: Logger.h:316
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:524
Mdouble log(Mdouble Power)
Definition: ExtendedMath.cc:104
std::string name
Definition: MercuryProb.h:48