appenderskeleton.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: appenderskeleton.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_APPENDERSKELETON_H
  25. #define LOG4QT_APPENDERSKELETON_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include "log4qt/appender.h"
  30. #include <QtCore/QMutex>
  31. #include "log4qt/helpers/logobjectptr.h"
  32. /******************************************************************************
  33. * Declarations
  34. ******************************************************************************/
  35. namespace Log4Qt
  36. {
  37. class Filter;
  38. class Layout;
  39. class Logger;
  40. class LoggingEvent;
  41. /*!
  42. * \brief The class AppenderSkeleton implements general Appender functionality.
  43. *
  44. * \note All the functions declared in this class are thread-safe.
  45. *
  46. * \note The ownership and lifetime of objects of this class are managed. See
  47. * \ref Ownership "Object ownership" for more details.
  48. */
  49. class AppenderSkeleton : public Appender
  50. {
  51. Q_OBJECT
  52. /*!
  53. * The property holds if the Appender has been activated.
  54. *
  55. * \sa isActive()
  56. */
  57. Q_PROPERTY(bool isActive READ isActive)
  58. /*!
  59. * The property holds if the Appender has been closed.
  60. *
  61. * \sa isClosed()
  62. */
  63. Q_PROPERTY(bool isClosed READ isClosed)
  64. /*!
  65. * The property holds the threshold level used by the Appender.
  66. *
  67. * \sa threshold(), setThreshold()
  68. */
  69. Q_PROPERTY(Level threshold READ threshold WRITE setThreshold)
  70. public:
  71. AppenderSkeleton(QObject *pParent = 0);
  72. protected:
  73. AppenderSkeleton(const bool isActive,
  74. QObject *pParent = 0);
  75. public:
  76. // virtual ~AppenderSkeleton(); Use compiler default
  77. private:
  78. AppenderSkeleton(const AppenderSkeleton &rOther); // Not implemented
  79. AppenderSkeleton &operator=(const AppenderSkeleton &rOther); // Not implemented
  80. public:
  81. // JAVA: ErrorHandler* errorHandler();
  82. virtual Filter *filter() const;
  83. virtual Layout *layout() const;
  84. bool isActive() const;
  85. bool isClosed() const;
  86. virtual QString name() const;
  87. Level threshold() const;
  88. // JAVA: void setErrorHandler(ErrorHandler *pErrorHandler);
  89. virtual void setLayout(Layout *pLayout);
  90. virtual void setName(const QString &rName);
  91. void setThreshold(Level level);
  92. virtual void activateOptions();
  93. virtual void addFilter(Filter *pFilter);
  94. virtual void clearFilters();
  95. virtual void close();
  96. /*!
  97. * Performs checks and delegates the actuall appending to the subclass
  98. * specific append() function.
  99. *
  100. * \sa append(), checkEntryConditions(), isAsSevereAsThreshold(), Filter
  101. */
  102. virtual void doAppend(const LoggingEvent &rEvent);
  103. // JAVA: void finalize();
  104. Filter* firstFilter() const;
  105. bool isAsSevereAsThreshold(Level level) const;
  106. protected:
  107. virtual void append(const LoggingEvent &rEvent) = 0;
  108. /*!
  109. * Tests if all entry conditions for using append() in this class are
  110. * met.
  111. *
  112. * If a conditions is not met, an error is logged and the function
  113. * returns false.
  114. *
  115. * The checked conditions are:
  116. * - That the appender has been activated (APPENDER_NOT_ACTIVATED_ERROR)
  117. * - That the appender was not closed (APPENDER_CLOSED_ERROR)
  118. * - That the appender has a layout set, if it requires one
  119. * (logging_error(APPENDER_USE_MISSING_LAYOUT_ERROR)
  120. *
  121. * The function is called as part of the checkEntryConditions() chain
  122. * started by doAppend(). The doAppend() function calls the subclass
  123. * specific checkEntryConditions() function. The function checks the
  124. * class specific conditions and calls checkEntryConditions() of
  125. * it's parent class. The last function called is
  126. * AppenderSkeleton::checkEntryConditions().
  127. *
  128. * \sa doAppend()
  129. */
  130. virtual bool checkEntryConditions() const;
  131. protected:
  132. mutable QMutex mObjectGuard;
  133. private:
  134. bool mAppendRecursionGuard;
  135. volatile bool mIsActive;
  136. volatile bool mIsClosed;
  137. LogObjectPtr<Layout> mpLayout;
  138. Level mThreshold;
  139. LogObjectPtr<Filter> mpHeadFilter;
  140. LogObjectPtr<Filter> mpTailFilter;
  141. };
  142. /**************************************************************************
  143. * Operators, Helper
  144. **************************************************************************/
  145. /**************************************************************************
  146. * Inline
  147. **************************************************************************/
  148. inline Filter *AppenderSkeleton::filter() const
  149. { QMutexLocker locker(&mObjectGuard);
  150. return mpHeadFilter; }
  151. inline Layout *AppenderSkeleton::layout() const
  152. { QMutexLocker locker(&mObjectGuard);
  153. return mpLayout; }
  154. inline QString AppenderSkeleton::name() const
  155. { QMutexLocker locker(&mObjectGuard);
  156. return objectName(); }
  157. inline Level AppenderSkeleton::threshold() const
  158. { // QMutexLocker locker(&mObjectGuard); // Level is threadsafe
  159. return mThreshold; }
  160. inline void AppenderSkeleton::setLayout(Layout *pLayout)
  161. { QMutexLocker locker(&mObjectGuard);
  162. mpLayout = pLayout; }
  163. inline void AppenderSkeleton::setName(const QString &rName)
  164. { QMutexLocker locker(&mObjectGuard);
  165. setObjectName(rName); }
  166. inline void AppenderSkeleton::setThreshold(Level level)
  167. { // QMutexLocker locker(&mObjectGuard); // Level is threadsafe
  168. mThreshold = level; }
  169. inline bool AppenderSkeleton::isActive() const
  170. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  171. return mIsActive; }
  172. inline bool AppenderSkeleton::isClosed() const
  173. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  174. return mIsClosed; }
  175. inline Filter *AppenderSkeleton::firstFilter() const
  176. { QMutexLocker locker(&mObjectGuard);
  177. return filter(); }
  178. inline bool AppenderSkeleton::isAsSevereAsThreshold(Level level) const
  179. { // QMutexLocker locker(&mObjectGuard); // Level is threadsafe
  180. return (mThreshold <= level); }
  181. } // namespace Log4Qt
  182. // Q_DECLARE_TYPEINFO(Log4Qt::AppenderSkeleton, Q_COMPLEX_TYPE); // Use default
  183. #endif // LOG4QT_APPENDERSKELETON_H