level.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: level.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_LEVEL_H
  25. #define LOG4QT_LEVEL_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include <QtCore/QString>
  30. #include <QtCore/QMetaType>
  31. #include "log4qt/log4qt.h"
  32. /******************************************************************************
  33. * Declarations
  34. ******************************************************************************/
  35. namespace Log4Qt
  36. {
  37. /*!
  38. * \brief The class Level defines the level of a logging event.
  39. *
  40. * \note All the functions declared in this class are thread-safe.
  41. */
  42. class Level
  43. {
  44. public:
  45. // Comparisson operators rely on the order:
  46. // NULL_INT < ALL_INT < TRACE_INT < ...
  47. // Serialisation uses unsigned 8 bit int
  48. /*!
  49. * The enumeration Value contains all possible Level values.
  50. */
  51. enum Value
  52. {
  53. /*! NULL_INT is used for no level has been specified */
  54. NULL_INT = 0,
  55. ALL_INT = 32,
  56. TRACE_INT = 64,
  57. DEBUG_INT = 96,
  58. INFO_INT = 128,
  59. WARN_INT = 150,
  60. ERROR_INT = 182,
  61. FATAL_INT = 214,
  62. OFF_INT = 255
  63. };
  64. public:
  65. Level(Value value = NULL_INT);
  66. // Level(const Level &rOther); // Use compiler default
  67. // virtual ~Level(); // Use compiler default
  68. // Level &operator=(const Level &rOther); // Use compiler default
  69. int syslogEquivalent() const;
  70. int toInt() const;
  71. bool operator==(const Level &rOther) const;
  72. bool operator!=(const Level &rOther) const;
  73. bool operator<(const Level &rOther) const;
  74. bool operator<=(const Level &rOther) const;
  75. bool operator>(const Level &rOther) const;
  76. bool operator>=(const Level &rOther) const;
  77. QString toString() const;
  78. static Level fromString(const QString &rName, bool *pOk = 0);
  79. private:
  80. // QMutex mObjectGuard;
  81. volatile Value mValue;
  82. #ifndef QT_NO_DATASTREAM
  83. // Needs to be friend to stream objects
  84. friend QDataStream &operator<<(QDataStream &rStream,
  85. const Level &rLevel);
  86. friend QDataStream &operator>>(QDataStream &rStream,
  87. Level &rLevel);
  88. #endif // QT_NO_DATASTREAM
  89. };
  90. /**************************************************************************
  91. * Operators, Helper
  92. **************************************************************************/
  93. #ifndef QT_NO_DATASTREAM
  94. /*!
  95. * \relates Level
  96. *
  97. * Writes the given error \a rLevel to the given stream \a rStream,
  98. * and returns a reference to the stream.
  99. */
  100. QDataStream &operator<<(QDataStream &rStream,
  101. const Level &rLevel);
  102. /*!
  103. * \relates Level
  104. *
  105. * Reads an error from the given stream \a rStream into the given
  106. * error \a rLevel, and returns a reference to the stream.
  107. */
  108. QDataStream &operator>>(QDataStream &rStream,
  109. Level &rLevel);
  110. #endif // QT_NO_DATASTREAM
  111. #ifndef QT_NO_DEBUG_STREAM
  112. /*!
  113. * \relates Level
  114. *
  115. * Writes all object member variables to the given debug stream \a rDebug
  116. * and returns the stream.
  117. *
  118. * <tt>
  119. * %Level("ERROR")
  120. * </tt>
  121. * \sa QDebug
  122. */
  123. QDebug operator<<(QDebug debug,
  124. const Level &rLevel);
  125. #endif // QT_NO_DEBUG_STREAM
  126. /**************************************************************************
  127. * Inline
  128. **************************************************************************/
  129. inline Level::Level(Value value) :
  130. mValue(value)
  131. {}
  132. inline int Level::toInt() const
  133. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  134. return mValue; }
  135. inline bool Level::operator==(const Level &rOther) const
  136. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  137. return mValue == rOther.mValue; }
  138. inline bool Level::operator!=(const Level &rOther) const
  139. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  140. return mValue != rOther.mValue; }
  141. inline bool Level::operator<(const Level &rOther) const
  142. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  143. return mValue < rOther.mValue; }
  144. inline bool Level::operator<=(const Level &rOther) const
  145. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  146. return mValue <= rOther.mValue; }
  147. inline bool Level::operator>(const Level &rOther) const
  148. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  149. return mValue > rOther.mValue; }
  150. inline bool Level::operator>=(const Level &rOther) const
  151. { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  152. return mValue >= rOther.mValue; }
  153. } // namespace Log4Qt
  154. Q_DECLARE_METATYPE(Log4Qt::Level)
  155. Q_DECLARE_TYPEINFO(Log4Qt::Level, Q_MOVABLE_TYPE);
  156. #endif // LOG4QT_LEVEL_H