testLogger.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "config.h"
00031
00032 #include <string>
00033
00034 #include "Logger.hpp"
00035
00036
00037
00038 using namespace std;
00039 using namespace mermaid::support::logging;
00040 using boost::shared_ptr;
00041
00042
00043
00044
00045
00046 void testDisable();
00047
00048
00049
00050
00051
00052 int main()
00053 {
00054 string s;
00055
00056 shared_ptr<Logger> logger = FastLogger::build();
00057 logger->enableAllLogLevels (TerminalOutput::build());
00058
00059 logger->log (LOG_STEP, "Starting Logger simple tests");
00060
00061 s = "test";
00062 logger->setPrefix ("Error levels");
00063 logger->log (LOG_ERROR, s);
00064 logger->log (LOG_WARN, s);
00065 logger->log (LOG_INFO, s);
00066 logger->setPrefix ("Reporting levels");
00067 logger->log (LOG_STEP, s);
00068 logger->log (LOG_BEGIN, s);
00069 logger->log (LOG_STEP, s);
00070 logger->log (LOG_SEPARATOR, s);
00071 logger->log (LOG_STEP, s);
00072 logger->log (LOG_END, s);
00073 logger->log (LOG_STEP, s);
00074 logger->log (LOG_SEPARATOR, s);
00075
00076 logger->setPrefix ("");
00077 logger->step ("Done with simple tests.");
00078
00079 logger->begin ("Testing with several outputs.");
00080 shared_ptr<LogOutput> newLogOutput = TerminalOutput::build();
00081 logger->disableAllLogLevels (newLogOutput);
00082 logger->enableErrorLogLevels (newLogOutput);
00083 logger->step ("1 should appear once.");
00084 logger->info ("2 should appear twice.");
00085 logger->disableLogLevel (LOG_INFO, newLogOutput);
00086 logger->info ("3 should appear once.");
00087 logger->warn ("4 should appear twice.");
00088 logger->disableAllLogLevels (newLogOutput);
00089 logger->error ("5 should appear once.");
00090 logger->end ("Done testing several outputs");
00091
00092 logger->begin ("Testing disable with a new logger.");
00093 testDisable();
00094 logger->end ("Done testing disable");
00095
00096 LBEGIN (logger, 1 << " Testing macros");
00097 LINFO (logger, 1 << " I have a spoon.");
00098 LWARN (logger, 1 << " My spoon is too big.");
00099 LERROR (logger, 1 << " errno=ESPOONTOOBIG; return;");
00100 LSEPARATOR (logger, 1 << " Separator");
00101 LSTEP (logger, 1 << " Step");
00102 LEND (logger, 1 << " Done testing macros");
00103
00104
00105 LBEGIN (logger, 1 << " Testing file output");
00106 shared_ptr<LogOutput> fileOutput = FileOutput::build ("logger_test_output.txt");
00107 logger->enableAllLogLevels (fileOutput);
00108 LBEGIN (logger, 1 << " FIRST LINE OF THE FILE");
00109 LINFO (logger, 1 << " This is an info message.");
00110 LWARN (logger, 1 << " I'm trying to divide by zero but I'm not Chuck Norris.");
00111 LERROR (logger, 1 << " I found 5 balls in the field so I'll play chess instead.");
00112 LSEPARATOR (logger, 1 << " Separator");
00113 LSTEP (logger, 1 << " Step");
00114 LEND (logger, 1 << " LAST LINE OF THE FILE");
00115 logger->disableAllLogLevels (fileOutput);
00116 LEND (logger, 1 << " Done testing file output");
00117
00118 LSTEP (logger, "Testing the LogScope class");
00119 LogScope one (logger, "Beginning message", "Final Message");
00120 LogScope two (logger, "Second beginning message", "Before final message");
00121 LogScope three (logger, "Third beginning message, without an ending one.");
00122 LogScope four (logger);
00123 logger->separator();
00124
00125
00126
00127
00128
00129 return 0;
00130 }
00131
00132 void testDisable()
00133 {
00134 shared_ptr<Logger> logger = FastLogger::build ("NEW LOGGER");
00135 logger->step ("This should never appear 1.");
00136 logger->enableAllLogLevels (TerminalOutput::build());
00137 logger->info ("Hello! This is the new logger!");
00138 logger->disableLogLevel (LOG_INFO);
00139 logger->info ("This should never appear 2.");
00140 LogScope ls (logger, "This logger goes bye bye.", "This should never appear 3.");
00141 logger->disableAllLogLevels();
00142 }