testLogger.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename testLogger.cpp
00024  * @Description Tests for the logging framework.
00025  * @Status Finished
00026  * @Version $Id: testLogger.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Joao Reis (joaocgreis@gmail.com)
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  * @Description Tests for disabling specific levels and LogScope.
00044  * @Author Joao Reis
00045  */
00046 void testDisable();
00047 
00048 /**
00049  * @Description Logger tests entry point.
00050  * @Author Joao Reis
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); // Should do nothing and give no errors
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   /* This should result in a compilation error if uncommented.
00125   LogScope five(one);
00126   LogScope six = one;
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 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3