]> vaikene.ee Git - evaf/blob - src/libs/Common/logger.h
Mac OS changes and switched to c++11.
[evaf] / src / libs / Common / logger.h
1 /**
2 * @file Common/logger.h
3 * @brief iLogger interface implementation
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011-2019 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #ifndef __COMMON_LOGGER_H
21 #define __COMMON_LOGGER_H
22
23 #include "ilogger.h"
24
25 #include <QObject>
26 #include <QString>
27 #include <QHash>
28 #include <QExplicitlySharedDataPointer>
29 #include <QSharedData>
30 #include <QScopedPointer>
31
32 class QThread;
33
34
35 namespace eVaf {
36 namespace Common {
37 namespace Internal {
38
39 /// Default fatal error message handler
40 [[noreturn]] void defFatalMsgHandler(QString const & msg, QString const & source, QString const & where);
41
42 /**
43 * Logger source.
44 *
45 * This class stores information about known sources.
46 */
47 class LoggerSource : public QSharedData
48 {
49 public:
50
51 LoggerSource();
52
53 LoggerSource(LoggerSource const & o);
54
55 /**
56 * Initializes the source
57 * @param source Name of the source
58 *
59 * This function initializes the newly created logger source and sets initial
60 * parameters for the source.
61 *
62 * Default parameters:
63 * @li severity is set to Fatal
64 * @li maximum size of the log file is set to 100 KiB
65 * @li maximum number of log files is set to 3
66 *
67 * Default parameters can be overwritten with values read from the logger.ini file.
68 * This file should have the [.default] section with new default values for all the
69 * sources. Individual sources can have their parameters changed in sections with the
70 * name of the source.
71 *
72 * Example logger.ini file:
73 * @code
74 * [.default]
75 * log_level = Fatal
76 * log_size = 100
77 * log_count = 3
78 *
79 * [my-source]
80 * log_level = Warning
81 * log_size = 1000
82 * log_count = 10
83 * @endcode
84 */
85 void init(QString const & source);
86
87
88 public: // Members (we don't bother adding getter/setter functions)
89
90 /// Name of the source
91 QString name;
92
93 /// Current severity level
94 iLogger::Severity severity;
95
96 /// File name
97 QString fileName;
98
99 /// Current maximum size of log files
100 uint maxSize;
101
102 /// Current maximum number of log files
103 uint maxCount;
104
105 };
106
107 /**
108 * Worker class for the logger.
109 *
110 * This class separates potentially expensive I/O from the iLogger interface making sure
111 * that writing to the log file does not block the main thread.
112 */
113 class LoggerWorker : public QObject
114 {
115 Q_OBJECT
116
117 public slots:
118
119 /**
120 * Writes a message to the log file
121 * @param src The logger source
122 * @param msg The message
123 *
124 * This function writes the message to the log file. It also controls the size and
125 * number of log files.
126 */
127 void writeToLogFile(LoggerSource const & src, QString const & msg);
128
129 };
130
131 /**
132 * iLogger interface implementation.
133 *
134 * This class implements the iLogger interface.
135 */
136 class Logger : public iLogger
137 {
138 Q_OBJECT
139
140 public:
141
142 /**
143 * Destroys the iLogger interface instance
144 */
145 static void destroyInstance();
146
147 Logger();
148
149 virtual ~Logger();
150
151 /**
152 * Initializes the iLogger interface implementation
153 * @return True if ok; false if initialization failed
154 */
155 bool init();
156
157 /*
158 iLogger interface
159 */
160
161 virtual QString defaultSource() const;
162
163 virtual void setDefaultSource(QString const & source);
164
165 virtual iLogger::Severity severity(QString const & source = QString());
166
167 virtual void setSeverity(iLogger::Severity severity, QString const & source = QString());
168
169 virtual uint maxSize(QString const & source = QString());
170
171 virtual void setMaxSize(uint maxSize, QString const & source = QString());
172
173 virtual uint maxCount(QString const & source = QString());
174
175 virtual void setMaxCount(uint maxCount, QString const & source = QString());
176
177 virtual iLogger::Severity consoleSeverity() const { return mConsoleSeverity; }
178
179 virtual void setConsoleSeverity(iLogger::Severity severity);
180
181 virtual void write(Severity severity, QString const & msg, QString const & source = QString(), QString const & where = QString());
182
183 virtual QString printf(char const * const fmt, ...) const;
184
185 virtual QString printable(QByteArray const & msg) const;
186
187 virtual FatalMsgHandler installFatalMsgHandler(FatalMsgHandler newHandler);
188
189
190 signals:
191
192 void writeToLogFile(LoggerSource const & src, QString const & msg);
193
194
195 private: // Members
196
197 /// Flag indicating that logger is fully initialized
198 bool mReady;
199
200 /// Current fatal error message handler
201 FatalMsgHandler mFatalMsgHandler;
202
203 /// Console output severity level
204 iLogger::Severity mConsoleSeverity;
205
206 /// Default logger source
207 QExplicitlySharedDataPointer<LoggerSource> mDefaultSource;
208
209 /// Other logger sources
210 QHash<QString, QExplicitlySharedDataPointer<LoggerSource> > mSources;
211
212 /// Worker thread
213 QScopedPointer<QThread> mThread;
214
215 /// Worker object
216 QScopedPointer<LoggerWorker> mWorker;
217
218
219 private: // Methods
220
221 /// Returns the source by the name. The source is created if it does not exist yet.
222 LoggerSource * getSource(QString const & name = QString());
223
224 #ifdef Q_OS_WIN32
225 /// Changes text colors on the Windows console
226 void setColor(short int c);
227 #endif
228
229 };
230
231 } // namespace eVaf::Common::Internal
232 } // namespace eVaf::Common
233 } // namespace eVaf
234
235 #endif // logger.h