Warnings.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include <cstdlib>
00030 #include <iostream>
00031 #include <algorithm>
00032 
00033 #include "Warnings.hpp"
00034 #include "Exception.hpp"
00035 #include "LogFile.hpp"
00036 
00037 Warnings* Warnings::mpInstance = NULL;
00038 
00039 Warnings::Warnings()
00040 {
00041 }
00042 
00043 void Warnings::NoisyDestroy(void)
00044 {
00045     if (mpInstance)
00046     {
00047         for (WarningsContainerType::iterator it = mpInstance->mWarningMessages.begin();
00048              it != mpInstance->mWarningMessages.end();
00049              ++it)
00050         {
00051             /*
00052              * Look at my warnings please.
00053              * First in pair is the context.
00054              * Second in pair is that actual warning.
00055              */
00056             std::cout << it->first << it->second << std::endl;
00057         }
00058         delete mpInstance;
00059         mpInstance = NULL;
00060     }
00061 }
00062 
00063 void Warnings::QuietDestroy(void)
00064 {
00065     if (mpInstance)
00066     {
00067         delete mpInstance;
00068         mpInstance = NULL;
00069     }
00070 }
00071 
00072 Warnings* Warnings::Instance()
00073 {
00074     if (mpInstance == NULL)
00075     {
00076         mpInstance = new Warnings();
00077         std::atexit(NoisyDestroy);
00078     }
00079     return mpInstance;
00080 }
00081 
00082 void Warnings::AddWarning(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber, bool onlyOnce)
00083 {
00084 
00085     std::stringstream line_number_stream;
00086     line_number_stream << lineNumber;
00087     std::string context("Chaste warning: in file " + rFilename + " at line "  + line_number_stream.str()  + ": ");
00088     std::pair<std::string, std::string> item(context, rMessage);
00089 
00090     if (onlyOnce)
00091     {
00092         WarningsContainerType::iterator it = find(mWarningMessages.begin(), mWarningMessages.end(), item);
00093         if (it != mWarningMessages.end())
00094         {
00095             return;
00096         }
00097     }
00098 
00099     mWarningMessages.push_back(item);
00100     LOG(1, context + rMessage);
00101 }
00102 
00103 unsigned Warnings::GetNumWarnings()
00104 {
00105     return mWarningMessages.size();
00106 }
00107 
00108 std::string Warnings::GetNextWarningMessage()
00109 {
00110     if (mWarningMessages.empty())
00111     {
00112         EXCEPTION("There are no warnings");
00113     }
00114     std::string message = mWarningMessages.front().second; // Second in pair is the actual warning
00115     mWarningMessages.pop_front();
00116 
00117     return message;
00118 }
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3