]> vaikene.ee Git - evaf/blob - src/libs/Common/logger.h
1dfdc62408a805f0ed985b93b36eb638898bbdc8
[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 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
31 class QThread;
32
33
34 namespace eVaf {
35 namespace Common {
36 namespace Internal {
37
38 /// Default fatal error message handler
39 void defFatalMsgHandler(QString const & msg, QString const & source, QString const & where);
40
41 /**
42 * Logger source.
43 *
44 * This class stores information about known sources.
45 */
46 class LoggerSource : public QSharedData
47 {
48 public:
49
50 LoggerSource();
51
52 LoggerSource(LoggerSource const & o);
53
54 /**
55 * Initializes the source
56 * @param source Name of the source
57 * @param logDir Full path to the log directory
58 * @param etcDir Full path to the configuration files directory
59 *
60 * This function initializes the newly created logger source and sets initial
61 * parameters for the source.
62 *
63 * Default parameters:
64 * @li severity is set to Fatal
65 * @li maximum size of the log file is set to 100 KiB
66 * @li maximum number of log files is set to 3
67 *
68 * Default parameters can be overwritten with values read from the logger.ini file.
69 * This file should have the [.default] section with new default values for all the
70 * sources. Individual sources can have their parameters changed in sections with the
71 * name of the source.
72 *
73 * Example logger.ini file:
74 * @code
75 * [.default]
76 * severity = Fatal
77 * maxSize = 100
78 * maxCount = 3
79 *
80 * [my-source]
81 * severity = Warning
82 * maxSize = 1000
83 * maxCount = 10
84 * @endcode
85 */
86 void init(QString const & source, QString const & logDir, QString const & etcDir);
87
88
89 public: // Members (we don't bother adding getter/setter functions)
90
91 /// Current severity level
92 iLogger::Severity severity;
93
94 /// Current log file name
95 QString fileName;
96
97 /// Current maximum size of log files
98 uint maxSize;
99
100 /// Current maximum number of log files
101 uint maxCount;
102
103 };
104
105 /**
106 * Worker class for the logger.
107 *
108 * This class separates potentially expensive I/O from the iLogger interface making sure
109 * that writing to the log file does not block the main thread.
110 */
111 class LoggerWorker : public QObject
112 {
113 Q_OBJECT
114
115 public slots:
116
117 /**
118 * Writes a message to the log file
119 * @param src The logger source
120 * @param msg The message
121 *
122 * This function writes the message to the log file. It also controls the size and
123 * number of log files.
124 */
125 void writeToLogFile(LoggerSource const & src, QString const & msg);
126
127 };
128
129 /**
130 * iLogger interface implementation.
131 *
132 * This class implements the iLogger interface.
133 */
134 class Logger : public iLogger
135 {
136 Q_OBJECT
137
138 public:
139
140 Logger();
141
142 virtual ~Logger();
143
144 /**
145 * Initializes the interface implementation
146 * @return True if ok; false if initialization failed
147 */
148 bool init();
149
150 /*
151 iLogger interface
152 */
153
154 virtual QString defaultSource() const { return mDefaultSource; }
155
156 virtual void setDefaultSource(QString const & source);
157
158 virtual iLogger::Severity severity(QString const & source = 0);
159
160 virtual void setSeverity(iLogger::Severity severity, QString const & source = 0);
161
162 virtual uint maxSize(QString const & source = 0);
163
164 virtual void setMaxSize(uint maxSize, QString const & source = 0);
165
166 virtual uint maxCount(QString const & source = 0);
167
168 virtual void setMaxCount(uint maxCount, QString const & source = 0);
169
170 virtual iLogger::Severity consoleSeverity() const { return mConsoleSeverity; }
171
172 virtual void setConsoleSeverity(iLogger::Severity severity);
173
174 virtual void write(Severity severity, QString const & msg, QString const & source = 0, QString const & where = 0);
175
176 virtual QString printf(char const * const fmt, ...) const;
177
178 virtual QString printable(QByteArray const & msg) const;
179
180 virtual FatalMsgHandler installFatalMsgHandler(FatalMsgHandler newHandler);
181
182
183 signals:
184
185 void writeToLogFile(LoggerSource const & src, QString const & msg);
186
187
188 private: // Members
189
190 /// Current fatal error message handler
191 FatalMsgHandler mFatalMsgHandler;
192
193 /// Console output severity level
194 iLogger::Severity mConsoleSeverity;
195
196 /// Current default source (defaults to "evaf")
197 QString mDefaultSource;
198
199 /// Logger sources
200 QHash<QString, QExplicitlySharedDataPointer<LoggerSource> > mSources;
201
202 /// Worker thread
203 QThread * mThread;
204
205 /// Worker object
206 LoggerWorker * mWorker;
207
208
209 private: // Methods
210
211 /// Returns the source by the name. The source is created if it does not exist yet.
212 LoggerSource * getSource(QString const & name);
213
214 #ifdef Q_OS_WIN32
215 /// Changes text colors on the Windows console
216 void setColor(short int c);
217 #endif
218
219 };
220
221 } // namespace eVaf::Common::Internal
222 } // namespace eVaf::Common
223 } // namespace eVaf
224
225 #endif // logger.h