LogFile.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
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 
00030 #ifndef LOGFILE_HPP_
00031 #define LOGFILE_HPP_
00032 
00033 #include <string>
00034 #include "OutputFileHandler.hpp"
00035 #include <iostream>
00036 #include <cassert>
00037 #include <iomanip>
00038 #include <ctime>
00039 
00068 class LogFile
00069 {
00070 private:
00071 
00073     static LogFile* mpInstance;
00074 
00076     bool mFileSet;
00077 
00079     out_stream mpOutStream;
00080 
00082     time_t mInitTime;
00083 
00085     unsigned mLevel;
00086 
00088     static const unsigned mMaxLoggingLevel = 2;
00089 
00091     unsigned mPrecision;
00092 
00096     LogFile();
00097 
00098 public:
00099 
00103     static LogFile* Instance();
00104 
00108     static unsigned Level();
00109 
00126     void Set(unsigned level, const std::string& rDirectory, const std::string& rFileName="log.txt");
00127 
00131     static unsigned MaxLoggingLevel();
00132 
00139     void SetPrecision(unsigned precision);
00140 
00148     void WriteHeader(std::string simulationType="");
00149 
00155     void WriteElapsedTime(std::string pre="");
00156 
00160     static void Close();
00161 
00165     bool IsFileSet();
00166 
00173     template <class T>
00174     LogFile& operator<<(T message)
00175     {
00176         if (mFileSet)
00177         {
00178             (*mpOutStream) << std::setprecision((int)mPrecision) << message << std::flush;
00179         }
00180 
00181         return *this;
00182     }
00183 };
00184 
00185 
00186 // Define the log macro
00187 #define LOG(level, message) assert(level>0); if(level <= LogFile::Level()) { (*LogFile::Instance()) << message << "\n"; }
00188 #define LOG_AND_COUT(level, message) {std::cout << message << std::endl << std::flush; LOG(level, message); }
00189 
00190 
00191 #endif /*LOGFILE_HPP_*/

Generated by  doxygen 1.6.2