writerappender.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: writerappender.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_WRITERAPPENDER_H
  25. #define LOG4QT_WRITERAPPENDER_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include "log4qt/appenderskeleton.h"
  30. /******************************************************************************
  31. * Declarations
  32. ******************************************************************************/
  33. class QTextCodec;
  34. class QTextStream;
  35. namespace Log4Qt
  36. {
  37. /*!
  38. * \brief The class WriterAppender appends log events to a QTextStream.
  39. *
  40. * \note All the functions declared in this class are thread-safe.
  41. *  
  42. * \note The ownership and lifetime of objects of this class are managed.
  43. * See \ref Ownership "Object ownership" for more details.
  44. */
  45. class WriterAppender : public AppenderSkeleton
  46. {
  47. Q_OBJECT
  48. /*!
  49. * The property holds the codec the appender uses.
  50. *
  51. * The default is null to use the codec the writer has set.
  52. *
  53. * \sa encoding(), setEncoding()
  54. */
  55. Q_PROPERTY(QTextCodec* encoding READ encoding WRITE setEncoding)
  56. /*!
  57. * The property holds the writer the appender uses.
  58. *
  59. * \sa writer(), setWriter()
  60. */
  61. Q_PROPERTY(QTextStream* writer READ writer WRITE setWriter)
  62. /*!
  63. * The property holds, if the writer flushes after all write operations.
  64. *
  65. * The default is true for flushing.
  66. *
  67. * \sa immediateFlush(), setImmediateFlush()
  68. */
  69. Q_PROPERTY(bool immediateFlush READ immediateFlush WRITE setImmediateFlush)
  70. public:
  71. WriterAppender(QObject *pParent = 0);
  72. WriterAppender(Layout *pLayout,
  73. QObject *pParent = 0);
  74. WriterAppender(Layout *pLayout,
  75. QTextStream *pTextStream,
  76. QObject *pParent = 0);
  77. virtual ~WriterAppender();
  78. private:
  79. WriterAppender(const WriterAppender &rOther); // Not implemented
  80. WriterAppender &operator=(const WriterAppender &rOther); // Not implemented
  81. public:
  82. virtual bool requiresLayout() const;
  83. QTextCodec *encoding() const;
  84. bool immediateFlush() const;
  85. QTextStream *writer() const;
  86. /*!
  87. * Sets the codec used by the writer to \a pTextCoded.
  88. *
  89. * If a codec is set with setEncoding, it will overwrite the codec set
  90. * in the text stream. A subsequent call with \a pTextCoded equals null
  91. * will resets the codec to the default QTextCodec::codecForLocale().
  92. *
  93. * \sa encoding(), QTextSream::setCodec(), QTextCodec::codecForLocale()
  94. */
  95. void setEncoding(QTextCodec *pTextCodec);
  96. void setImmediateFlush(bool immediateFlush);
  97. void setWriter(QTextStream *pTextStream);
  98. virtual void activateOptions();
  99. virtual void close();
  100. protected:
  101. virtual void append(const LoggingEvent &rEvent);
  102. /*!
  103. * Tests if all entry conditions for using append() in this class are
  104. * met.
  105. *
  106. * If a conditions is not met, an error is logged and the function
  107. * returns false. Otherwise the result of
  108. * AppenderSkeleton::checkEntryConditions() is returned.
  109. *
  110. * The checked conditions are:
  111. * - A writer has been set (APPENDER_USE_MISSING_WRITER_ERROR)
  112. *
  113. * The function is called as part of the checkEntryConditions() chain
  114. * started by AppenderSkeleton::doAppend().
  115. *
  116. * \sa AppenderSkeleton::doAppend(),
  117. * AppenderSkeleton::checkEntryConditions()
  118. */
  119. virtual bool checkEntryConditions() const;
  120. void closeWriter();
  121. #ifndef QT_NO_DEBUG_STREAM
  122. /*!
  123. * Writes all object member variables to the given debug stream
  124. * \a rDebug and returns the stream.
  125. *
  126. * <tt>
  127. * %WriterAppender(name:"WA" encoding:"" immediateFlush:true
  128. * isactive:false isclosed:false layout:"TTCC"
  129. * referencecount:1 threshold:"NULL"
  130. * writer:0x0)
  131. * </tt>
  132. * \sa QDebug, operator<<(QDebug debug, const LogObject &rLogObject )
  133. */
  134. virtual QDebug debug(QDebug &rDebug) const;
  135. #endif // QT_NO_DEBUG_STREAM
  136. virtual bool handleIoErrors() const;
  137. void writeFooter() const;
  138. void writeHeader() const;
  139. private:
  140. QTextCodec *mpEncoding;
  141. QTextStream *mpWriter;
  142. volatile bool mImmediateFlush;
  143. };
  144. /**************************************************************************
  145. * Operators, Helper
  146. **************************************************************************/
  147. /**************************************************************************
  148. * Inline
  149. **************************************************************************/
  150. inline QTextCodec *WriterAppender::encoding() const
  151. { QMutexLocker locker(&mObjectGuard);
  152. return mpEncoding; }
  153. inline bool WriterAppender::immediateFlush() const
  154. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  155. return mImmediateFlush; }
  156. inline QTextStream *WriterAppender::writer() const
  157. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  158. return mpWriter; }
  159. inline void WriterAppender::setImmediateFlush(bool immediateFlush)
  160. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  161. mImmediateFlush = immediateFlush; }
  162. } // namespace Log4Qt
  163. // Q_DECLARE_TYPEINFO(Log4Qt::WriterAppender, Q_COMPLEX_TYPE); // Use default
  164. #endif // LOG4QT_WRITERAPPENDER_H