fileappender.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: fileappender.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_FILEAPPENDER_H
  25. #define LOG4QT_FILEAPPENDER_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include "log4qt/writerappender.h"
  30. /******************************************************************************
  31. * Declarations
  32. ******************************************************************************/
  33. class QFile;
  34. class QTextStream;
  35. namespace Log4Qt
  36. {
  37. /*!
  38. * \brief The class FileAppender appends log events to a file.
  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. See
  43. * \ref Ownership "Object ownership" for more details.
  44. */
  45. class FileAppender : public WriterAppender
  46. {
  47. Q_OBJECT
  48. /*!
  49. * The property holds, if the output is appended to the file.
  50. *
  51. * The default is false for not appending.
  52. *
  53. * \sa appendFile(), setAppendFile()
  54. */
  55. Q_PROPERTY(bool appendFile READ appendFile WRITE setAppendFile)
  56. /*!
  57. * The property holds, if the output is buffered.
  58. *
  59. * The default is true for buffering.
  60. *
  61. * \sa bufferedIo(), setBufferedIo()
  62. */
  63. Q_PROPERTY(bool bufferedIo READ bufferedIo WRITE setBufferedIo)
  64. /*!
  65. * The property holds the name of the file.
  66. *
  67. * \sa file(), setFile()
  68. */
  69. Q_PROPERTY(QString file READ file WRITE setFile)
  70. public:
  71. FileAppender(QObject *pParent = 0);
  72. FileAppender(Layout *pLayout,
  73. const QString &rFileName,
  74. QObject *pParent = 0);
  75. FileAppender(Layout *pLayout,
  76. const QString &rFileName,
  77. bool append,
  78. QObject *pParent = 0);
  79. FileAppender(Layout *pLayout,
  80. const QString &rFileName,
  81. bool append,
  82. bool buffered,
  83. QObject *pParent = 0);
  84. virtual ~FileAppender();
  85. private:
  86. FileAppender(const FileAppender &rOther); // Not implemented
  87. FileAppender &operator=(const FileAppender &rOther); // Not implemented
  88. public:
  89. bool appendFile() const;
  90. QString file() const;
  91. bool bufferedIo() const;
  92. // JAVA: int bufferSize() const;
  93. void setAppendFile(bool append);
  94. void setBufferedIo(bool buffered);
  95. // JAVA: void setBufferSize(int bufferSize);
  96. void setFile(const QString &rFileName);
  97. virtual void activateOptions();
  98. virtual void close();
  99. protected:
  100. /*!
  101. * Tests if all entry conditions for using append() in this class are met.
  102. *
  103. * If a conditions is not met, an error is logged and the function returns
  104. * false. Otherwise the result of WriterAppender::checkEntryConditions()
  105. * is returned.
  106. *
  107. * The checked conditions are:
  108. * - That a file is set and open (APPENDER_NO_OPEN_FILE_ERROR)
  109. *
  110. * The function is called as part of the checkEntryConditions() chain
  111. * started by AppenderSkeleton::doAppend().
  112. *
  113. * \sa AppenderSkeleton::doAppend(), AppenderSkeleton::checkEntryConditions()
  114. */
  115. virtual bool checkEntryConditions() const;
  116. void closeFile();
  117. #ifndef QT_NO_DEBUG_STREAM
  118. /*!
  119. * Writes all object member variables to the given debug stream \a rDebug
  120. * and returns the stream.
  121. *
  122. * <tt>
  123. * %FileAppender(name:"FA" appendfile:false bufferedio:true encoding:""
  124. * file:"/log.txt" filter: 0x0 immediateflush:true isactive:false
  125. * isclosed:false layout:"TTCC" referencecount:2
  126. * threshold:"NULL" writer:0x0)
  127. * </tt>
  128. * \sa QDebug, operator<<(QDebug debug, const LogObject &rLogObject)
  129. */
  130. virtual QDebug debug(QDebug &rDebug) const;
  131. #endif // QT_NO_DEBUG_STREAM
  132. /*!
  133. * Checks for file I/O errrors. If an error is found it is logged and the
  134. * function returns true. Otherwise false is returned.
  135. */
  136. virtual bool handleIoErrors() const;
  137. /*!
  138. * Opens the file for the appender based on the specified file name and
  139. * mode. A text stream is created and passed on to the super class
  140. * WriterAppender.
  141. *
  142. * If the parent directory of the specified file does not exists,
  143. * it is created.
  144. */
  145. void openFile();
  146. /*!
  147. * Removes the file \a rFile. If the operation is successful, true is
  148. * returned. Otherwise an APPENDER_REMOVE_FILE_ERROR error is logged
  149. * and false is returned.
  150. */
  151. bool removeFile(QFile &rFile) const;
  152. /*!
  153. * Renames the file \a rFile to \a rFileName. If the operation is
  154. * successful, true is returned. Otherwise an
  155. * APPENDER_RENAMING_FILE_ERROR error is logged and false is returned.
  156. */
  157. bool renameFile(QFile &rFile,
  158. const QString &rFileName) const;
  159. // JAVA: void setQWForFiles(Writer writer);
  160. private:
  161. volatile bool mAppendFile;
  162. volatile bool mBufferedIo;
  163. QString mFileName;
  164. QFile *mpFile;
  165. QTextStream *mpTextStream;
  166. };
  167. /**************************************************************************
  168. * Operators, Helper
  169. **************************************************************************/
  170. /**************************************************************************
  171. * Inline
  172. **************************************************************************/
  173. inline bool FileAppender::appendFile() const
  174. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  175. return mAppendFile; }
  176. inline QString FileAppender::file() const
  177. { QMutexLocker locker(&mObjectGuard);
  178. return mFileName; }
  179. inline bool FileAppender::bufferedIo() const
  180. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  181. return mBufferedIo; }
  182. inline void FileAppender::setAppendFile(bool append)
  183. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  184. mAppendFile = append; }
  185. inline void FileAppender::setBufferedIo(bool buffered)
  186. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  187. mBufferedIo = buffered; }
  188. inline void FileAppender::setFile(const QString &rFileName)
  189. { QMutexLocker locker(&mObjectGuard);
  190. mFileName = rFileName; }
  191. } // namespace Log4Qt
  192. // Q_DECLARE_TYPEINFO(Log4Qt::FileAppender, Q_COMPLEX_TYPE); // Use default
  193. #endif // LOG4QT_FILEAPPENDER_H