2 * @file Common/logger.h
3 * @brief iLogger interface implementation
6 * Copyright (c) 2011-2019 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
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.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
20 #ifndef __COMMON_LOGGER_H
21 #define __COMMON_LOGGER_H
28 #include <QExplicitlySharedDataPointer>
29 #include <QSharedData>
30 #include <QScopedPointer>
39 /// Default fatal error message handler
40 [[noreturn]] void defFatalMsgHandler(QString
const & msg
, QString
const & source
, QString
const & where
);
45 * This class stores information about known sources.
47 class LoggerSource
: public QSharedData
53 LoggerSource(LoggerSource
const & o
);
56 * Initializes the source
57 * @param source Name of the source
59 * This function initializes the newly created logger source and sets initial
60 * parameters for the source.
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
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
72 * Example logger.ini file:
85 void init(QString
const & source
);
88 public: // Members (we don't bother adding getter/setter functions)
90 /// Name of the source
93 /// Current severity level
94 iLogger::Severity severity
;
99 /// Current maximum size of log files
102 /// Current maximum number of log files
108 * Worker class for the logger.
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.
113 class LoggerWorker
: public QObject
120 * Writes a message to the log file
121 * @param src The logger source
122 * @param msg The message
124 * This function writes the message to the log file. It also controls the size and
125 * number of log files.
127 void writeToLogFile(LoggerSource
const & src
, QString
const & msg
);
132 * iLogger interface implementation.
134 * This class implements the iLogger interface.
136 class Logger
: public iLogger
143 * Destroys the iLogger interface instance
145 static void destroyInstance();
152 * Initializes the iLogger interface implementation
153 * @return True if ok; false if initialization failed
161 virtual QString
defaultSource() const;
163 virtual void setDefaultSource(QString
const & source
);
165 virtual iLogger::Severity
severity(QString
const & source
= QString());
167 virtual void setSeverity(iLogger::Severity severity
, QString
const & source
= QString());
169 virtual uint
maxSize(QString
const & source
= QString());
171 virtual void setMaxSize(uint maxSize
, QString
const & source
= QString());
173 virtual uint
maxCount(QString
const & source
= QString());
175 virtual void setMaxCount(uint maxCount
, QString
const & source
= QString());
177 virtual iLogger::Severity
consoleSeverity() const { return mConsoleSeverity
; }
179 virtual void setConsoleSeverity(iLogger::Severity severity
);
181 virtual void write(Severity severity
, QString
const & msg
, QString
const & source
= QString(), QString
const & where
= QString());
183 virtual QString
printf(char const * const fmt
, ...) const;
185 virtual QString
printable(QByteArray
const & msg
) const;
187 virtual FatalMsgHandler
installFatalMsgHandler(FatalMsgHandler newHandler
);
192 void writeToLogFile(LoggerSource
const & src
, QString
const & msg
);
197 /// Flag indicating that logger is fully initialized
200 /// Current fatal error message handler
201 FatalMsgHandler mFatalMsgHandler
;
203 /// Console output severity level
204 iLogger::Severity mConsoleSeverity
;
206 /// Default logger source
207 QExplicitlySharedDataPointer
<LoggerSource
> mDefaultSource
;
209 /// Other logger sources
210 QHash
<QString
, QExplicitlySharedDataPointer
<LoggerSource
> > mSources
;
213 QScopedPointer
<QThread
> mThread
;
216 QScopedPointer
<LoggerWorker
> mWorker
;
221 /// Returns the source by the name. The source is created if it does not exist yet.
222 LoggerSource
* getSource(QString
const & name
= QString());
225 /// Changes text colors on the Windows console
226 void setColor(short int c
);
231 } // namespace eVaf::Common::Internal
232 } // namespace eVaf::Common