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 
)
18 {
19  if (!condition)
20  {
21  std::cerr << "Assert failed: " << message << std::endl;
22  std::exit(1);
23  }
24 }

Referenced by main().

◆ main()

int main ( )
27 {
28  int x = 3;
29  bool hasLogged = false;
30  std::string lastMessage;
31  std::string lastModule;
32  auto tmpLogger = [&](std::string module, std::string msg, Flusher doFlush)
33  {
34  lastMessage = msg;
35  lastModule = module;
36  hasLogged = true;
37  };
38  loggerOutput->onFatal = tmpLogger;
39  loggerOutput->onError = tmpLogger;
40  loggerOutput->onWarn = tmpLogger;
41  loggerOutput->onInfo = tmpLogger;
42  loggerOutput->onVerbose = tmpLogger;
43  loggerOutput->onDebug = tmpLogger;
44 
45  //Basic usage cases:
46  unitLogger(ERROR, "Oopsie!"); //An error!
47  assertOrDie(hasLogged, "No output detected!");
48  hasLogged = false;
49 
50  unitLogger(FATAL, "Mweh. x = %", x);
51  assertOrDie(hasLogged, "No output detected!");
52  assertOrDie(lastMessage == "Mweh. x = 3", "Substitution gone wrong!");
53  hasLogged = false;
54 
55  unitLogger(DEBUG, "You won't see me!");
56  assertOrDie(!hasLogged, "Output detected!");
57 
58  unitLogger(WARN, "Escapes are possible! %\% sure!", 100.01f);
59  assertOrDie(lastMessage == "Escapes are possible! 100.01% sure!", "Escape gone wrong!");
60  //Usage case for redefining with an function
61 
62  unitLogger(FATAL, "Test");
63  assertOrDie(lastModule == "UnitTester", "Module was incorrect!");
64  logger(FATAL, "Test");
65  assertOrDie(lastModule != "UnitTester", "Module was incorrect!");
66 
67  return 0;
68 }
Logger< HG_LOGLEVEL_SELFTEST > unitLogger("UnitTester")
void assertOrDie(bool condition, std::string message)
Definition: LoggerUnitTest.cpp:17
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:31
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:32
LoggerOutput * loggerOutput
Declaration of the output functions.
Definition: Logger.cc:262
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:33
Flusher
The Logger class provides ability to write log messages in your own customized format.
Definition: Logger.h:94
std::function< void(std::string, std::string, Flusher)> onVerbose
Definition: Logger.h:142
std::function< void(std::string, std::string, Flusher)> onFatal
Definition: Logger.h:138
std::function< void(std::string, std::string, Flusher)> onWarn
Definition: Logger.h:140
std::function< void(std::string, std::string, Flusher)> onError
Definition: Logger.h:139
std::function< void(std::string, std::string, Flusher)> onInfo
Definition: Logger.h:141
std::function< void(std::string, std::string, Flusher)> onDebug
Definition: Logger.h:143
#define DEBUG
Definition: main.h:181
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
list x
Definition: plotDoE.py:28

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

Variable Documentation

◆ unitLogger

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

Referenced by main().