]> vaikene.ee Git - evaf/blob - src/libs/Common/logger.h
More work on the common library and the main GUI application.
[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
27
28 namespace eVaf {
29 namespace Common {
30 namespace Internal {
31
32 /// Default fatal error message handler
33 void defFatalMsgHandler(QString const & msg, QString const & source, QString const & where);
34
35 /**
36 * Logger source.
37 *
38 * This class stores information about known sources.
39 */
40 class LoggerSource : public QSharedData
41 {
42 public:
43
44 LoggerSource();
45
46 LoggerSource(LoggerSource const & o);
47
48 /**
49 * Initializes the source
50 * @param source Name of the source
51 * @param logDir Full path to the log directory
52 * @param etcDir Full path to the configuration files directory
53 *
54 * This function initializes the newly created logger source and sets initial
55 * parameters for the source.
56 *
57 * Default parameters:
58 * @li severity is set to Fatal
59 * @li maximum size of the log file is set to 100 KiB
60 * @li maximum number of log files is set to 3
61 *
62 * Default parameters can be overwritten with values read from the logger.ini file.
63 * This file should have the [.default] section with new default values for all the
64 * sources. Individual sources can have their parameters changed in sections with the
65 * name of the source.
66 *
67 * Example logger.ini file:
68 * @code
69 * [.default]
70 * severity = Fatal
71 * maxSize = 100
72 * maxCount = 3
73 *
74 * [my-source]
75 * severity = Warning
76 * maxSize = 1000
77 * maxCount = 10
78 * @endcode
79 */
80 void init(QString const & source, QString const & logDir, QString const & etcDir);
81
82
83 public: // Members (we don't bother adding getter/setter functions)
84
85 /// Current severity level
86 iLogger::Severity severity;
87
88 /// Current log file name
89 QString fileName;
90
91 /// Current maximum size of log files
92 uint maxSize;
93
94 /// Current maximum number of log files
95 uint maxCount;
96
97 };
98
99 /**
100 * iLogger interface implementation.
101 *
102 * This class implements the iLogger interface.
103 */
104 class Logger : public iLogger
105 {
106 Q_OBJECT
107
108 public:
109
110 Logger();
111
112 virtual ~Logger();
113
114 /**
115 * Initializes the interface implementation
116 * @return True if ok; false if initialization failed
117 */
118 bool init();
119
120 /*
121 iLogger interface
122 */
123
124 virtual QString defaultSource() const { return mDefaultSource; }
125
126 virtual void setDefaultSource(QString const & source);
127
128 virtual Severity severity(QString const & source = 0) const;
129
130 virtual void setSeverity(Severity severity, QString const & source = 0);
131
132 virtual uint maxSize(QString const & source = 0) const;
133
134 virtual void setMaxSize(uint maxSize, QString const & source = 0);
135
136 virtual uint maxCount(QString const & source = 0) const;
137
138 virtual void setMaxCount(uint maxCount, QString const & source = 0);
139
140 virtual Severity consoleSeverity() const { return mConsoleSeverity; }
141
142 virtual void setConsoleSeverity(Severity severity);
143
144 virtual void write(Severity severity, QString const & msg, QString const & source = 0, QString const & where = 0);
145
146 virtual QString printf(char const * const fmt, ...) const;
147
148 virtual FatalMsgHandler installFatalMsgHandler(FatalMsgHandler newHandler);
149
150
151 private: // Members
152
153 /// Current fatal error message handler
154 FatalMsgHandler mFatalMsgHandler;
155
156 /// Current default source (defaults to "evaf")
157 QString mDefaultSource;
158
159 /// Logger sources
160 QHash<QString, QExplicitlySharedDataPointer<LoggerSource> > mSources;
161
162
163 private: // Methods
164
165 /// Returns the source by the name
166 LoggerSource * getSource(QString const & name) const;
167
168 /// Creates a new source
169 LoggerSource * addSource(QString const & name);
170
171 #ifdef Q_OS_WIN32
172 /// Changes text colors on the Windows console
173 void setColor(short int c);
174 #endif
175
176 };
177
178 } // namespace eVaf::Common::Internal
179 } // namespace eVaf::Common
180 } // namespace eVaf
181
182 #endif // logger.h