EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairLogger.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairLogger.h
1 
2 /*
3  * FairLogger.h
4  *
5  * Created on: Mar 03, 2011
6  * Author: f.uhlig
7  */
8 
9 #ifndef BASE_FAIRLOGGER_H_
10 #define BASE_FAIRLOGGER_H_
11 
12 #define IMP_CONVERTTOSTRING(s) # s
13 #define CONVERTTOSTRING(s) IMP_CONVERTTOSTRING(s)
14 #define MESSAGE_ORIGIN __FILE__, CONVERTTOSTRING(__LINE__), __FUNCTION__
15 
16 #define FAIRLOG(level) \
17  !(gLogger->IsLogNeeded(level)) ? gLogger->GetNullStream(level) : gLogger->GetOutputStream(level, MESSAGE_ORIGIN)
18 
19 #define FAIRLOG_IF(level, condition) \
20  !(condition) ? gLogger->GetNullStream(level) : LOG(level)
21 
22 #include "Rtypes.h"
23 
24 #include <ostream>
25 #include <fstream>
26 #include <iomanip>
27 
28 // Definiton of the different log levels
29 // TODO(F.U): Find bettter names for DEBUG1..4
32  };
33 
34 static const char* const LogLevelString[] = { "FATAL", "ERROR", "WARNING",
35  "INFO", "DEBUG", "DEBUG1",
36  "DEBUG2", "DEBUG3", "DEBUG4"
37  };
39  /* Normal Text */
41 
42  /* Foreground Color */
52 };
53 
54 
55 static const char* const LogLevelColor[] = { "\33[01;31m", "\33[01;33m",
56  "\33[00;33m", "\33[01;33m",
57  "\33[01;32m", "\33[01;32m",
58  "\33[01;32m", "\33[01;32m",
59  "\33[01;32m"
60  };
61 
62 // Definition of the different verbosity levels of the output
63 // With the level HIGH the output contains a time stamp, the location from
64 // which the message commes (File, Function and Line) toegether with the
65 // log level and the mesage to be printed.
66 // The verbosity level MEDIUM has the above information without the time stamp
67 // The LOW verbosity level prints only the log level together with the message
69 static const char* const LogVerbosityString[] = { "HIGH", "MEDIUM", "LOW" };
70 
71 class FairLogger : public std::ostream
72 {
73  public:
74  static FairLogger* GetLogger();
75 
76  void SetLogFileName(const char* name);
77 
78  void SetLogToScreen(Bool_t log1) {
79  fLogToScreen = log1;
80  if (!fLogToScreen) {
81  SetLogScreenLevel("FATAL");
82  }
83  }
84 
85  void SetLogToFile(Bool_t log1) {
86  fLogToFile = log1;
87  if (!fLogToFile) {
88  SetLogFileLevel("FATAL");
89  }
90  }
91 
92  void SetColoredLog(Bool_t log1) { fLogColored = log1; }
93 
94  void SetLogFileLevel(const char* level) {
97  }
98 
99  void SetLogScreenLevel(const char* level) {
101  SetMinLogLevel();
102  }
103 
104  void SetLogVerbosityLevel(const char* vlevel) {
106  }
107 
108  Bool_t IsLogNeeded(FairLogLevel logLevel);
109 
110  void Fatal(const char* file, const char* line, const char* func,
111  const char* format, ...);
112 
113 
114  void Error(const char* file, const char* line, const char* func,
115  const char* format, ...);
116 
117  void Warning(const char* file, const char* line, const char* func,
118  const char* format, ...);
119 
120  void Info(const char* file, const char* line, const char* func,
121  const char* format, ...);
122 
123  void Debug(const char* file, const char* line, const char* func,
124  const char* format, ...);
125  void Debug1(const char* file, const char* line, const char* func,
126  const char* format, ...);
127  void Debug2(const char* file, const char* line, const char* func,
128  const char* format, ...);
129  void Debug3(const char* file, const char* line, const char* func,
130  const char* format, ...);
131  void Debug4(const char* file, const char* line, const char* func,
132  const char* format, ...);
133 
134  FairLogger& GetOutputStream(FairLogLevel level, const char* file, const char* line, const char* func);
135 
136  std::ostream& GetNullStream(FairLogLevel level) {
137  fLevel=level;
138  return *fNullStream;
139  }
140 
141  void SetScreenStreamToCerr(bool errorStream);
142 
145  template <class T> FairLogger& operator<<(const T& t) {
146  if (fLogToScreen && fLevel <= fLogScreenLevel) {
147  *(fScreenStream) << t;
148  }
149  if (fLogToFile && fLevel <= fLogFileLevel) {
150  *(fFileStream) << t;
151  }
152  return *this;
153  }
154 
157 #if (__GNUC__ >= 3)
158  FairLogger& operator<<(std::ios_base & (*manip) (std::ios_base&));
159 #endif
160  FairLogger& operator<<(std::ostream & (*manip) (std::ostream&));
161 
164  static std::ostream& endl(std::ostream&);
165 
168  static std::ostream& flush(std::ostream&);
169 
170  private:
172  FairLogger();
173  FairLogger(const FairLogger&);
175  ~FairLogger();
176 
177 
178  void Log(FairLogLevel level, const char* file, const char* line,
179  const char*, const char* format, va_list arglist);
180 
181  void LogFatalMessage(std::ostream& strm);
182 
183  void OpenLogFile();
184  void CloseLogFile();
185 
186  FairLogLevel ConvertToLogLevel(const char* level) const;
187  FairLogVerbosityLevel ConvertToLogVerbosityLevel(const char* level) const;
188 
189  void GetTime();
190 
191  void SetMinLogLevel();
192 
193  const char* ConvertLogLevelToString(FairLogLevel level) const
194  { return LogLevelString[level]; }
195 
196  const char* fLogFileName;
197  Bool_t fLogToScreen;
198  Bool_t fLogToFile;
199  Bool_t fLogColored;
203  Int_t fBufferSize;
205  std::vector<char> fDynamicBuffer;
207 
208  static const int fgkTimeBufferLength = 80;
212  std::ostream* fScreenStream;
213  std::ostream* fFileStream;
214  std::ostream* fNullStream;
215  Bool_t fLogFileOpen;
216  Bool_t fIsNewLine;
217  ClassDef(FairLogger, 3)
218 };
219 
220 extern FairLogger* gLogger;
221 
222 #endif // BASE_FAIRLOGGER_H_