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 #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
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