2 * @file Common/logger.h
3 * @brief iLogger interface implementation
6 * Copyright (c) 2011-2012 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>
38 /// Default fatal error message handler
39 void defFatalMsgHandler(QString
const & msg
, QString
const & source
, QString
const & where
);
44 * This class stores information about known sources.
46 class LoggerSource
: public QSharedData
52 LoggerSource(LoggerSource
const & o
);
55 * Initializes the source
56 * @param source Name of the source
58 * This function initializes the newly created logger source and sets initial
59 * parameters for the source.
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
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
71 * Example logger.ini file:
84 void init(QString
const & source
);
87 public: // Members (we don't bother adding getter/setter functions)
89 /// Name of the source
92 /// Current severity level
93 iLogger::Severity severity
;
98 /// Current maximum size of log files
101 /// Current maximum number of log files
107 * Worker class for the logger.
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.
112 class LoggerWorker
: public QObject
119 * Writes a message to the log file
120 * @param src The logger source
121 * @param msg The message
123 * This function writes the message to the log file. It also controls the size and
124 * number of log files.
126 void writeToLogFile(LoggerSource
const & src
, QString
const & msg
);
131 * iLogger interface implementation.
133 * This class implements the iLogger interface.
135 class Logger
: public iLogger
146 * Initializes the iLogger interface implementation
147 * @return True if ok; false if initialization failed
155 virtual QString
defaultSource() const;
157 virtual void setDefaultSource(QString
const & source
);
159 virtual iLogger::Severity
severity(QString
const & source
= 0);
161 virtual void setSeverity(iLogger::Severity severity
, QString
const & source
= 0);
163 virtual uint
maxSize(QString
const & source
= 0);
165 virtual void setMaxSize(uint maxSize
, QString
const & source
= 0);
167 virtual uint
maxCount(QString
const & source
= 0);
169 virtual void setMaxCount(uint maxCount
, QString
const & source
= 0);
171 virtual iLogger::Severity
consoleSeverity() const { return mConsoleSeverity
; }
173 virtual void setConsoleSeverity(iLogger::Severity severity
);
175 virtual void write(Severity severity
, QString
const & msg
, QString
const & source
= 0, QString
const & where
= 0);
177 virtual QString
printf(char const * const fmt
, ...) const;
179 virtual QString
printable(QByteArray
const & msg
) const;
181 virtual FatalMsgHandler
installFatalMsgHandler(FatalMsgHandler newHandler
);
186 void writeToLogFile(LoggerSource
const & src
, QString
const & msg
);
191 /// Flag indicating that logger is fully initialized
194 /// Current fatal error message handler
195 FatalMsgHandler mFatalMsgHandler
;
197 /// Console output severity level
198 iLogger::Severity mConsoleSeverity
;
200 /// Default logger source
201 QExplicitlySharedDataPointer
<LoggerSource
> mDefaultSource
;
203 /// Other logger sources
204 QHash
<QString
, QExplicitlySharedDataPointer
<LoggerSource
> > mSources
;
210 LoggerWorker
* mWorker
;
215 /// Returns the source by the name. The source is created if it does not exist yet.
216 LoggerSource
* getSource(QString
const & name
= QString());
219 /// Changes text colors on the Windows console
220 void setColor(short int c
);
225 } // namespace eVaf::Common::Internal
226 } // namespace eVaf::Common