MercuryDPM  0.11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LoggerUnitTest.cpp
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, 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 #include <iostream>
27 #include "Logger.h"
28 
29 // --- Declaring a logger.
30 // --- This allows you to redefine LogLevels based on command line options.
31 #ifndef HG_LOGLEVEL_SELFTEST
32 #define HG_LOGLEVEL_SELFTEST Log::DEFAULT
33 #endif
35 
36 /* Since we're testing the logger, lets not use the logger itself... */
37 void assertOrDie(bool condition, std::string message) {
38  if (!condition) {
39  std::cerr << "Assert failed: " << message << std::endl;
40  std::exit(1);
41  }
42 }
43 
44 int main() {
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 }
Logger.
Definition: Logger.h:161
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
int main()
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