SLIC  6.1.1
Simulation for the Linear Collider (SLIC) - Geant4 simulation application
LogStream.hh
Go to the documentation of this file.
1 #ifndef SLIC_LOGSTREAM_HH_
2 #define SLIC_LOGSTREAM_HH_ 1
3 
4 // LCDD
5 #include "TimeUtil.hh"
6 
7 // STL
8 #include <iostream>
9 #include <fstream>
10 #include <iomanip>
11 #include <string>
12 #include <vector>
13 
14 using std::ofstream;
15 using std::string;
16 using std::ostream;
17 using std::ios;
18 using std::ios_base;
19 
20 namespace LOG {
21 
25 enum EManip {
26  done = 101, head = 102, time = 103, level = 104, name = 105, sep = 106, endl = 107
27 };
28 
32 enum ELevel {
33  none = 0, always = 1, fatal = 2, error = 3, warning = 4, okay = 5, verbose = 6, debug = 7, nlevels = 8
34 };
35 }
36 
40 static std::string levelNames[LOG::nlevels] = { "NONE", "ALWAYS", "FATAL", "ERROR", "WARNING", "OKAY", "VERBOSE", "DEBUG" };
41 
42 namespace slic {
43 
71 class LogStream {
72 
73  protected:
74 
81  LogStream(std::string name, LOG::ELevel level, ostream* os = &std::cout) :
83  ;
84  }
85 
90  m_name("NONE"), m_outputLevel(LOG::okay), m_currentMessageLevel(LOG::none), m_os(&std::cout), m_active(true), m_fos(0) {
91  ;
92  }
93 
97  virtual ~LogStream() {
98  }
99 
103  friend class LogManager;
104 
105  public:
106 
111  const std::string& getName() const {
112  return m_name;
113  }
114 
119  bool isActive() const {
120  return m_active;
121  }
122 
128  return m_outputLevel;
129  }
130 
135  void setOutputLevel(const LOG::ELevel& outputLevel) {
136  m_outputLevel = outputLevel;
137  }
138 
143  ostream& getOutputStream() const {
144  return *m_os;
145  }
146 
152  return m_currentMessageLevel;
153  }
154 
161  }
162 
171 
173 
174  if (getCurrentLevel() <= getOutputLevel()) {
175  // Set active state.
176  setActive(true);
177  } else {
178  // Set inactive state.
179  setActive(false);
180  }
181  return *this;
182  }
183 
190  if (isActive()) {
191  // End of message.
192  if (x == LOG::done) {
194  // Print full header info.
195  } else if (x == LOG::head) {
196  getOutputStream() << TimeUtil::getAscTime() << " :: " << levelNames[m_currentMessageLevel] << " :: " << getName() << " :: ";
197  // Print current time.
198  } else if (x == LOG::time) {
200  // Print current message level.
201  } else if (x == LOG::level) {
203  // Print logger name.
204  } else if (x == LOG::name) {
205  getOutputStream() << getName();
206  } else if (x == LOG::sep) {
207  getOutputStream() << " :: ";
208  // Print end of line terminator.
209  } else if (x == LOG::endl) {
211  }
212  }
213  return *this;
214  }
215 
220  void setOutputFile(const std::string& file) {
221  m_fos = new ofstream();
222  m_fos->open(file.c_str(), ios::app | ios::out);
223  m_os = m_fos;
224  }
225 
231  return *this << LOG::debug;
232  }
233 
239  return *this << LOG::verbose;
240  }
241 
247  return *this << LOG::okay;
248  }
249 
255  return *this << LOG::warning;
256  }
257 
263  return *this << LOG::error;
264  }
265 
271  return *this << LOG::fatal;
272  }
273 
279  return *this << LOG::always;
280  }
281 
287  void log(const std::string& message, const LOG::ELevel& level) {
288  (*this) << level << message << LOG::done;
289  }
290 
295  void log(const std::string& mesg) {
296  log(mesg, getOutputLevel());
297  }
298 
303  void debug(const std::string& mesg) {
304  log(mesg, LOG::debug);
305  }
306 
311  void verbose(const std::string& mesg) {
312  log(mesg, LOG::verbose);
313  }
314 
319  void okay(const std::string& mesg) {
320  log(mesg, LOG::okay);
321  }
322 
327  void warning(const std::string& mesg) {
328  log(mesg, LOG::warning);
329  }
330 
335  void error(const std::string& mesg) {
336  log(mesg, LOG::error);
337  }
338 
343  void fatal(const std::string& mesg) {
344  log(mesg, LOG::fatal);
345  }
346 
351  void always(const std::string& mesg) {
352  log(mesg, LOG::always);
353  }
354 
360  //LogStream& operator <<(const std::_Setiosflags &manip) {
361  // if (isActive()) {
362  // getOutputStream() << manip;
363  // }
364  // return *this;
365  //}
372  //LogStream& operator <<(const std::_Resetiosflags &manip) {
373  // if (isActive()) {
374  // getOutputStream() << manip;
375  // }
376  // return *this;
377  //}
383  //LogStream& operator <<(const std::_Setbase &manip) {
384  // if (isActive()) {
385  // getOutputStream() << manip;
386  // }
387  // return *this;
388  //}
394  //LogStream& operator <<(const std::_Setprecision &manip) {
395  // if (isActive()) {
396  // getOutputStream() << manip;
397  // }
398  // return *this;
399  //}
405  //LogStream& operator <<(const std::_Setw &manip) {
406  // if (isActive()) {
407  // getOutputStream() << manip;
408  // }
409  // return *this;
410  //}
416  LogStream& operator <<(ios& (*manip)(std::ios&)) {
417  if (isActive()) {
418  getOutputStream() << manip;
419  }
420  return *this;
421  }
422 
428  LogStream& operator <<(std::ios_base& (*manip)(std::ios_base&)) {
429  if (isActive()) {
430  getOutputStream() << manip;
431  }
432  return *this;
433  }
434 
440  template<typename T>
441  LogStream& operator<<(const T& arg) {
442  if (isActive()) {
443  getOutputStream() << arg;
444  }
445  return *this;
446  }
447 
453  template<typename T>
454  LogStream& operator <<(const std::vector<T>& v) {
455  if (isActive()) {
456  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i) {
457  getOutputStream() << *i << " ";
458  }
459  }
460  return *this;
461  }
462 
467  int width() const {
468  return isActive() ? getOutputStream().width() : 0;
469  }
470 
476  int width(int v) {
477  return isActive() ? getOutputStream().width(v) : 0;
478  }
479 
484  long flags() const {
485  return isActive() ? getOutputStream().flags() : 0;
486  }
487 
493  long flags(std::ios_base::fmtflags v) {
494  return isActive() ? getOutputStream().flags(v) : 0;
495  }
496 
502  long setf(std::ios_base::fmtflags v) {
503  return isActive() ? getOutputStream().setf(v) : 0;
504  }
505 
510  char fill() const {
511  return isActive() ? getOutputStream().fill() : -1;
512  }
513 
519  char fill(char v) {
520  return isActive() ? getOutputStream().fill(v) : -1;
521  }
522 
527  int precision() const {
528  return isActive() ? getOutputStream().precision() : 0;
529  }
530 
536  int precision(int v) {
537  return isActive() ? getOutputStream().precision(v) : 0;
538  }
539 
544  int rdstate() const {
545  return isActive() ? getOutputStream().rdstate() : std::ios_base::failbit;
546  }
547 
552  int good() const {
553  return isActive() ? getOutputStream().good() : 0;
554  }
555 
560  int eof() const {
561  return isActive() ? getOutputStream().eof() : 0;
562  }
563 
568  int bad() const {
569  return isActive() ? getOutputStream().bad() : 0;
570  }
571 
578  long setf(std::ios_base::fmtflags _f, std::ios_base::fmtflags _m) {
579  return isActive() ? getOutputStream().setf(_f, _m) : 0;
580  }
581 
586  void unsetf(std::ios_base::fmtflags _l) {
587  if (isActive()) {
588  getOutputStream().unsetf(_l);
589  }
590  }
591 
596  void clear(std::ios_base::iostate _i = std::ios_base::failbit) {
597  if (isActive()) {
598  getOutputStream().clear(_i);
599  }
600  }
601 
602  private:
603 
608  void setActive(bool active) {
609  m_active = active;
610  }
611 
612  private:
613  std::string m_name;
616  ostream* m_os;
617  bool m_active;
618  std::ofstream* m_fos;
619 };
620 }
621 
622 #endif
static std::string levelNames[LOG::nlevels]
Definition: LogStream.hh:40
Manages LogStreams and their associated LogMessenger objects.
Definition: LogManager.hh:31
A class for streaming messages to a log.
Definition: LogStream.hh:71
LogStream(std::string name, LOG::ELevel level, ostream *os=&std::cout)
Definition: LogStream.hh:81
std::ofstream * m_fos
Definition: LogStream.hh:618
LogStream & verbose()
Definition: LogStream.hh:238
void verbose(const std::string &mesg)
Definition: LogStream.hh:311
LOG::ELevel m_outputLevel
Definition: LogStream.hh:614
void log(const std::string &mesg)
Definition: LogStream.hh:295
void clear(std::ios_base::iostate _i=std::ios_base::failbit)
Definition: LogStream.hh:596
LogStream & okay()
Definition: LogStream.hh:246
char fill(char v)
Definition: LogStream.hh:519
void setCurrentLevel(LOG::ELevel level)
Definition: LogStream.hh:159
void warning(const std::string &mesg)
Definition: LogStream.hh:327
bool isActive() const
Definition: LogStream.hh:119
long flags(std::ios_base::fmtflags v)
Definition: LogStream.hh:493
int eof() const
Definition: LogStream.hh:560
long setf(std::ios_base::fmtflags v)
Definition: LogStream.hh:502
void setOutputLevel(const LOG::ELevel &outputLevel)
Definition: LogStream.hh:135
void debug(const std::string &mesg)
Definition: LogStream.hh:303
LOG::ELevel getOutputLevel() const
Definition: LogStream.hh:127
int good() const
Definition: LogStream.hh:552
LogStream & error()
Definition: LogStream.hh:262
int rdstate() const
Definition: LogStream.hh:544
LogStream & fatal()
Definition: LogStream.hh:270
int precision(int v)
Definition: LogStream.hh:536
long setf(std::ios_base::fmtflags _f, std::ios_base::fmtflags _m)
Definition: LogStream.hh:578
void error(const std::string &mesg)
Definition: LogStream.hh:335
int precision() const
Definition: LogStream.hh:527
const std::string & getName() const
Definition: LogStream.hh:111
void setActive(bool active)
Definition: LogStream.hh:608
LOG::ELevel getCurrentLevel() const
Definition: LogStream.hh:151
void okay(const std::string &mesg)
Definition: LogStream.hh:319
void unsetf(std::ios_base::fmtflags _l)
Definition: LogStream.hh:586
void log(const std::string &message, const LOG::ELevel &level)
Definition: LogStream.hh:287
ostream * m_os
Definition: LogStream.hh:616
char fill() const
Definition: LogStream.hh:510
ostream & getOutputStream() const
Definition: LogStream.hh:143
long flags() const
Definition: LogStream.hh:484
virtual ~LogStream()
Definition: LogStream.hh:97
void always(const std::string &mesg)
Definition: LogStream.hh:351
LogStream & operator<<(const LOG::ELevel &level)
Definition: LogStream.hh:170
LogStream & always()
Definition: LogStream.hh:278
int width(int v)
Definition: LogStream.hh:476
LogStream & operator<<(const T &arg)
Definition: LogStream.hh:441
LOG::ELevel m_currentMessageLevel
Definition: LogStream.hh:615
int bad() const
Definition: LogStream.hh:568
int width() const
Definition: LogStream.hh:467
std::string m_name
Definition: LogStream.hh:613
LogStream & warning()
Definition: LogStream.hh:254
LogStream & debug()
Definition: LogStream.hh:230
void fatal(const std::string &mesg)
Definition: LogStream.hh:343
void setOutputFile(const std::string &file)
Definition: LogStream.hh:220
static std::string getAscTime()
Definition: TimeUtil.hh:22
Definition: LogStream.hh:20
ELevel
Definition: LogStream.hh:32
@ okay
Definition: LogStream.hh:33
@ debug
Definition: LogStream.hh:33
@ fatal
Definition: LogStream.hh:33
@ warning
Definition: LogStream.hh:33
@ always
Definition: LogStream.hh:33
@ nlevels
Definition: LogStream.hh:33
@ none
Definition: LogStream.hh:33
@ error
Definition: LogStream.hh:33
@ verbose
Definition: LogStream.hh:33
EManip
Definition: LogStream.hh:25
@ head
Definition: LogStream.hh:26
@ name
Definition: LogStream.hh:26
@ time
Definition: LogStream.hh:26
@ endl
Definition: LogStream.hh:26
@ level
Definition: LogStream.hh:26
@ done
Definition: LogStream.hh:26
@ sep
Definition: LogStream.hh:26