consoleappender.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: consoleappender.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_CONSOLEAPPENDER_H
  25. #define LOG4QT_CONSOLEAPPENDER_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 ConsoleAppender appends to stdout or stderr.
  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 ConsoleAppender : public WriterAppender
  46. {
  47. Q_OBJECT
  48. /*!
  49. * The property holds the target used by the appender.
  50. *
  51. * The default is STDOUT_TARGET for the standard output.
  52. *
  53. * \sa Target, target(), setTarget()
  54. */
  55. Q_PROPERTY(QString target READ target WRITE setTarget)
  56. public:
  57. /*!
  58. * The enum defines the possible output targets
  59. *
  60. * \sa target(), setTarget()
  61. */
  62. enum Target {
  63. /*! The output target is standard out. */
  64. STDOUT_TARGET,
  65. /*! The output target is standard error. */
  66. STDERR_TARGET
  67. };
  68. Q_ENUMS(Target)
  69. ConsoleAppender(QObject *pParent = 0);
  70. ConsoleAppender(Layout *pLayout,
  71. QObject *pParent = 0);
  72. ConsoleAppender(Layout *pLayout,
  73. const QString &rTarget,
  74. QObject *pParent = 0);
  75. /*!
  76. * Creates a ConsoleAppender with the layout \a pLayout, the target
  77. * value specified by the \a target constant and the parent
  78. * \a pParent.
  79. */
  80. ConsoleAppender(Layout *pLayout,
  81. Target target,
  82. QObject *pParent = 0);
  83. virtual ~ConsoleAppender();
  84. private:
  85. ConsoleAppender(const ConsoleAppender &rOther); // Not implemented
  86. ConsoleAppender &operator=(const ConsoleAppender &rOther); // Not implemented
  87. public:
  88. // JAVA: bool follow() const;
  89. QString target() const;
  90. // JAVA: void setFollow(bool follow);
  91. void setTarget(const QString &rTarget);
  92. /*!
  93. * Sets the target to the value specified by the \a target constant.
  94. */
  95. void setTarget(Target target);
  96. virtual void activateOptions();
  97. virtual void close();
  98. protected:
  99. void closeStream();
  100. #ifndef QT_NO_DEBUG_STREAM
  101. /*!
  102. * Writes all object member variables to the given debug stream
  103. * \a rDebug and returns the stream.
  104. *
  105. * <tt>
  106. * %ConsoleAppender(name:"CA" filter:0x0 isactive:true isclosed:false
  107. * layout:"PL" target:"STDERR" referenceCount:1
  108. * threshold:"WARN_SET")
  109. * </tt>
  110. * \sa QDebug, operator<<(QDebug debug, const LogObject &rLogObject)
  111. */
  112. virtual QDebug debug(QDebug &rDebug) const;
  113. #endif // QT_NO_DEBUG_STREAM
  114. private:
  115. volatile Target mTarget;
  116. QTextStream *mpTextStream;
  117. };
  118. /**************************************************************************
  119. * Operators, Helper
  120. **************************************************************************/
  121. /**************************************************************************
  122. * Inline
  123. **************************************************************************/
  124. inline void ConsoleAppender::setTarget(Target target)
  125. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  126. mTarget = target; }
  127. } // namespace Log4Qt
  128. // Q_DECLARE_TYPEINFO(Log4Qt::ConsoleAppender, Q_COMPLEX_TYPE); // Use default
  129. #endif // LOG4QT_CONSOLEAPPENDER_H