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 <sstream>
00032 #include <algorithm>
00033 
00034 #include "Warnings.hpp"
00035 #include "Exception.hpp"
00036 #include "LogFile.hpp"
00037 
00038 
00039 
00040 Warnings* Warnings::mpInstance = NULL;
00041 
00042 Warnings::Warnings()
00043 {
00044 }
00045 
00046 void Warnings::NoisyDestroy(void)
00047 {
00048     if (mpInstance)
00049     {
00050         for (WarningsContainerType::iterator it = mpInstance->mWarningMessages.begin();
00051              it != mpInstance->mWarningMessages.end();
00052              ++it)
00053         {
00054             //Look at my warnings please
00055             //First in pair is the context
00056             //Second in pair is that actual warning
00057             std::cout << it->first << it->second << std::endl;
00058         }
00059         delete mpInstance;
00060         mpInstance = NULL;
00061     }
00062 }
00063 
00064 void Warnings::QuietDestroy(void)
00065 {
00066     if (mpInstance)
00067     {
00068         delete mpInstance;
00069         mpInstance = NULL;
00070     }
00071 }
00072 
00073 Warnings* Warnings::Instance()
00074 {
00075     if (mpInstance == NULL)
00076     {
00077         mpInstance = new Warnings();
00078         std::atexit(NoisyDestroy);
00079     }
00080     return mpInstance;
00081 }
00082 
00083 
00084 void 
00085 Warnings::AddWarning(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber, bool onlyOnce)
00086 {
00087     
00088     std::stringstream line_number_stream;
00089     line_number_stream << lineNumber;
00090     std::string context("Chaste warning: in file " + rFilename + " at line "  + line_number_stream.str()  + ": ");
00091     std::pair<std::string, std::string> item(context, rMessage);
00092     
00093     if (onlyOnce)
00094     {
00095         WarningsContainerType::iterator it = find(mWarningMessages.begin(), mWarningMessages.end(), item);
00096         if (it != mWarningMessages.end())
00097         {
00098             return;
00099         }
00100     }
00101 
00102     mWarningMessages.push_back(item);
00103     LOG(1, context + rMessage);
00104 }
00105 
00106 
00107 unsigned 
00108 Warnings::GetNumWarnings()
00109 {
00110     return mWarningMessages.size();
00111 }  
00112     
00113 std::string Warnings::GetNextWarningMessage()
00114 {
00115     if (mWarningMessages.empty())
00116     {
00117         EXCEPTION("There are no warnings");
00118     }
00119     std::string message = mWarningMessages.front().second;  //Second in pair is the actual warning.
00120     mWarningMessages.pop_front();
00121 
00122     return message;
00123 }

Generated on Tue May 31 14:31:41 2011 for Chaste by  doxygen 1.5.5