MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SimpleLogger Class Reference

#include <simple_logger.hpp>

+ Collaboration diagram for SimpleLogger:

Public Member Functions

void dump (string type, string msg, bool autoFormat=true)
 Dumps a message to console. More...
 
string dumpStats (int which)
 Dump statistics onto console. More...
 
bool isDir (const std::string &name)
 Checks if given directory path exists on system. More...
 
std::string log (string type, const string &msg)
 Compose a message and log to logging file. More...
 
template<typename A , typename B >
string mapToString (const map< A, B > &m, string title="") const
 
template<typename A , typename B >
void mapToXML (ostringstream &ss, const map< A, B > &m, const char *tagName, unsigned indent) const
 Converts a map to XML like staructure. More...
 
string save (const char *outFile="")
 Convert this logger to XML. More...
 
 SimpleLogger ()
 Constructor of logger. The wrapper script of moose must make sure that $HOME/.moose is created. The logger will write to $HOME/.moose/log. More...
 
const std::string timeStamp ()
 Get current timestamp. More...
 
void updateGlobalCount (string type)
 When an element is created in moose, log its presense in this map. More...
 
 ~SimpleLogger ()
 

Public Attributes

vector< float > creationTime
 
string endTime
 
string homeDir
 
vector< float > initializationTime
 
ostringstream logSS
 
string mooseDir
 
string outputFile
 
vector< float > simulationTime
 
string startTime
 

Private Attributes

map< string, unsigned long > elementsMap
 
map< string, float > timekeeperMap
 

Detailed Description

Definition at line 69 of file simple_logger.hpp.

Constructor & Destructor Documentation

SimpleLogger::SimpleLogger ( )
inline

Constructor of logger. The wrapper script of moose must make sure that $HOME/.moose is created. The logger will write to $HOME/.moose/log.

Definition at line 78 of file simple_logger.hpp.

79  {
80  startTime = timeStamp();
81  homeDir = getenv("HOME");
82 
83  logSS << "<log simulator=\"moose\">" << endl;
84  logSS << "\t<start_time>" << startTime << "</start_time>" << endl;
85  logSS << "\t<messages>" << endl;
86 
87 #ifdef OS_WINDOWS
88  outputFile = homeDir + "\\.moose\\log";
89 #else
90  outputFile = homeDir + "/.moose/log";
91 #endif
92  }
ostringstream logSS
const std::string timeStamp()
Get current timestamp.
SimpleLogger::~SimpleLogger ( )
inline

Definition at line 94 of file simple_logger.hpp.

95  {
96 
97  }

Member Function Documentation

void SimpleLogger::dump ( string  type,
string  msg,
bool  autoFormat = true 
)
inline

Dumps a message to console.

Parameters
typeType of message e.g. WARN, ERROR, FATAL etc.
msgMessage to dump.
autoFormatUse ` to demarcate coloring of output.

Definition at line 279 of file simple_logger.hpp.

References T_BLUE, T_CYAN, T_GREEN, T_MAGENTA, T_RED, T_RESET, and T_YELLOW.

280  {
281 
282 #if VERBOSITY < 0
283  return;
284 #endif
285 
286  stringstream ss;
287  ss.precision( 12 );
288  ss << "[" << type << "] ";
289  bool set = false;
290  bool reset = true;
291  string color = T_GREEN;
292  if(type == "WARNING" || type == "WARN" || type == "FIXME")
293  color = T_YELLOW;
294  else if(type == "DEBUG")
295  color = T_CYAN;
296  else if(type == "ERROR" || type == "FAIL" || type == "FATAL" || type == "ASSERT_FAILURE")
297  color = T_RED;
298  else if(type == "INFO" || type == "EXPECT_FAILURE")
299  color = T_MAGENTA;
300  else if(type == "LOG")
301  color = T_BLUE;
302 
303  for(unsigned int i = 0; i < msg.size(); ++i)
304  {
305  if('`' == msg[i])
306  {
307  if(!set and reset)
308  {
309  set = true;
310  reset = false;
311  ss << color;
312  }
313  else if(set && !reset)
314  {
315  reset = true;
316  set = false;
317  ss << T_RESET;
318  }
319  }
320  else if('\n' == msg[i])
321  ss << "\n + ";
322  else
323  ss << msg[i];
324  }
325 
326  /* Be safe than sorry */
327  if(!reset)
328  ss << T_RESET;
329 
330  logSS << ss.str() << endl;
331  cerr << ss.str() << endl;
332  }
#define T_MAGENTA
#define T_CYAN
#define T_BLUE
ostringstream logSS
#define T_YELLOW
#define T_GREEN
#define T_RED
#define T_RESET
string SimpleLogger::dumpStats ( int  which)
inline

Dump statistics onto console.

Parameters
whichIf which is 0 then print elements inside moose, if it is 1 then print total time taken during simulation.
Returns

Definition at line 159 of file simple_logger.hpp.

160  {
161  stringstream ss;
162  // unsigned width = 50;
163  ss << endl;
164  if(which == 0)
165  ss << mapToString<string, unsigned long>(elementsMap, "data_structure");
166 
167  else if( which == 1)
168  {
169  timekeeperMap["Simulation"] = accumulate(
170  simulationTime.begin()
171  , simulationTime.end()
172  , 0.0
173  );
174  timekeeperMap["Initialization"] = accumulate(
175  initializationTime.begin()
176  , initializationTime.end()
177  , 0.0
178  );
179  timekeeperMap["Creation"] = accumulate(
180  creationTime.begin()
181  , creationTime.end()
182  , 0.0
183  );
184 
185  ss << mapToString<string, float>( timekeeperMap, "simulation_stats" );
186  }
187  return ss.str();
188  }
map< string, unsigned long > elementsMap
vector< float > simulationTime
map< string, float > timekeeperMap
vector< float > initializationTime
vector< float > creationTime
bool SimpleLogger::isDir ( const std::string &  name)
inline

Checks if given directory path exists on system.

Parameters
nameDirectory path as string.
Returns
true, if directory exists, false otherwise.

Definition at line 257 of file simple_logger.hpp.

258  {
259 #ifdef OS_WINDOWS
260  struct _stat buf;
261  int result = _stat( name.c_str(), &buf );
262 #else
263  struct stat buf;
264  int result = stat( name.c_str(), &buf );
265 #endif
266  if(result == 0 && S_ISDIR(buf.st_mode))
267  return true;
268  else
269  return false;
270  }
static char name[]
Definition: mfield.cpp:401
std::string SimpleLogger::log ( string  type,
const string &  msg 
)
inline

Compose a message and log to logging file.

Parameters
type
msg
level
Returns

Definition at line 344 of file simple_logger.hpp.

345  {
346 #ifdef ENABLE_LOGGER
347  stringstream ss;
348  string time = timeStamp();
349  fstream logF;
350  logF.open(outputFile.c_str(), std::fstream::out | std::fstream::app);
351  ss << "<" << type << " time=\"" << time << "\">";
352  ss << msg << "</" << type << ">" << endl;
353  logF << ss.str();
354  logF.close();
355  string newmsg = ss.str();
356  return newmsg;
357 #else
358  return string("");
359 #endif
360 
361  }
const std::string timeStamp()
Get current timestamp.
template<typename A , typename B >
string SimpleLogger::mapToString ( const map< A, B > &  m,
string  title = "" 
) const
inline

Definition at line 130 of file simple_logger.hpp.

131  {
132  unsigned width = 50;
133  stringstream ss;
134 
135  ss << title;
136  for(unsigned i = 0; i < width - title.size(); ++i)
137  ss << "~";
138  ss << endl;
139 
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;
143 
144  for(unsigned i = 0; i < width; ++i)
145  ss << "=";
146 
147  ss << endl;
148  return ss.str();
149  }
template<typename A , typename B >
void SimpleLogger::mapToXML ( ostringstream &  ss,
const map< A, B > &  m,
const char *  tagName,
unsigned  indent 
) const
inline

Converts a map to XML like staructure.

Template Parameters
A
B
Parameters
ssInput ostringstream.
mstd::map
tagNameThis is current tag name. Useful when recursing.
indentUsually a tag character.

Definition at line 202 of file simple_logger.hpp.

204  {
205  string prefix = "";
206  for(unsigned int i = 0; i < indent; ++i)
207  prefix += "\t";
208  ss << prefix << "<" << tagName << ">" << endl;
209 
210  typename map<A, B>::const_iterator it;
211  for(it = m.begin(); it != m.end(); it++)
212  {
213  ss << prefix << prefix
214  << "<" << it->first << ">"
215  << it->second
216  << "</" << it->first << ">" << endl;
217  }
218 
219  ss << prefix << "</" << tagName << ">" << endl;
220  }
string SimpleLogger::save ( const char *  outFile = "")
inline

Convert this logger to XML.

Returns
A XML string.

Definition at line 227 of file simple_logger.hpp.

228  {
229  string logFile = string(outFile);
230  if(logFile.size() == 0)
231  logFile = outputFile;
232 
233  // End of messages.
234  logSS << "\t</messages>" << endl;
235 
236  mapToXML<string, unsigned long>(logSS, elementsMap, "data_structure", 1);
237  mapToXML<string, float>(logSS, timekeeperMap, "times", 1);
238 
239  logSS << "\t<end_time>" << timeStamp() << "</end_time>" << endl;
240 
241  logSS << "</log>" << endl;
242 
243  fstream logF;
244  logF.open(logFile.c_str(), std::fstream::out | std::fstream::app);
245  logF << logSS.str();
246  logF.close();
247  return logSS.str();
248  }
map< string, unsigned long > elementsMap
ostringstream logSS
map< string, float > timekeeperMap
const std::string timeStamp()
Get current timestamp.
const std::string SimpleLogger::timeStamp ( )
inline

Get current timestamp.

Returns
A string represeting current timestamp.

Definition at line 104 of file simple_logger.hpp.

105  {
106  time_t now = time(0);
107  struct tm tstruct;
108  char buf[80];
109  tstruct = *localtime(&now);
110  strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
111  return buf;
112  }
void SimpleLogger::updateGlobalCount ( string  type)
inline

When an element is created in moose, log its presense in this map.

Parameters
typeType of element.
pathPath of the element.

Definition at line 121 of file simple_logger.hpp.

122  {
123  if(elementsMap.find(type) == elementsMap.end())
124  elementsMap[type] = 1;
125  else
126  elementsMap[type] = elementsMap[type] + 1;
127  }
map< string, unsigned long > elementsMap

Member Data Documentation

vector<float> SimpleLogger::creationTime

Definition at line 382 of file simple_logger.hpp.

map<string, unsigned long> SimpleLogger::elementsMap
private

Definition at line 365 of file simple_logger.hpp.

string SimpleLogger::endTime

Definition at line 375 of file simple_logger.hpp.

string SimpleLogger::homeDir

Definition at line 371 of file simple_logger.hpp.

vector<float> SimpleLogger::initializationTime

Definition at line 381 of file simple_logger.hpp.

Referenced by Shell::initCinfo().

ostringstream SimpleLogger::logSS

Definition at line 377 of file simple_logger.hpp.

string SimpleLogger::mooseDir

Definition at line 370 of file simple_logger.hpp.

string SimpleLogger::outputFile

Definition at line 373 of file simple_logger.hpp.

vector<float> SimpleLogger::simulationTime

Definition at line 380 of file simple_logger.hpp.

string SimpleLogger::startTime

Definition at line 374 of file simple_logger.hpp.

map<string, float> SimpleLogger::timekeeperMap
private

Definition at line 366 of file simple_logger.hpp.


The documentation for this class was generated from the following file: