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 #ifndef LOGFILE_HPP_
00030 #define LOGFILE_HPP_
00031
00032 #include <string>
00033 #include "OutputFileHandler.hpp"
00034 #include <iostream>
00035 #include <cassert>
00036 #include <iomanip>
00037 #include <ctime>
00038
00067 class LogFile
00068 {
00069 private:
00070
00072 static LogFile* mpInstance;
00073
00075 bool mFileSet;
00076
00078 out_stream mpOutStream;
00079
00081 time_t mInitTime;
00082
00084 unsigned mLevel;
00085
00087 static const unsigned mMaxLoggingLevel = 2;
00088
00090 unsigned mPrecision;
00091
00095 LogFile();
00096
00097 public:
00098
00102 static LogFile* Instance();
00103
00107 static unsigned Level();
00108
00125 void Set(unsigned level, const std::string& rDirectory, const std::string& rFileName="log.txt");
00126
00130 static unsigned MaxLoggingLevel();
00131
00138 void SetPrecision(unsigned precision);
00139
00147 void WriteHeader(std::string simulationType="");
00148
00154 void WriteElapsedTime(std::string pre="");
00155
00159 static void Close();
00160
00164 bool IsFileSet();
00165
00172 template <class T>
00173 LogFile& operator<<(T message)
00174 {
00175 if (mFileSet)
00176 {
00177 (*mpOutStream) << std::setprecision((int)mPrecision) << message << std::flush;
00178 }
00179
00180 return *this;
00181 }
00182 };
00183
00184
00185 #define LOG(level, message) assert(level>0); if(level <= LogFile::Level()) { (*LogFile::Instance()) << message << "\n"; }
00186 #define LOG_AND_COUT(level, message) {std::cout << message << std::endl << std::flush; LOG(level, message); }
00187
00188 #endif