19void Logger::registerParameters(StateP state)
21 uint *level =
new uint(3);
22 state->getRegistry()->registerEntry(
"log.level", (voidP) level, ECF::UINT,
23 "log level; valid values are 1 (minimal) to 5 (verbose) (default: 3)");
24 std::string *file =
new std::string(
"");
25 state->getRegistry()->registerEntry(
"log.filename", (voidP) file, ECF::STRING,
26 "log filename (default: none)");
27 uint *freq =
new uint(1);
28 state->getRegistry()->registerEntry(
"log.frequency", (voidP) freq, ECF::UINT,
29 "log only every 'frequency' generations (default: 1)");
35 if(logFile_.is_open())
39 voidP sptr = state->getRegistry()->getEntry(
"log.level");
40 currentLevel_ = *((uint*) sptr.get());
41 sptr = state->getRegistry()->getEntry(
"log.frequency");
42 logFrequency_ = *((uint*) sptr.get());
44 if(state->getRegistry()->isModified(
"log.filename")) {
45 sptr = state->getRegistry()->getEntry(
"log.filename");
46 logFileName_ = *((std::string*) sptr.get());
48 logFile_.open(logFileName_.c_str());
52 throw std::string(
"Error: can't open logfile (") + logFileName_ +
")";
56 bFileDefined_ =
false;
65 if(currentLevel_ >= (uint) logLevel) {
67 if(logLevel > 5) logLevel = 5;
68 if(logLevel < 0) logLevel = 0;
71 message = uint2str(state_->getCommunicator()->getCommGlobalRank()) +
": " + message;
75 logs_.back().logLevel = logLevel;
76 logs_.back().message = message;
78 if(logLevel == 1 || state_->getGenerationNo() % logFrequency_ == 0)
79 std::cout << logs_.back().message << std::endl;
86 std::ofstream logFile(fileName.c_str(),std::ios::app);
87 for (uint i = 0; i < logs_.size(); ++i){
88 logFile << logs_[i].message << std::endl;
103 if(!check && state_->getGenerationNo() > 1 && state_->getGenerationNo() % logFrequency_ != 0) {
108 std::stringstream logOutput;
110 for (uint i = 0; i < logs_.size(); ++i) {
111 logOutput << logs_[i].message << std::endl;
115 if(state_->getCommunicator()->getCommGlobalSize() != 1) {
117 if(state_->getCommunicator()->getCommGlobalRank() == 0) {
118 std::string remoteLogs = state_->getCommunicator()->recvLogsGlobal();
119 logFile_ << logOutput.str() << remoteLogs;
122 state_->getCommunicator()->sendLogsGlobal(logOutput.str());
127 logFile_ << logOutput.str();
129 logFile_ << logOutput.str();
136void Logger::saveToX(std::string fileName)
138 std::ofstream logFile(fileName.c_str(), std::ios::app);
139 logFile<<
"<log>"<<std::endl;
141 for (uint i = 0; i < logs_.size(); ++i){
142 logFile <<
"<message logLevel=\""<< logs_[i].logLevel <<
"\">" << logs_[i].message <<
"</message>"<< std::endl;
151void Logger::saveToX()
155 logFile_ <<
"<log>" << std::endl;
156 for (uint i = 0; i < logs_.size(); ++i){
157 logFile_ <<
"<message logLevel=\""<< logs_[i].logLevel <<
"\">" << logs_[i].message <<
"</message>"<< std::endl;
159 logFile_ <<
"</log>";
171bool Logger::operate(StateP state)
180 logFrequency_ = freq;
void flushLog()
deletes current logs
void log(int logLevel, std::string message)
void saveTo(std::string fileName)
bool initialize(StateP state)
void setLogFrequency(uint freq)