summaryrefslogtreecommitdiff
path: root/ext/Log4Qt/src/logmanager.h
blob: 641f6582dbada230c809671fd53e9cc6befaa926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/******************************************************************************
 *
 * package:     Log4Qt
 * file:        logmanager.h
 * created:     September 2007
 * author:      Martin Heinrich
 *
 *
 * Copyright 2007 Martin Heinrich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

#ifndef LOG4QT_LOGMANAGER_H
#define LOG4QT_LOGMANAGER_H


/******************************************************************************
 * Dependencies
 ******************************************************************************/

#include <QtCore/QObject>

#include <QtCore/QHash>
#include <QtCore/QList>
#include <QtCore/QMutex>
#include <QtCore/QString>
#include "level.h"
#include "logger.h"


/******************************************************************************
 * Declarations
 ******************************************************************************/

namespace Log4Qt
{

	class LoggerRepository;

	/*!
	 * \brief The class LogManager manages Logger in the default
	 *        LoggerRepository.
	 *
	 * The LogManager manages logger in a single Hierarchy instance. It
	 * provides access to special logger over the logLogger(), qtLogger()
	 * and rootLogger() member functions.
	 *
	 * The LogManager is handling the initialisation on startup. The
	 * initialisation procedure will first attempt to configure the package
	 * based on environment variables. If the attempt fails it will check for
	 * the existence of configuration files in several location. For detailed
	 * description of the initialisation procedure see \ref Init
	 * "Initialization procedure".
	 *
	 * Messages created by qDebug(), qWarning(), qCritical() and qFatal() can
	 * be can be handled by the LogManager. By default the message handling
	 * is disabled. It can be enabled by calling setHandleQtMessages(). Once
	 * enabled all messages are logged using the logger qtLogger().
	 *
	 * The Log4Qt runtime version is accessible over version(). The macros
		 * \ref Log4Qt::LOG4QT_VERSION "LOG4QT_VERSION" and
		 * \ref Log4Qt::LOG4QT_VERSION_STR "LOG4QT_VERSION_STR" provide the
		 * compile time version.
		 *
	 * \note All the functions declared in this class are thread-safe.
	 */
	class LOG4QT_EXPORT LogManager
	{
	private:
			LogManager();
			LogManager(const LogManager &rOther); // Not implemented
			virtual ~LogManager();
			LogManager &operator=(const LogManager &rOther); // Not implemented

	public:
		/*!
		 * Returns if the handling of messages created by calls to qDebug(),
		 * qWarning(), qCritical() and qFatal() is activated.
		 *
		 * \sa setHandleQtMessages()
		 */
			static bool handleQtMessages();

			static LoggerRepository *loggerRepository();

		/*!
		 * Returns the logger used for logging internal messages. See
		 * \ref LogLog "Logging within the package" for more details.
		 *
		 * Calling this function is equivalent to calling logger("Log4Qt").
		 */
			static Logger *logLogger();

		/*!
		 * Returns a pointer to the logger used for logging messages created by
		 * calls to qDebug(), qWarning(), qCritical() and qFatal().
		 *
		 * Calling this function is equivalent to calling logger("Qt").
		 *
		 * \sa setHandleQtMessages()
		 */
			static Logger *qtLogger();

			static Logger *rootLogger();
			static QList<Logger*> loggers();
			static Level threshold();
			static void setThreshold(Level level);

		/*!
		 * Activates or deactivates the handling of messages created by calls
		 * to qDebug(), qWarning(), qCritical() and qFatal() is activated.
		 *
		 * If activated, a Qt message handler is installed. Messages are logged
		 * using the logger returned by qtLogger(). For fatal messages the same
		 * exit procedure is implemented as for qFatal().
		 *
		 * The following mappping is used from QtMsgType to Level:
		 *
			 * <table align="center" border="1" cellpadding="2" cellspacing="0" bordercolor="#84b0c7">
			 * <tr bgcolor="#d5e1e8">
			 * <th> &nbsp;&nbsp;&nbsp; QtMsgType &nbsp;&nbsp;&nbsp;</th>
			 * <th> %Level </th>
			 * </tr><tr>
			 * <td> QtDebugMsg </td>
			 * <td> Level::DEBUG_INT </td>
			 * </tr><tr bgcolor="#ffffff">
			 * <td> QtWarningMsg </td>
			 * <td> Level::WARN_INT </td>
			 * </tr><tr>
			 * <td> QtCriticalMsg </td>
			 * <td> Level::ERROR_INT </td>
			 * </tr><tr bgcolor="#ffffff">
			 * <td> QtFatalMsg </td>
			 * <td> Level::FATAL_INT </td>
			 * </tr><tr>
			 * <td> QtSystemMsg </td>
			 * <td> Level::TRACE_INT </td>
			 * </tr>
			 * </table>
		 *
		 * The default value is false for not handling Qt messages.
		 *
		 * \sa handleQtMessages(), qInstallMsgHandler(), qFatal()
		 */
			static void setHandleQtMessages(bool handleQtMessages);

			/*!
			 * Configures the logging for the package to its default behaviour.
			 *
			 * The logger logLogger() is configured to be not additive. Messages
			 * with the level Level::ERROR_INT and Level::FATAL_INT are written
			 * to \c stderr using a ConsoleAppender. The remaining messages are
			 * written to \c stdout using a second ConsoleAppender. The level is
			 * read from the system environment or application settings using
			 * InitialisationHelper::setting() with the key \c Debug. If a level
			 * value is found, but it is not a valid Level string,
			 * Level::DEBUG_INT is used. If no level string is found
			 * Level::ERROR_INT is used.
			 *
			 * The function does not remove any appender from the logger
			 * logLogger().
			 *
			 * \sa \ref LogLog "Logging within the package",
			 *     \ref Env "Environment Variables",
			 *     resetConfiguration(), InitialisationHelper::setting()
			 */
			static void configureLogLogger();

			static bool exists(const char *pName);
			// JAVA: void fireAddAppenderEvent(Logger *pLogger, Appender *pAppender);

				/*!
				 * Returns the LogManager instance.
				 */
				static LogManager *instance();

				static Logger *logger(const QString &rName);

			/*!
			 * Reset all values contained in logger repository to their default.
			 *
			 * All appenders are removed from all loggers. The loggers are handled
			 * in no particular order. The last loggers to be reset are qtLogger(),
			 * logLogger() and rootLogger() in that order.
			 *
			 * The handling of messages created by calls to qDebug(), qWarning(),
			 * qCritical() and qFatal() is deactivated.
			 *
			 * The internal logging is initialised to its default bahaviour
			 * using configureLogLogger().
		 *
		 * \sa LoggerRepository::resetConfiguration(), setHandleQtMessages(),
		 *     configureLogLogger()
			 */
			static void resetConfiguration();

			static void shutdown();

			/*!
			 * Executes the default initialisation procedure of the package.
			 *
			 * The function will test for the setting \c DefaultInitOverride in
			 * the system environment and application settings using
			 * \ref InitialisationHelper::setting(). If the value is present and
			 * set to anything else then \c false, the initialisation is aborted.
			 * <br>
			 * The system environment and application settings are tested for the
			 * setting \c Configuration. If it is found and it is a valid path to
			 * a file, the package is configured with the file using
			 * \ref PropertyConfigurator::doConfigure(const QString &, LoggerRepository *)
			 * "PropertyConfigurator::doConfigure()". If the setting
			 * \c Configuration is not available and a QCoreApplication object is
			 * present, the application settings are tested for a group
			 * \c Properties. If the group exists, the package is configured
			 * with the setting using the
			 * \ref PropertyConfigurator::doConfigure(const QSettings &r, LoggerRepository *)
			 * "PropertyConfiguratordoConfigure()". If neither a configuration
			 * file nor configuration settings could be found, the current working
			 * directory is searched for the file \c "log4qt.properties". If it is
			 * found, the package is configured with the file using
			 * \ref PropertyConfigurator::doConfigure(const QString &, LoggerRepository *)
			 * "PropertyConfigurator::doConfigure()".
			 *
			 * \sa \ref Init "Initialization procedure",
			 *     \ref Env "Environment Variables",
			 *     InitialisationHelper::setting()
			 */
			static void startup();

				/*!
				 * Returns the version number of Log4Qt at run-time. This may be a
				 * different version than the version the application was compiled
				 * against.
				 *
				 * \sa \ref Log4Qt::LOG4QT_VERSION "LOG4QT_VERSION",
				 *     \ref Log4Qt::LOG4QT_VERSION_STR "LOG4QT_VERSION_STR"

				 */
				static const char* version();

	private:
		void doSetHandleQtMessages(bool handleQtMessages);
		void doConfigureLogLogger();
		void doStartup();
		void welcome();
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
		static void qtMessageHandler(QtMsgType type,
																 const char *pMessage);
#else
		static void qtMessageHandler(QtMsgType type, const QMessageLogContext &, const QString & pMessage);
#endif

	private:
			mutable QMutex mObjectGuard;
			LoggerRepository *mpLoggerRepository;
			Logger *mpNullLogger;
			bool mHandleQtMessages;
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
			QtMsgHandler mOldQtMsgHandler;
#else
			QtMessageHandler mOldQtMsgHandler;
#endif
			static LogManager *mspInstance;
	};


	/***************************************************************************
	 * Operators, Helper
	 ***************************************************************************/

	#ifndef QT_NO_DEBUG_STREAM
	/*!
	 * \relates LogManager
	 *
	 * Writes all object member variables to the given debug stream \a rDebug and
	 * returns the stream.
	 *
	 * <tt>
	 * %LogManager(loggerrepository:Hierarchy(loggers:6 root-level:"DEBUG"
	 *             root-appenders:0 log-level: "NULL" log-appenders:0
	 *             qt-level: "NULL" qt-appenders:0 handleqtmessages: false )
	 * </tt>
	 * \sa QDebug
	 */
	QDebug operator<<(QDebug debug,
										const LogManager &rLogManager);
	#endif // QT_NO_DEBUG_STREAM


	/**************************************************************************
	 * Inline
	 **************************************************************************/

	inline LoggerRepository *LogManager::loggerRepository()
		{   // QMutexLocker locker(&instance()->mObjectGuard); // Constant for object lifetime
			return instance()->mpLoggerRepository;  }

	inline bool LogManager::handleQtMessages()
	{   // QMutexLocker locker(&instance()->mObjectGuard);  // Read/Write of bool is safe
				return instance()->mHandleQtMessages;   }

	inline Logger *LogManager::logLogger()
	{   // QMutexLocker locker(&instance()->mObjectGuard); // Constant for object lifetime
				return logger(QLatin1String("Log4Qt"));   }

	inline Logger *LogManager::qtLogger()
	{   // QMutexLocker locker(&instance()->mObjectGuard); // Constant for object lifetime
				return logger(QLatin1String("Qt"));    }

	inline void LogManager::setHandleQtMessages(bool handleQtMessages)
	{	instance()->doSetHandleQtMessages(handleQtMessages);	}

	inline void LogManager::configureLogLogger()
	{	instance()->doConfigureLogLogger();	}

	inline void LogManager::startup()
	{	instance()->doStartup();	}

} // namespace Log4Qt


// Q_DECLARE_TYPEINFO(Log4Qt::LogManager, Q_COMPLEX_TYPE); // Use default


#endif // LOG4QT_LOGMANAGER_H