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