ECF 1.5
Logger.cpp
1#include <iostream>
2#include "ECF_base.h"
3
4
5Logger::Logger()
6{
7 currentLevel_ = 3;
8 bFileDefined_ = false;
9 logFrequency_ = 1;
10}
11
12
13Logger::~Logger()
14{
15 logFile_.close();
16}
17
18
19void Logger::registerParameters(StateP state)
20{
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)");
30}
31
32
33bool Logger::initialize(StateP state)
34{
35 if(logFile_.is_open())
36 logFile_.close();
37
38 //reads parameters from registry
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());
43
44 if(state->getRegistry()->isModified("log.filename")) {
45 sptr = state->getRegistry()->getEntry("log.filename");
46 logFileName_ = *((std::string*) sptr.get());
47
48 logFile_.open(logFileName_.c_str());
49 if(logFile_)
50 bFileDefined_ = true;
51 else {
52 throw std::string("Error: can't open logfile (") + logFileName_ + ")";
53 }
54 }
55 else
56 bFileDefined_ = false;
57
58 state_ = state;
59 return true;
60}
61
62
63void Logger::log(int logLevel, std::string message)
64{
65 if(currentLevel_ >= (uint) logLevel) {
66 Log tmp;
67 if(logLevel > 5) logLevel = 5;
68 if(logLevel < 0) logLevel = 0;
69
70#ifdef _MPI
71 message = uint2str(state_->getCommunicator()->getCommGlobalRank()) + ": " + message;
72#endif
73
74 logs_.push_back(tmp);
75 logs_.back().logLevel = logLevel;
76 logs_.back().message = message;
77
78 if(logLevel == 1 || state_->getGenerationNo() % logFrequency_ == 0)
79 std::cout << logs_.back().message << std::endl;
80 }
81}
82
83
84void Logger::saveTo(std::string fileName)
85{
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;
89 }
90
91 logFile.close();
92 logs_.clear();
93}
94
95
96void Logger::saveTo(bool check)
97{
98 if(!bFileDefined_) {
99 logs_.clear();
100 return;
101 }
102
103 if(!check && state_->getGenerationNo() > 1 && state_->getGenerationNo() % logFrequency_ != 0) {
104 logs_.clear();
105 return;
106 }
107
108 std::stringstream logOutput;
109
110 for (uint i = 0; i < logs_.size(); ++i) {
111 logOutput << logs_[i].message << std::endl;
112 }
113
114#ifdef _MPI
115 if(state_->getCommunicator()->getCommGlobalSize() != 1) {
116
117 if(state_->getCommunicator()->getCommGlobalRank() == 0) {
118 std::string remoteLogs = state_->getCommunicator()->recvLogsGlobal();
119 logFile_ << logOutput.str() << remoteLogs;
120 }
121 else {
122 state_->getCommunicator()->sendLogsGlobal(logOutput.str());
123 }
124 }
125
126 else
127 logFile_ << logOutput.str();
128#else
129 logFile_ << logOutput.str();
130#endif
131
132 logs_.clear();
133}
134
135
136void Logger::saveToX(std::string fileName)
137{
138 std::ofstream logFile(fileName.c_str(), std::ios::app);
139 logFile<<"<log>"<<std::endl;
140
141 for (uint i = 0; i < logs_.size(); ++i){
142 logFile <<"<message logLevel=\""<< logs_[i].logLevel << "\">" << logs_[i].message <<"</message>"<< std::endl;
143 }
144 logFile<<"</log>";
145
146 logFile.close();
147 logs_.clear();
148}
149
150
151void Logger::saveToX()
152{
153 if(!bFileDefined_)
154 return;
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;
158 }
159 logFile_ << "</log>";
160
161 logs_.clear();
162}
163
164
166{
167 logs_.clear();
168}
169
170
171bool Logger::operate(StateP state)
172{
173 saveTo();
174 return true;
175}
176
177
179{
180 logFrequency_ = freq;
181}
void flushLog()
deletes current logs
Definition: Logger.cpp:165
void log(int logLevel, std::string message)
Definition: Logger.cpp:63
void saveTo(std::string fileName)
Definition: Logger.cpp:84
bool initialize(StateP state)
Definition: Logger.cpp:33
void setLogFrequency(uint freq)
Definition: Logger.cpp:178