writerappender.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: writerappender.cpp
  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. /******************************************************************************
  25. * Dependencies
  26. ******************************************************************************/
  27. #include "log4qt/writerappender.h"
  28. #include <QtCore/QDebug>
  29. #include <QtCore/QTextCodec>
  30. #include "log4qt/layout.h"
  31. #include "log4qt/loggingevent.h"
  32. namespace Log4Qt
  33. {
  34. /**************************************************************************
  35. * Declarations
  36. **************************************************************************/
  37. /**************************************************************************
  38. * C helper functions
  39. **************************************************************************/
  40. /**************************************************************************
  41. * Class implementation: WriterAppender
  42. **************************************************************************/
  43. WriterAppender::WriterAppender(QObject *pParent) :
  44. AppenderSkeleton(false, pParent),
  45. mpEncoding(0),
  46. mpWriter(0),
  47. mImmediateFlush(true)
  48. {
  49. }
  50. WriterAppender::WriterAppender(Layout *pLayout,
  51. QObject *pParent) :
  52. AppenderSkeleton(false, pParent),
  53. mpEncoding(0),
  54. mpWriter(0),
  55. mImmediateFlush(true)
  56. {
  57. setLayout(pLayout);
  58. }
  59. WriterAppender::WriterAppender(Layout *pLayout,
  60. QTextStream *pTextStream,
  61. QObject *pParent) :
  62. AppenderSkeleton(false, pParent),
  63. mpEncoding(0),
  64. mpWriter(pTextStream),
  65. mImmediateFlush(true)
  66. {
  67. setLayout(pLayout);
  68. }
  69. WriterAppender::~WriterAppender()
  70. {
  71. close();
  72. }
  73. void WriterAppender::setEncoding(QTextCodec *pEncoding)
  74. {
  75. QMutexLocker locker(&mObjectGuard);
  76. if (mpEncoding == pEncoding)
  77. return;
  78. mpEncoding = pEncoding;
  79. if (mpWriter)
  80. {
  81. if (mpEncoding)
  82. mpWriter->setCodec(mpEncoding);
  83. else
  84. mpWriter->setCodec(QTextCodec::codecForLocale());
  85. }
  86. }
  87. void WriterAppender::setWriter(QTextStream *pTextStream)
  88. {
  89. QMutexLocker locker(&mObjectGuard);
  90. closeWriter();
  91. mpWriter = pTextStream;
  92. if (mpEncoding && mpWriter)
  93. mpWriter->setCodec(mpEncoding);
  94. writeHeader();
  95. }
  96. void WriterAppender::activateOptions()
  97. {
  98. QMutexLocker locker(&mObjectGuard);
  99. if (!writer())
  100. {
  101. LogError e = LOG4QT_QCLASS_ERROR(QT_TR_NOOP("Activation of Appender '%1' that requires writer and has no writer set"),
  102. APPENDER_ACTIVATE_MISSING_WRITER_ERROR);
  103. e << name();
  104. logger()->error(e);
  105. return;
  106. }
  107. AppenderSkeleton::activateOptions();
  108. }
  109. void WriterAppender::close()
  110. {
  111. QMutexLocker locker(&mObjectGuard);
  112. if (isClosed())
  113. return;
  114. AppenderSkeleton::close();
  115. closeWriter();
  116. }
  117. bool WriterAppender::requiresLayout() const
  118. {
  119. return true;
  120. }
  121. void WriterAppender::append(const LoggingEvent &rEvent)
  122. {
  123. // Q_ASSERT_X(, "WriterAppender::append()", "Lock must be held by caller");
  124. Q_ASSERT_X(layout(), "WriterAppender::append()", "Layout must not be null");
  125. QString message(layout()->format(rEvent));
  126. *mpWriter << message;
  127. if (handleIoErrors())
  128. return;
  129. if (immediateFlush())
  130. {
  131. mpWriter->flush();
  132. if (handleIoErrors())
  133. return;
  134. }
  135. }
  136. bool WriterAppender::checkEntryConditions() const
  137. {
  138. // Q_ASSERT_X(, "WriterAppender::checkEntryConditions()", "Lock must be held by caller")
  139. if (!writer())
  140. {
  141. LogError e = LOG4QT_QCLASS_ERROR(QT_TR_NOOP("Use of appender '%1' without a writer set"),
  142. APPENDER_USE_MISSING_WRITER_ERROR);
  143. e << name();
  144. logger()->error(e);
  145. return false;
  146. }
  147. return AppenderSkeleton::checkEntryConditions();
  148. }
  149. void WriterAppender::closeWriter()
  150. {
  151. // Q_ASSERT_X(, "WriterAppender::closeWriter()", "Lock must be held by caller")
  152. if (!mpWriter)
  153. return;
  154. writeFooter();
  155. mpWriter = 0;
  156. }
  157. #ifndef QT_NO_DEBUG_STREAM
  158. QDebug WriterAppender::debug(QDebug &rDebug) const
  159. {
  160. QString layout_name;
  161. if (layout())
  162. layout_name = layout()->name();
  163. QString codec_name;
  164. if (encoding())
  165. codec_name = QLatin1String(encoding()->name());
  166. rDebug.nospace() << "WriterAppender("
  167. << "name:" << name() << " "
  168. << "encoding:" << codec_name << " "
  169. << "filter:" << firstFilter()
  170. << "immediateFlush:" << immediateFlush()
  171. << "isactive:" << isActive()
  172. << "isclosed:" << isClosed()
  173. << "layout:" << layout_name
  174. << "referencecount:" << referenceCount() << " "
  175. << "threshold:" << threshold().toString()
  176. << "writer:" << writer()
  177. << ")";
  178. return rDebug.space();
  179. }
  180. #endif // QT_NO_DEBUG_STREAM
  181. bool WriterAppender::handleIoErrors() const
  182. {
  183. return false;
  184. }
  185. void WriterAppender::writeFooter() const
  186. {
  187. // Q_ASSERT_X(, "WriterAppender::writeFooter()", "Lock must be held by caller")
  188. if (!layout() || !mpWriter)
  189. return;
  190. QString footer = layout()->footer();
  191. if (footer.isEmpty())
  192. return;
  193. *mpWriter << footer << Layout::endOfLine();
  194. if (handleIoErrors())
  195. return;
  196. }
  197. void WriterAppender::writeHeader() const
  198. {
  199. // Q_ASSERT_X(, "WriterAppender::writeHeader()", "Lock must be held by caller")
  200. if (!layout() || !mpWriter)
  201. return;
  202. QString header = layout()->header();
  203. if (header.isEmpty())
  204. return;
  205. *mpWriter << header << Layout::endOfLine();
  206. if (handleIoErrors())
  207. return;
  208. }
  209. /******************************************************************************
  210. * Implementation: Operators, Helper
  211. ******************************************************************************/
  212. } // namespace Log4Qt