34 #ifndef MOOSE_LOGGER_INC
35 #define MOOSE_LOGGER_INC
51 #define T_RESET "\033[0m"
52 #define T_BLACK "\033[30m"
53 #define T_RED "\033[31m"
54 #define T_GREEN "\033[32m"
55 #define T_YELLOW "\033[33m"
56 #define T_BLUE "\033[34m"
57 #define T_MAGENTA "\033[35m"
58 #define T_CYAN "\033[36m"
59 #define T_WHITE "\033[37m"
60 #define T_BOLDBLACK "\033[1m\033[30m"
61 #define T_BOLDRED "\033[1m\033[31m"
62 #define T_BOLDGREEN "\033[1m\033[32m"
63 #define T_BOLDYELLOW "\033[1m\033[33m"
64 #define T_BOLDBLUE "\033[1m\033[34m"
65 #define T_BOLDMAGENTA "\033[1m\033[35m"
66 #define T_BOLDCYAN "\033[1m\033[36m"
67 #define T_BOLDWHITE "\033[1m\033[37m"
80 startTime = timeStamp();
81 homeDir = getenv(
"HOME");
83 logSS <<
"<log simulator=\"moose\">" << endl;
84 logSS <<
"\t<start_time>" << startTime <<
"</start_time>" << endl;
85 logSS <<
"\t<messages>" << endl;
88 outputFile = homeDir +
"\\.moose\\log";
90 outputFile = homeDir +
"/.moose/log";
106 time_t now = time(0);
109 tstruct = *localtime(&now);
110 strftime(buf,
sizeof(buf),
"%Y-%m-%d.%X", &tstruct);
123 if(elementsMap.find(type) == elementsMap.end())
124 elementsMap[type] = 1;
126 elementsMap[type] = elementsMap[type] + 1;
129 template<
typename A,
typename B>
136 for(
unsigned i = 0; i < width - title.size(); ++i)
140 typename map<A, B>::const_iterator it;
141 for( it = m.begin(); it != m.end(); it++)
142 ss << setw(width/2) << it->first << setw(width/2) << it->second << endl;
144 for(
unsigned i = 0; i < width; ++i)
165 ss << mapToString<string, unsigned long>(elementsMap,
"data_structure");
169 timekeeperMap[
"Simulation"] = accumulate(
170 simulationTime.begin()
171 , simulationTime.end()
174 timekeeperMap[
"Initialization"] = accumulate(
175 initializationTime.begin()
176 , initializationTime.end()
179 timekeeperMap[
"Creation"] = accumulate(
185 ss << mapToString<string, float>( timekeeperMap,
"simulation_stats" );
201 template<
typename A,
typename B>
202 void mapToXML(ostringstream& ss,
const map<A, B>& m,
const char* tagName
203 ,
unsigned indent)
const
206 for(
unsigned int i = 0; i < indent; ++i)
208 ss << prefix <<
"<" << tagName <<
">" << endl;
210 typename map<A, B>::const_iterator it;
211 for(it = m.begin(); it != m.end(); it++)
213 ss << prefix << prefix
214 <<
"<" << it->first <<
">"
216 <<
"</" << it->first <<
">" << endl;
219 ss << prefix <<
"</" << tagName <<
">" << endl;
227 string save(
const char* outFile =
"")
229 string logFile = string(outFile);
230 if(logFile.size() == 0)
231 logFile = outputFile;
234 logSS <<
"\t</messages>" << endl;
236 mapToXML<string, unsigned long>(logSS, elementsMap,
"data_structure", 1);
237 mapToXML<string, float>(logSS, timekeeperMap,
"times", 1);
239 logSS <<
"\t<end_time>" << timeStamp() <<
"</end_time>" << endl;
241 logSS <<
"</log>" << endl;
244 logF.open(logFile.c_str(), std::fstream::out | std::fstream::app);
261 int result = _stat( name.c_str(), &buf );
264 int result = stat( name.c_str(), &buf );
266 if(result == 0 && S_ISDIR(buf.st_mode))
279 void dump(
string type,
string msg,
bool autoFormat =
true)
288 ss <<
"[" << type <<
"] ";
292 if(type ==
"WARNING" || type ==
"WARN" || type ==
"FIXME")
294 else if(type ==
"DEBUG")
296 else if(type ==
"ERROR" || type ==
"FAIL" || type ==
"FATAL" || type ==
"ASSERT_FAILURE")
298 else if(type ==
"INFO" || type ==
"EXPECT_FAILURE")
300 else if(type ==
"LOG")
303 for(
unsigned int i = 0; i < msg.size(); ++i)
313 else if(set && !reset)
320 else if(
'\n' == msg[i])
330 logSS << ss.str() << endl;
331 cerr << ss.str() << endl;
344 std::string
log(
string type,
const string& msg)
348 string time = timeStamp();
350 logF.open(outputFile.c_str(), std::fstream::out | std::fstream::app);
351 ss <<
"<" << type <<
" time=\"" << time <<
"\">";
352 ss << msg <<
"</" << type <<
">" << endl;
355 string newmsg = ss.str();
string mapToString(const map< A, B > &m, string title="") const
SimpleLogger()
Constructor of logger. The wrapper script of moose must make sure that $HOME/.moose is created...
void updateGlobalCount(string type)
When an element is created in moose, log its presense in this map.
string dumpStats(int which)
Dump statistics onto console.
map< string, unsigned long > elementsMap
string save(const char *outFile="")
Convert this logger to XML.
vector< float > simulationTime
void mapToXML(ostringstream &ss, const map< A, B > &m, const char *tagName, unsigned indent) const
Converts a map to XML like staructure.
map< string, float > timekeeperMap
bool isDir(const std::string &name)
Checks if given directory path exists on system.
vector< float > initializationTime
const std::string timeStamp()
Get current timestamp.
void dump(string type, string msg, bool autoFormat=true)
Dumps a message to console.
vector< float > creationTime
std::string log(string type, const string &msg)
Compose a message and log to logging file.