LoggerUnitTest.cpp File Reference
#include <iostream>
#include "Logger.h"
#include <cstdlib>

Macros

#define HG_LOGLEVEL_SELFTEST   Log::DEFAULT
 

Functions

void assertOrDie (bool condition, std::string message)
 
int main ()
 

Variables

Logger< HG_LOGLEVEL_SELFTESTunitLogger ("UnitTester")
 

Macro Definition Documentation

◆ HG_LOGLEVEL_SELFTEST

#define HG_LOGLEVEL_SELFTEST   Log::DEFAULT

Function Documentation

◆ assertOrDie()

void assertOrDie ( bool  condition,
std::string  message 
)
39 {
40  if (!condition)
41  {
42  std::cerr << "Assert failed: " << message << std::endl;
43  std::exit(1);
44  }
45 }

Referenced by main().

◆ main()

int main ( )
48 {
49  int x = 3;
50  bool hasLogged = false;
51  std::string lastMessage;
52  std::string lastModule;
53  auto tmpLogger = [&](std::string module, std::string msg, Flusher doFlush)
54  {
55  lastMessage = msg;
56  lastModule = module;
57  hasLogged = true;
58  };
59  loggerOutput->onFatal = tmpLogger;
60  loggerOutput->onError = tmpLogger;
61  loggerOutput->onWarn = tmpLogger;
62  loggerOutput->onInfo = tmpLogger;
63  loggerOutput->onVerbose = tmpLogger;
64  loggerOutput->onDebug = tmpLogger;
65 
66  //Basic usage cases:
67  unitLogger(ERROR, "Oopsie!"); //An error!
68  assertOrDie(hasLogged, "No output detected!");
69  hasLogged = false;
70 
71  unitLogger(FATAL, "Mweh. x = %", x);
72  assertOrDie(hasLogged, "No output detected!");
73  assertOrDie(lastMessage == "Mweh. x = 3", "Substitution gone wrong!");
74  hasLogged = false;
75 
76  unitLogger(DEBUG, "You won't see me!");
77  assertOrDie(!hasLogged, "Output detected!");
78 
79  unitLogger(WARN, "Escapes are possible! %\% sure!", 100.01f);
80  assertOrDie(lastMessage == "Escapes are possible! 100.01% sure!", "Escape gone wrong!");
81  //Usage case for redefining with an function
82 
83  unitLogger(FATAL, "Test");
84  assertOrDie(lastModule == "UnitTester", "Module was incorrect!");
85  logger(FATAL, "Test");
86  assertOrDie(lastModule != "UnitTester", "Module was incorrect!");
87 
88  return 0;
89 }
Logger< HG_LOGLEVEL_SELFTEST > unitLogger("UnitTester")
void assertOrDie(bool condition, std::string message)
Definition: LoggerUnitTest.cpp:38
LL< Log::DEBUG > DEBUG
Debug information.
Definition: Logger.cc:58
LL< Log::FATAL > FATAL
Definition of the different loglevels by its wrapper class LL. These are used as tags in template met...
Definition: Logger.cc:52
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:53
LoggerOutput * loggerOutput
Declaration of the output functions.
Definition: Logger.cc:283
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
Flusher
The Logger class provides ability to write log messages in your own customized format.
Definition: Logger.h:115
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

References assertOrDie(), DEBUG, ERROR, FATAL, logger, loggerOutput, LoggerOutput::onDebug, LoggerOutput::onError, LoggerOutput::onFatal, LoggerOutput::onInfo, LoggerOutput::onVerbose, LoggerOutput::onWarn, unitLogger, and WARN.

Variable Documentation

◆ unitLogger

Logger<HG_LOGLEVEL_SELFTEST> unitLogger("UnitTester") ( "UnitTester"  )

Referenced by main().