LogFile.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef LOGFILE_HPP_
00037 #define LOGFILE_HPP_
00038
00039 #include <string>
00040 #include "OutputFileHandler.hpp"
00041 #include <iostream>
00042 #include <cassert>
00043 #include <iomanip>
00044 #include <ctime>
00045
00080 class LogFile
00081 {
00082 private:
00083
00085 static LogFile* mpInstance;
00086
00088 bool mFileSet;
00089
00091 out_stream mpOutStream;
00092
00094 time_t mInitTime;
00095
00097 unsigned mLevel;
00098
00100 static const unsigned mMaxLoggingLevel = 2;
00101
00103 unsigned mPrecision;
00104
00108 LogFile();
00109
00110 public:
00111
00115 static LogFile* Instance();
00116
00120 static unsigned Level();
00121
00138 void Set(unsigned level, const std::string& rDirectory, const std::string& rFileName="log.txt");
00139
00143 static unsigned MaxLoggingLevel();
00144
00151 void SetPrecision(unsigned precision);
00152
00160 void WriteHeader(std::string simulationType="");
00161
00167 void WriteElapsedTime(std::string pre="");
00168
00172 static void Close();
00173
00177 bool IsFileSet();
00178
00186 template <class T>
00187 LogFile& operator<<(T message)
00188 {
00189 if (mFileSet)
00190 {
00191 (*mpOutStream) << std::setprecision((int)mPrecision) << message << std::flush;
00192 }
00193
00194 return *this;
00195 }
00196 };
00197
00198
00199 #define LOG(level, message) assert(level>0); if(level <= LogFile::Level()) { (*LogFile::Instance()) << message << "\n"; }
00200 #define LOG_AND_COUT(level, message) {std::cout << message << std::endl << std::flush; LOG(level, message); }
00201
00202 #endif