QwAnalysis
QwLog.h
Go to the documentation of this file.
1 /*!
2  * \file QwLog.h
3  * \brief A logfile class, based on an identical class in the Hermes analyzer
4  *
5  * \author Wouter Deconinck
6  * \date 2009-11-25
7  */
8 
9 #ifndef QWLOG_HH
10 #define QWLOG_HH
11 
12 // System headers
13 #include <iostream>
14 #include <iomanip>
15 #include <string>
16 #include <vector>
17 using std::string;
18 
19 // Qweak headers
20 #include "QwTypes.h"
21 #include "QwColor.h"
22 
23 /*!
24  * \note Because QwOptions depends on QwLog, and QwLog depends also on QwOptions,
25  * we cannot include QwOptions in the QwLog header file here. QwLog is treated
26  * as more basic than QwOptions.
27  */
28 
29 // Forward declarations
30 class QwOptions;
31 
32 /*! \def QwOut
33  * \brief Predefined log drain for explicit output
34  */
35 #define QwOut gQwLog(QwLog::kAlways,__PRETTY_FUNCTION__)
36 
37 /*! \def QwError
38  * \brief Predefined log drain for errors
39  */
40 #define QwError gQwLog(QwLog::kError,__PRETTY_FUNCTION__)
41 
42 /*! \def QwWarning
43  * \brief Predefined log drain for warnings
44  */
45 #define QwWarning gQwLog(QwLog::kWarning,__PRETTY_FUNCTION__)
46 
47 /*! \def QwMessage
48  * \brief Predefined log drain for regular messages
49  */
50 #define QwMessage gQwLog(QwLog::kMessage,__PRETTY_FUNCTION__)
51 
52 /*! \def QwVerbose
53  * \brief Predefined log drain for verbose messages
54  */
55 #define QwVerbose gQwLog(QwLog::kVerbose,__PRETTY_FUNCTION__)
56 
57 /*! \def QwDebug
58  * \brief Predefined log drain for debugging output
59  */
60 #define QwDebug gQwLog(QwLog::kDebug,__PRETTY_FUNCTION__)
61 
62 
63 /**
64  * \class QwLog
65  * \ingroup QwAnalysis
66  * \brief A logfile class
67  *
68  * This class should not be used directly. Instead one can write text to the
69  * screen or a log file via the predefined log drains QwError, QwWarning,
70  * QwMessage, QwVerbose and QwDebug. A special log drain QwOut will always
71  * be printed and is reserved for output \b explicitly requested by the user.
72  *
73  * An example could be:
74 \verbatim
75  QwMessage << "Hello World !!!" << QwLog::endl;
76 \endverbatim
77  */
78 class QwLog : public std::ostream {
79 
80  public:
81 
82  /// \brief Define available class options for QwOptions
83  static void DefineOptions(QwOptions* options);
84  /// \brief Process class options for QwOptions
85  void ProcessOptions(QwOptions* options);
86  // Note: this uses pointers as opposed to references, because as indicated
87  // above the QwLog class cannot depend on the QwOptions class. When using a
88  // pointer we only need a forward declaration and we do not need to include
89  // the header file QwOptions.h.
90 
91  //! Loglevels
92  /*! enum of possible log levels */
93  enum QwLogLevel {
94  kAlways = -1, /*!< Explicit output */
95  kError = 0, /*!< Error loglevel */
96  kWarning = 1, /*!< Warning loglevel */
97  kMessage = 2, /*!< Message loglevel */
98  kVerbose = 3, /*!< Verbose loglevel */
99  kDebug = 4 /*!< Debug loglevel */
100  };
101 
102  //! Log file open modes
103  static const std::ios_base::openmode kTruncate;
104  static const std::ios_base::openmode kAppend;
105 
106  /*! \brief The constructor
107  */
108  QwLog();
109 
110  /*! \brief The destructor
111  */
112  virtual ~QwLog();
113 
114  /*! \brief Determine whether the function name matches a specified list of regular expressions
115  */
116  bool IsDebugFunction(const string func_name);
117 
118  /*! \brief Initialize the log file with name 'name'
119  */
120  void InitLogFile(const std::string name, const std::ios_base::openmode mode = kAppend);
121 
122  /*! \brief Set the screen color mode
123  */
124  void SetScreenColor(bool flag);
125 
126  /*! \brief Set the screen log level
127  */
128  void SetScreenThreshold(int thr);
129 
130  /*! \brief Set the file log level
131  */
132  void SetFileThreshold(int thr);
133 
134  /*! \brief Set the stream log level
135  */
136  QwLog& operator()(const QwLogLevel level,
137  const std::string func_sig = "<unknown>");
138 
139  /*! \brief Stream an object to the output stream
140  */
141  template <class T> QwLog& operator<<(const T &t) {
142  if (fScreen && fLogLevel <= fScreenThreshold) {
143  *(fScreen) << t;
144  }
145  if (fFile && fLogLevel <= fFileThreshold) {
146  *(fFile) << t;
147  }
148  return *this;
149  }
150 
151  /*! \brief Pass the ios_base manipulators
152  */
153 #if (__GNUC__ >= 3)
154  QwLog& operator<<(std::ios_base & (*manip) (std::ios_base &));
155 #endif
156  QwLog& operator<<(std::ostream & (*manip) (std::ostream &));
157 
158  /*! \brief End of the line
159  */
160  static std::ostream& endl(std::ostream&);
161 
162  /*! \brief Flush the streams
163  */
164  static std::ostream& flush(std::ostream&);
165 
166  private:
167 
168  /*! \brief Get the local time
169  */
170  const char* GetTime();
171  char fTimeString[128];
172 
173  //! Screen thresholds and stream
175  std::ostream *fScreen;
176  //! File thresholds and stream
178  std::ostream *fFile;
179  //! Log level of this stream
181 
182  //! Flag to print function signature on warning or error
184 
185  //! List of regular expressions for functions that will have increased log level
186  std::map<std::string,bool> fIsDebugFunction;
187  std::vector<std::string> fDebugFunctionRegexString;
188 
189  //! Flag to disable color
190  bool fUseColor;
191 
192  //! Flags only relevant for current line, but static for use in static function
193  static bool fFileAtNewLine;
194  static bool fScreenInColor;
195  static bool fScreenAtNewLine;
196 
197 };
198 
199 extern QwLog gQwLog;
200 
201 #endif
void InitLogFile(const std::string name, const std::ios_base::openmode mode=kAppend)
Initialize the log file with name &#39;name&#39;.
Definition: QwLog.cc:157
std::vector< std::string > fDebugFunctionRegexString
Definition: QwLog.h:187
QwLogLevel fFileThreshold
File thresholds and stream.
Definition: QwLog.h:177
static bool fScreenAtNewLine
Definition: QwLog.h:195
void SetScreenThreshold(int thr)
Set the screen log level.
Definition: QwLog.cc:178
A logfile class.
Definition: QwLog.h:78
An options class.
Definition: QwOptions.h:133
static bool fFileAtNewLine
Flags only relevant for current line, but static for use in static function.
Definition: QwLog.h:193
QwLogLevel fScreenThreshold
Screen thresholds and stream.
Definition: QwLog.h:174
QwLog()
The constructor.
Definition: QwLog.cc:35
std::ostream * fScreen
Definition: QwLog.h:175
static const std::ios_base::openmode kAppend
Definition: QwLog.h:104
void ProcessOptions(QwOptions *options)
Process class options for QwOptions.
Definition: QwLog.cc:108
QwLog & operator<<(const T &t)
Stream an object to the output stream.
Definition: QwLog.h:141
static const std::ios_base::openmode kTruncate
Log file open modes.
Definition: QwLog.h:103
char fTimeString[128]
Definition: QwLog.h:171
void SetScreenColor(bool flag)
Set the screen color mode.
Definition: QwLog.cc:171
const char * GetTime()
Get the local time.
Definition: QwLog.cc:332
void SetFileThreshold(int thr)
Set the file log level.
Definition: QwLog.cc:185
QwLogLevel
Loglevels.
Definition: QwLog.h:93
virtual ~QwLog()
The destructor.
Definition: QwLog.cc:53
std::map< std::string, bool > fIsDebugFunction
List of regular expressions for functions that will have increased log level.
Definition: QwLog.h:186
bool fPrintFunctionSignature
Flag to print function signature on warning or error.
Definition: QwLog.h:183
static void DefineOptions(QwOptions *options)
Define available class options for QwOptions.
Definition: QwLog.cc:73
QwLogLevel fLogLevel
Log level of this stream.
Definition: QwLog.h:180
static const double T
Magnetic field: base unit is T.
Definition: QwUnits.h:111
QwLog & operator()(const QwLogLevel level, const std::string func_sig="<unknown>")
Set the stream log level.
Definition: QwLog.cc:192
std::ostream * fFile
Definition: QwLog.h:178
bool fUseColor
Flag to disable color.
Definition: QwLog.h:190
static bool fScreenInColor
Definition: QwLog.h:194
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
bool IsDebugFunction(const string func_name)
Determine whether the function name matches a specified list of regular expressions.
Definition: QwLog.cc:137
QwLog gQwLog
Definition: QwLog.cc:22
static std::ostream & flush(std::ostream &)
Flush the streams.
Definition: QwLog.cc:319