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