00001 /****************************************************************************** 00002 * 00003 * package: Log4Qt 00004 * file: logmanager.h 00005 * created: September 2007 00006 * author: Martin Heinrich 00007 * 00008 * 00009 * Copyright 2007 Martin Heinrich 00010 * 00011 * Licensed under the Apache License, Version 2.0 (the "License"); 00012 * you may not use this file except in compliance with the License. 00013 * You may obtain a copy of the License at 00014 * 00015 * http://www.apache.org/licenses/LICENSE-2.0 00016 * 00017 * Unless required by applicable law or agreed to in writing, software 00018 * distributed under the License is distributed on an "AS IS" BASIS, 00019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 * See the License for the specific language governing permissions and 00021 * limitations under the License. 00022 * 00023 ******************************************************************************/ 00024 00025 #ifndef LOG4QT_LOGMANAGER_H 00026 #define LOG4QT_LOGMANAGER_H 00027 00028 00029 /****************************************************************************** 00030 * Dependencies 00031 ******************************************************************************/ 00032 00033 #include <QtCore/QObject> 00034 00035 #include <QtCore/QHash> 00036 #include <QtCore/QList> 00037 #include <QtCore/QMutex> 00038 #include <QtCore/QString> 00039 #include "log4qt/level.h" 00040 #include "log4qt/logger.h" 00041 00042 00043 /****************************************************************************** 00044 * Declarations 00045 ******************************************************************************/ 00046 00047 namespace Log4Qt 00048 { 00049 00050 class LoggerRepository; 00051 00079 class LogManager 00080 { 00081 private: 00082 LogManager(); 00083 LogManager(const LogManager &rOther); // Not implemented 00084 virtual ~LogManager(); 00085 LogManager &operator=(const LogManager &rOther); // Not implemented 00086 00087 public: 00094 static bool handleQtMessages(); 00095 00096 static LoggerRepository *loggerRepository(); 00097 00104 static Logger *logLogger(); 00105 00114 static Logger *qtLogger(); 00115 00116 static Logger *rootLogger(); 00117 static QList<Logger*> loggers(); 00118 static Level threshold(); 00119 static void setThreshold(Level level); 00120 00157 static void setHandleQtMessages(bool handleQtMessages); 00158 00179 static void configureLogLogger(); 00180 00181 static bool exists(const char *pName); 00182 // JAVA: void fireAddAppenderEvent(Logger *pLogger, Appender *pAppender); 00183 00187 static LogManager *instance(); 00188 00189 static Logger *logger(const QString &rName); 00190 00207 static void resetConfiguration(); 00208 00209 static void shutdown(); 00210 00240 static void startup(); 00241 00251 static const char* version(); 00252 00253 private: 00254 void doSetHandleQtMessages(bool handleQtMessages); 00255 void doConfigureLogLogger(); 00256 void doStartup(); 00257 void welcome(); 00258 static void qtMessageHandler(QtMsgType type, 00259 const char *pMessage); 00260 00261 private: 00262 mutable QMutex mObjectGuard; 00263 LoggerRepository *mpLoggerRepository; 00264 Logger *mpNullLogger; 00265 bool mHandleQtMessages; 00266 QtMsgHandler mOldQtMsgHandler; 00267 static LogManager *mspInstance; 00268 }; 00269 00270 00271 /*************************************************************************** 00272 * Operators, Helper 00273 ***************************************************************************/ 00274 00275 #ifndef QT_NO_DEBUG_STREAM 00276 00289 QDebug operator<<(QDebug debug, 00290 const LogManager &rLogManager); 00291 #endif // QT_NO_DEBUG_STREAM 00292 00293 00294 /************************************************************************** 00295 * Inline 00296 **************************************************************************/ 00297 00298 inline LoggerRepository *LogManager::loggerRepository() 00299 { // QMutexLocker locker(&instance()->mObjectGuard); // Constant for object lifetime 00300 return instance()->mpLoggerRepository; } 00301 00302 inline bool LogManager::handleQtMessages() 00303 { // QMutexLocker locker(&instance()->mObjectGuard); // Read/Write of bool is safe 00304 return instance()->mHandleQtMessages; } 00305 00306 inline Logger *LogManager::logLogger() 00307 { // QMutexLocker locker(&instance()->mObjectGuard); // Constant for object lifetime 00308 return logger(QLatin1String("Log4Qt")); } 00309 00310 inline Logger *LogManager::qtLogger() 00311 { // QMutexLocker locker(&instance()->mObjectGuard); // Constant for object lifetime 00312 return logger(QLatin1String("Qt")); } 00313 00314 inline void LogManager::setHandleQtMessages(bool handleQtMessages) 00315 { instance()->doSetHandleQtMessages(handleQtMessages); } 00316 00317 inline void LogManager::configureLogLogger() 00318 { instance()->doConfigureLogLogger(); } 00319 00320 inline void LogManager::startup() 00321 { instance()->doStartup(); } 00322 00323 } // namespace Log4Qt 00324 00325 00326 // Q_DECLARE_TYPEINFO(Log4Qt::LogManager, Q_COMPLEX_TYPE); // Use default 00327 00328 00329 #endif // LOG4QT_LOGMANAGER_H