level.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: level.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/level.h"
  28. #include <QtCore/QCoreApplication>
  29. #include <QtCore/QDataStream>
  30. #include <QtCore/QDebug>
  31. #include "log4qt/logger.h"
  32. namespace Log4Qt
  33. {
  34. /**************************************************************************
  35. * Declarations
  36. **************************************************************************/
  37. /**************************************************************************
  38. * C helper functions
  39. **************************************************************************/
  40. LOG4QT_DECLARE_STATIC_LOGGER(logger, Log4Qt::Level)
  41. /**************************************************************************
  42. * Class implementation: Level
  43. **************************************************************************/
  44. int Level::syslogEquivalent() const
  45. {
  46. // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  47. switch (mValue)
  48. {
  49. case NULL_INT:
  50. case ALL_INT:
  51. case TRACE_INT:
  52. case DEBUG_INT:
  53. return 7;
  54. case INFO_INT:
  55. return 6;
  56. case WARN_INT:
  57. return 4;
  58. case ERROR_INT:
  59. return 3;
  60. case FATAL_INT:
  61. case OFF_INT:
  62. return 0;
  63. default:
  64. Q_ASSERT_X(false, "Level::syslogEquivalent()", "Unknown level value");
  65. return 7;
  66. }
  67. }
  68. QString Level::toString() const
  69. {
  70. // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe
  71. const char *p_context = "Level";
  72. switch (mValue)
  73. {
  74. case NULL_INT:
  75. return QCoreApplication::translate(p_context, "NULL");
  76. case ALL_INT:
  77. return QCoreApplication::translate(p_context, "ALL");
  78. case TRACE_INT:
  79. return QCoreApplication::translate(p_context, "TRACE");
  80. case DEBUG_INT:
  81. return QCoreApplication::translate(p_context, "DEBUG");
  82. case INFO_INT:
  83. return QCoreApplication::translate(p_context, "INFO");
  84. case WARN_INT:
  85. return QCoreApplication::translate(p_context, "WARN");
  86. case ERROR_INT:
  87. return QCoreApplication::translate(p_context, "ERROR");
  88. case FATAL_INT:
  89. return QCoreApplication::translate(p_context, "FATAL");
  90. case OFF_INT:
  91. return QCoreApplication::translate(p_context, "OFF");
  92. default:
  93. Q_ASSERT_X(false, "Level::toString()", "Unknown level value");
  94. return QCoreApplication::translate(p_context, "NULL");
  95. }
  96. }
  97. Level Level::fromString(const QString &rLevel, bool *pOk)
  98. {
  99. const char *p_context = "Level";
  100. if (pOk)
  101. *pOk = true;
  102. if (rLevel == QLatin1String("OFF") ||
  103. rLevel == QCoreApplication::translate(p_context, "OFF"))
  104. return OFF_INT;
  105. if (rLevel == QLatin1String("FATAL") ||
  106. rLevel == QCoreApplication::translate(p_context, "FATAL"))
  107. return FATAL_INT;
  108. if (rLevel == QLatin1String("ERROR") ||
  109. rLevel == QCoreApplication::translate(p_context, "ERROR"))
  110. return ERROR_INT;
  111. if (rLevel == QLatin1String("WARN") ||
  112. rLevel == QCoreApplication::translate(p_context, "WARN"))
  113. return WARN_INT;
  114. if (rLevel == QLatin1String("INFO") ||
  115. rLevel == QCoreApplication::translate(p_context, "INFO"))
  116. return INFO_INT;
  117. if (rLevel == QLatin1String("DEBUG") ||
  118. rLevel == QCoreApplication::translate(p_context, "DEBUG"))
  119. return DEBUG_INT;
  120. if (rLevel == QLatin1String("TRACE") ||
  121. rLevel == QCoreApplication::translate(p_context, "TRACE"))
  122. return TRACE_INT;
  123. if (rLevel == QLatin1String("ALL") ||
  124. rLevel == QCoreApplication::translate(p_context, "ALL"))
  125. return ALL_INT;
  126. if (rLevel == QLatin1String("NULL") ||
  127. rLevel == QCoreApplication::translate(p_context, "NULL"))
  128. return NULL_INT;
  129. logger()->warn("Use of invalid level string '%1'. Using 'Level::NULL_INT' instead.", rLevel);
  130. if (pOk)
  131. *pOk = false;
  132. return NULL_INT;
  133. }
  134. /**************************************************************************
  135. * Implementation: Operators, Helper
  136. **************************************************************************/
  137. #ifndef QT_NO_DATASTREAM
  138. QDataStream &operator<<(QDataStream &rStream,
  139. const Level &rLevel)
  140. {
  141. quint8 l = rLevel.mValue;
  142. rStream << l;
  143. return rStream;
  144. }
  145. QDataStream &operator>>(QDataStream &rStream,
  146. Level &rLevel)
  147. {
  148. quint8 l;
  149. rStream >> l;
  150. rLevel.mValue = (Level::Value)l;
  151. return rStream;
  152. }
  153. #endif // QT_NO_DATASTREAM
  154. #ifndef QT_NO_DEBUG_STREAM
  155. QDebug operator<<(QDebug debug,
  156. const Level &rLevel)
  157. {
  158. debug.nospace() << "Level("
  159. << rLevel.toString()
  160. << ")";
  161. return debug.space();
  162. }
  163. #endif // QT_NO_DEBUG_STREAM
  164. } // namespace Log4Qt