LogFile.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 
00067 class LogFile
00068 {
00069 private:
00071     static LogFile* mpInstance;
00073     bool mFileSet;
00075     out_stream mpOutStream;
00076 
00077     time_t mInitTime;
00078 
00080     unsigned mLevel;
00081 
00083     static const unsigned mMaxLoggingLevel = 2;
00084 
00086     unsigned mPrecision;
00087 
00091     LogFile();
00092 
00093 public:
00097     static LogFile* Instance();
00098 
00099     static unsigned Level();
00100 
00117     void Set(unsigned level, std::string directory, std::string fileName="log.txt");
00118 
00120     static unsigned MaxLoggingLevel();
00121     
00128     void SetPrecision(unsigned precision);
00129 
00137     void WriteHeader(std::string simulationType="");
00138 
00144     void WriteElapsedTime(std::string pre="");
00145 
00149     static void Close();
00150 
00154     bool IsFileSet();
00155 
00160     template <class T>
00161     LogFile& operator<<(T message)
00162     {
00163         if (mFileSet)
00164         {
00165             (*mpOutStream) << std::setprecision((int)mPrecision) << message << std::flush;
00166         }
00167 
00168         return *this;
00169     }
00170 };
00171 
00172 
00173 // define the log macro
00174 #define LOG(level, message) assert(level>0); if(level <= LogFile::Level()) { (*LogFile::Instance()) << message << "\n"; }
00175 #define LOG_AND_COUT(level, message) {std::cout << message << std::endl << std::flush; LOG(level, message); }
00176 
00177 
00178 #endif /*LOGFILE_HPP_*/

Generated on Wed Mar 18 12:51:50 2009 for Chaste by  doxygen 1.5.5