00001 #include <iostream>
00002 #include "ECF_base.h"
00003
00004
00005 Logger::Logger()
00006 {
00007 currentLevel_ = 3;
00008 bFileDefined_ = false;
00009 }
00010
00011
00012 Logger::~Logger()
00013 {
00014 logFile_.close();
00015 }
00016
00017
00018 void Logger::registerParameters(StateP state)
00019 {
00020 uint *level = new uint(3);
00021 state->getRegistry()->registerEntry("log.level", (voidP) level, ECF::UINT);
00022 std::string *file = new std::string("");
00023 state->getRegistry()->registerEntry("log.filename", (voidP) file, ECF::STRING);
00024 uint *freq = new uint(1);
00025 state->getRegistry()->registerEntry("log.frequency", (voidP) freq, ECF::UINT);
00026 }
00027
00028
00029 bool Logger::initialize(StateP state)
00030 {
00031 if(logFile_.is_open())
00032 logFile_.close();
00033
00034
00035 voidP sptr = state->getRegistry()->getEntry("log.level");
00036 currentLevel_ = *((uint*) sptr.get());
00037 sptr = state->getRegistry()->getEntry("log.frequency");
00038 logFrequency_ = *((uint*) sptr.get());
00039
00040 if(state->getRegistry()->isModified("log.filename")) {
00041 sptr = state->getRegistry()->getEntry("log.filename");
00042 logFileName_ = *((std::string*) sptr.get());
00043
00044 logFile_.open(logFileName_.c_str());
00045 if(logFile_)
00046 bFileDefined_ = true;
00047 else {
00048 throw std::string("Error: can't open logfile (") + logFileName_ + ")";
00049 }
00050 }
00051 else
00052 bFileDefined_ = false;
00053
00054 state_ = state;
00055 return true;
00056 }
00057
00058
00059 void Logger::log(int logLevel, std::string message)
00060 {
00061 if(currentLevel_ >= logLevel) {
00062 Log tmp;
00063 if(logLevel > 5) logLevel = 5;
00064 if(logLevel < 0) logLevel = 0;
00065
00066 #ifdef _MPI
00067 message = uint2str(state_->getCommunicator()->getCommGlobalRank()) + ": " + message;
00068 #endif
00069
00070 logs_.push_back(tmp);
00071 logs_.back().logLevel = logLevel;
00072 logs_.back().message = message;
00073
00074 if(logLevel == 1 || state_->getGenerationNo() % logFrequency_ == 0)
00075 std::cout << logs_.back().message << std::endl;
00076 }
00077 }
00078
00079
00080 void Logger::saveTo(std::string fileName)
00081 {
00082 std::ofstream logFile(fileName.c_str(),std::ios::app);
00083 for (unsigned int i = 0; i < logs_.size(); ++i){
00084 logFile << logs_[i].message << std::endl;
00085 }
00086
00087 logFile.close();
00088 logs_.clear();
00089 }
00090
00091
00092 void Logger::saveTo(bool check)
00093 {
00094 if(!bFileDefined_) {
00095 logs_.clear();
00096 return;
00097 }
00098
00099 if(!check && state_->getGenerationNo() > 1 && state_->getGenerationNo() % logFrequency_ != 0) {
00100 logs_.clear();
00101 return;
00102 }
00103
00104 std::stringstream logOutput;
00105
00106 for (unsigned int i = 0; i < logs_.size(); ++i) {
00107 logOutput << logs_[i].message << std::endl;
00108 }
00109
00110 #ifdef _MPI
00111 if(state_->getCommunicator()->getCommGlobalSize() != 1) {
00112
00113 if(state_->getCommunicator()->getCommGlobalRank() == 0) {
00114 std::string remoteLogs = state_->getCommunicator()->recvLogsGlobal();
00115 logFile_ << logOutput.str() << remoteLogs;
00116 }
00117 else {
00118 state_->getCommunicator()->sendLogsGlobal(logOutput.str());
00119 }
00120 }
00121
00122 else
00123 logFile_ << logOutput.str();
00124 #else
00125 logFile_ << logOutput.str();
00126 #endif
00127
00128 logs_.clear();
00129 }
00130
00131
00132 void Logger::saveToX(std::string fileName)
00133 {
00134 std::ofstream logFile(fileName.c_str(), std::ios::app);
00135 logFile<<"<log>"<<std::endl;
00136
00137 for (unsigned int i = 0; i < logs_.size(); ++i){
00138 logFile <<"<message logLevel=\""<< logs_[i].logLevel << "\">" << logs_[i].message <<"</message>"<< std::endl;
00139 }
00140 logFile<<"</log>";
00141
00142 logFile.close();
00143 logs_.clear();
00144 }
00145
00146
00147 void Logger::saveToX()
00148 {
00149 if(!bFileDefined_)
00150 return;
00151 logFile_ << "<log>" << std::endl;
00152 for (unsigned int i = 0; i < logs_.size(); ++i){
00153 logFile_ <<"<message logLevel=\""<< logs_[i].logLevel << "\">" << logs_[i].message <<"</message>"<< std::endl;
00154 }
00155 logFile_ << "</log>";
00156
00157 logs_.clear();
00158 }
00159
00160
00161 void Logger::flushLog()
00162 {
00163 logs_.clear();
00164 }
00165
00166
00167 bool Logger::operate(StateP state)
00168 {
00169 saveTo();
00170 return true;
00171 }
00172
00173
00174 void Logger::setLogFrequency(uint freq)
00175 {
00176 logFrequency_ = freq;
00177 }