MercuryDPM  0.11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LoggerUnitTest.cpp File Reference
#include <iostream>
#include "Logger.h"

Go to the source code of this file.

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

#define HG_LOGLEVEL_SELFTEST   Log::DEFAULT

Definition at line 32 of file LoggerUnitTest.cpp.

Function Documentation

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

Definition at line 37 of file LoggerUnitTest.cpp.

Referenced by main().

37  {
38  if (!condition) {
39  std::cerr << "Assert failed: " << message << std::endl;
40  std::exit(1);
41  }
42 }
int main ( )

Definition at line 44 of file LoggerUnitTest.cpp.

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

44  {
45  int x = 3;
46  bool hasLogged = false;
47  std::string lastMessage;
48  std::string lastModule;
49  auto tmpLogger = [&](std::string module, std::string msg) {
50  lastMessage = msg;
51  lastModule = module;
52  hasLogged = true;
53  };
54  loggerOutput->onFatal = tmpLogger;
55  loggerOutput->onError = tmpLogger;
56  loggerOutput->onWarn = tmpLogger;
57  loggerOutput->onInfo = tmpLogger;
58  loggerOutput->onVerbose = tmpLogger;
59  loggerOutput->onDebug = tmpLogger;
60 
61  //Basic usage cases:
62  unitLogger(ERROR, "Oopsie!"); //An error!
63  assertOrDie(hasLogged, "No output detected!");
64  hasLogged = false;
65 
66  unitLogger(FATAL, "Mweh. x = %", x);
67  assertOrDie(hasLogged, "No output detected!");
68  assertOrDie(lastMessage == "Mweh. x = 3", "Substitution gone wrong!");
69  hasLogged = false;
70 
71  unitLogger(DEBUG, "You won't see me!");
72  assertOrDie(!hasLogged, "Output detected!");
73 
74  unitLogger(WARN, "Escapes are possible! %\% sure!", 100.01f);
75  assertOrDie(lastMessage == "Escapes are possible! 100.01% sure!", "Escape gone wrong!");
76  //Usage case for redefining with an function
77 
78  unitLogger(FATAL, "Test");
79  assertOrDie(lastModule == "UnitTester", "Module was incorrect!");
80  logger(FATAL, "Test");
81  assertOrDie(lastModule != "UnitTester", "Module was incorrect!");
82 
83  return 0;
84 }
LL< Log::DEBUG > DEBUG
Debug information.
Definition: Logger.cc:31
std::function< void(std::string, std::string)> onWarn
Definition: Logger.h:144
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
std::function< void(std::string, std::string)> onVerbose
Definition: Logger.h:146
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:26
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:27
std::function< void(std::string, std::string)> onError
Definition: Logger.h:143
void assertOrDie(bool condition, std::string message)
LL< Log::FATAL > FATAL
Fatal log level.
Definition: Logger.cc:25
LoggerOutput * loggerOutput
Declaration of the output functions. If the output needs to be redirected, please swap the loggerOutp...
Definition: Logger.cc:110
std::function< void(std::string, std::string)> onDebug
Definition: Logger.h:147
std::function< void(std::string, std::string)> onInfo
Definition: Logger.h:145
Logger< HG_LOGLEVEL_SELFTEST > unitLogger("UnitTester")
std::function< void(std::string, std::string)> onFatal
Definition: Logger.h:142

Variable Documentation

Logger<HG_LOGLEVEL_SELFTEST> unitLogger("UnitTester")

Referenced by main().