configuratorhelper.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: configuratorhelper.h
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * Copyright 2007 Martin Heinrich
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************/
  24. #ifndef LOG4QT_HELPERS_CONFIGURATORHELPER_H
  25. #define LOG4QT_HELPERS_CONFIGURATORHELPER_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include <QtCore/QObject>
  30. #include <QtCore/QList>
  31. #include <QtCore/QMutex>
  32. #include "log4qt/loggingevent.h"
  33. /******************************************************************************
  34. * Declarations
  35. ******************************************************************************/
  36. class QFileSystemWatcher;
  37. namespace Log4Qt
  38. {
  39. /*!
  40. * \brief The class ConfiguratorHelper provides a confiuration file watch
  41. * and last error for configurator classes.
  42. *
  43. * A configuration file can be set using setConfigurationFile(). The file
  44. * is watched for changes. If a change occurs the configuration is reloaded
  45. * and the ConfigurationFileChanged() signal is emitted. Error information
  46. * for the last call to a configure function or the last configuration file
  47. * change can be accessed using configureError().
  48. *
  49. * \note All the functions declared in this class are thread-safe.
  50. */
  51. class ConfiguratorHelper : public QObject
  52. {
  53. Q_OBJECT
  54. public:
  55. /*!
  56. * Prototype for a configure callback function. The function is called
  57. * when then configuration file is changed and takes the
  58. * configuration file as a parameter.
  59. *
  60. * \sa setConfigurationFile(),
  61. * PropertyConfigurator::configure(const QString &)
  62. */
  63. typedef bool (*ConfigureFunc)(const QString &rFileName);
  64. private:
  65. ConfiguratorHelper();
  66. ConfiguratorHelper(const ConfiguratorHelper &rOther); // Not implemented
  67. virtual ~ConfiguratorHelper();
  68. ConfiguratorHelper &operator=(const ConfiguratorHelper &rOther); // Not implemented
  69. public:
  70. /*!
  71. * Returns the error information for the last configuration operation
  72. * that took place. The configuration operation could be the result of
  73. * a call to one of the configure methods or through a change
  74. * to the configuration file.
  75. *
  76. * \sa setConfigureError(), PropertyConfigurator::configure(),
  77. * setConfigurationFile()
  78. */
  79. static QList<LoggingEvent> configureError();
  80. /*!
  81. * Returns the current configuration file.
  82. *
  83. * \sa setConfigurationFile()
  84. */
  85. static QString configurationFile();
  86. /*!
  87. * Returns the ConfiguratorHelper instance.
  88. */
  89. static ConfiguratorHelper *instance();
  90. /*!
  91. * Sets the configuration error information for the last configuration
  92. * operation.
  93. *
  94. * \sa configureError()
  95. */
  96. static void setConfigureError(const QList<LoggingEvent> &rConfigureError);
  97. /*!
  98. * Sets the configuration file to \a rFileName. The file is watched for
  99. * changes. On a file change the function \a pConfigureFunc will be called
  100. * and the signal configurationFileChange() will be emitted.
  101. *
  102. * Setting the configuration file to an empty string stops the file watch.
  103. *
  104. * \sa configurationFile(), PropertyConfigurator::configureAndWatch(),
  105. * configureError()
  106. */
  107. static void setConfigurationFile(const QString &rFileName = QString(),
  108. ConfigureFunc pConfigureFunc = 0);
  109. signals:
  110. /*!
  111. * The signal is emitted after a change to the file \a rFileName
  112. * was processed. If an error occured during the configuration, the
  113. * flag \a error will be true and error information is available
  114. * over configureError().
  115. */
  116. void configurationFileChanged(const QString &rFileName,
  117. bool error);
  118. private slots:
  119. void doConfigurationFileChanged(const QString &rFileName);
  120. private:
  121. void doSetConfigurationFile(const QString &rFileName,
  122. ConfigureFunc pConfigureFunc);
  123. private:
  124. mutable QMutex mObjectGuard;
  125. QString mConfigurationFile;
  126. ConfigureFunc mpConfigureFunc;
  127. QFileSystemWatcher *mpConfigurationFileWatch;
  128. QList<LoggingEvent> mConfigureError;
  129. #ifndef QT_NO_DEBUG_STREAM
  130. // Needs to be friend to access details
  131. friend QDebug operator<<(QDebug debug,
  132. const ConfiguratorHelper &rConfiguratorHelper);
  133. #endif // QT_NO_DEBUG_STREAM
  134. };
  135. /**************************************************************************
  136. * Operators, Helper
  137. **************************************************************************/
  138. #ifndef QT_NO_DEBUG_STREAM
  139. /*!
  140. * \relates ConfiguratorHelper
  141. *
  142. * Writes all object member variables to the given debug stream \a rDebug and
  143. * returns the stream.
  144. *
  145. * <tt>
  146. * %ConfiguratorHelper(configurationfile: "" configurefunc: false
  147. * filesystemwatcher: QObject(0x0) )
  148. * </tt>
  149. * \sa QDebug, ConfiguratorHelper::logManager()
  150. */
  151. QDebug operator<<(QDebug debug,
  152. const ConfiguratorHelper &rConfiguratorHelper);
  153. #endif // QT_NO_DEBUG_STREAM
  154. /**************************************************************************
  155. * Inline
  156. **************************************************************************/
  157. inline QList<LoggingEvent> ConfiguratorHelper::configureError()
  158. { QMutexLocker locker(&instance()->mObjectGuard);
  159. return instance()->mConfigureError; }
  160. inline QString ConfiguratorHelper::configurationFile()
  161. { QMutexLocker locker(&instance()->mObjectGuard);
  162. return instance()->mConfigurationFile; }
  163. inline void ConfiguratorHelper::setConfigureError(const QList<LoggingEvent> &rConfigureError)
  164. { QMutexLocker locker(&instance()->mObjectGuard);
  165. instance()->mConfigureError = rConfigureError; }
  166. inline void ConfiguratorHelper::setConfigurationFile(const QString &rFileName,
  167. ConfigureFunc pConfigureFunc)
  168. { instance()->doSetConfigurationFile(rFileName, pConfigureFunc); }
  169. } // namespace Log4Qt
  170. // Q_DECLARE_TYPEINFO(Log4Qt::ConfiguratorHelper, Q_COMPLEX_TYPE); // use default
  171. #endif // LOG4QT_HELPERS_CONFIGURATORHELPER_H