2 * @file Common/logger.h
3 * @brief iLogger interface implementation
6 * Copyright (c) 2011 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
57 * @param logDir Full path to the log directory
58 * @param etcDir Full path to the configuration files directory
60 * This function initializes the newly created logger source and sets initial
61 * parameters for the source.
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
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
73 * Example logger.ini file:
86 void init(QString
const & source
, QString
const & logDir
, QString
const & etcDir
);
89 public: // Members (we don't bother adding getter/setter functions)
91 /// Current severity level
92 iLogger::Severity severity
;
94 /// Current log file name
97 /// Current maximum size of log files
100 /// Current maximum number of log files
106 * Worker class for the logger.
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.
111 class LoggerWorker
: public QObject
118 * Writes a message to the log file
119 * @param src The logger source
120 * @param msg The message
122 * This function writes the message to the log file. It also controls the size and
123 * number of log files.
125 void writeToLogFile(LoggerSource
const & src
, QString
const & msg
);
130 * iLogger interface implementation.
132 * This class implements the iLogger interface.
134 class Logger
: public iLogger
145 * Initializes the interface implementation
146 * @return True if ok; false if initialization failed
154 virtual QString
defaultSource() const { return mDefaultSource
; }
156 virtual void setDefaultSource(QString
const & source
);
158 virtual iLogger::Severity
severity(QString
const & source
= 0);
160 virtual void setSeverity(iLogger::Severity severity
, QString
const & source
= 0);
162 virtual uint
maxSize(QString
const & source
= 0);
164 virtual void setMaxSize(uint maxSize
, QString
const & source
= 0);
166 virtual uint
maxCount(QString
const & source
= 0);
168 virtual void setMaxCount(uint maxCount
, QString
const & source
= 0);
170 virtual iLogger::Severity
consoleSeverity() const { return mConsoleSeverity
; }
172 virtual void setConsoleSeverity(iLogger::Severity severity
);
174 virtual void write(Severity severity
, QString
const & msg
, QString
const & source
= 0, QString
const & where
= 0);
176 virtual QString
printf(char const * const fmt
, ...) const;
178 virtual QString
printable(QByteArray
const & msg
) const;
180 virtual FatalMsgHandler
installFatalMsgHandler(FatalMsgHandler newHandler
);
185 void writeToLogFile(LoggerSource
const & src
, QString
const & msg
);
190 /// Current fatal error message handler
191 FatalMsgHandler mFatalMsgHandler
;
193 /// Console output severity level
194 iLogger::Severity mConsoleSeverity
;
196 /// Current default source (defaults to "evaf")
197 QString mDefaultSource
;
200 QHash
<QString
, QExplicitlySharedDataPointer
<LoggerSource
> > mSources
;
206 LoggerWorker
* mWorker
;
211 /// Returns the source by the name. The source is created if it does not exist yet.
212 LoggerSource
* getSource(QString
const & name
);
215 /// Changes text colors on the Windows console
216 void setColor(short int c
);
221 } // namespace eVaf::Common::Internal
222 } // namespace eVaf::Common