hierarchy.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: hierarchy.cpp
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * changes: Sep 2008, Martin Heinrich:
  10. * - Fixed problem in Qt 4.4 where QReadWriteLock is by default
  11. * non-recursive.
  12. *
  13. *
  14. * Copyright 2007 - 2008 Martin Heinrich
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. ******************************************************************************/
  29. /******************************************************************************
  30. * Dependencies
  31. ******************************************************************************/
  32. #include "log4qt/hierarchy.h"
  33. #include <QtCore/QDebug>
  34. #include "log4qt/logger.h"
  35. namespace Log4Qt
  36. {
  37. /**************************************************************************
  38. * Declarations
  39. **************************************************************************/
  40. /**************************************************************************
  41. * C helper functions
  42. **************************************************************************/
  43. LOG4QT_DECLARE_STATIC_LOGGER(static_logger, Log4Qt::LoggerRepository)
  44. /**************************************************************************
  45. * Class implementation: Hierarchy
  46. **************************************************************************/
  47. Hierarchy::Hierarchy() :
  48. #if QT_VERSION < QT_VERSION_CHECK(4, 4, 0)
  49. mObjectGuard(),
  50. #else
  51. mObjectGuard(QReadWriteLock::Recursive),
  52. #endif
  53. mLoggers(),
  54. mThreshold(Level::NULL_INT),
  55. mpRootLogger(logger(QString()))
  56. {
  57. // Store root logger to allow rootLogger() to be const
  58. }
  59. Hierarchy::~Hierarchy()
  60. {
  61. static_logger()->warn("Unexpected destruction of Hierarchy");
  62. // QWriteLocker locker(&mObjectGuard);
  63. //
  64. // resetConfiguration();
  65. // clear();
  66. // delete mpRootLogger;
  67. }
  68. bool Hierarchy::exists(const QString &rName) const
  69. {
  70. QReadLocker locker(&mObjectGuard);
  71. return mLoggers.contains(rName);
  72. }
  73. Logger *Hierarchy::logger(const QString &rName)
  74. {
  75. QWriteLocker locker(&mObjectGuard);
  76. return createLogger(rName);
  77. }
  78. QList<Logger *> Hierarchy::loggers() const
  79. {
  80. QReadLocker locker(&mObjectGuard);
  81. return mLoggers.values();
  82. }
  83. void Hierarchy::setThreshold(const QString &rThreshold)
  84. {
  85. setThreshold(Level::fromString(rThreshold));
  86. }
  87. void Hierarchy::resetConfiguration()
  88. {
  89. QWriteLocker locker(&mObjectGuard);
  90. // Reset all loggers.
  91. // Leave log, qt and root logger to the last to allow debugging of shutdown.
  92. Logger *p_logging_logger = logger(QLatin1String("Log4Qt"));
  93. Logger *p_qt_logger = logger(QLatin1String("Qt"));
  94. Logger *p_root_logger = rootLogger();
  95. Logger *p_logger;
  96. Q_FOREACH(p_logger, mLoggers)
  97. {
  98. if ((p_logger == p_logging_logger) || (p_logger == p_qt_logger) || (p_logger == p_root_logger))
  99. continue;
  100. resetLogger(p_logger, Level::NULL_INT);
  101. }
  102. resetLogger(p_qt_logger, Level::NULL_INT);
  103. resetLogger(p_logging_logger, Level::NULL_INT);
  104. resetLogger(p_root_logger, Level::DEBUG_INT);
  105. }
  106. void Hierarchy::shutdown()
  107. {
  108. static_logger()->debug("Shutting down Hierarchy");
  109. resetConfiguration();
  110. }
  111. #ifndef QT_NO_DEBUG_STREAM
  112. QDebug Hierarchy::debug(QDebug &rDebug) const
  113. {
  114. rDebug.nospace() << "Hierarchy("
  115. << "loggers:" << loggers().count() << " "
  116. << "threshold:" << threshold().toString() << " "
  117. << "root-level:" << rootLogger()->level().toString() << " "
  118. << "root-appenders:" << rootLogger()->appenders().count()
  119. << ")";
  120. return rDebug.space();
  121. }
  122. #endif // QT_NO_DEBUG_STREAM
  123. Logger *Hierarchy::createLogger(const QString &rName)
  124. {
  125. // Q_ASSERT_X(, "Hierarchy::createLogger", "Lock must be held by caller")
  126. const QString name_separator = QLatin1String("::");
  127. Logger *p_logger = mLoggers.value(rName, 0);
  128. if (p_logger != 0)
  129. return p_logger;
  130. if (rName.isEmpty())
  131. {
  132. p_logger = new Logger(this, Level::DEBUG_INT, QLatin1String("root"), 0);
  133. mLoggers.insert(QString(), p_logger);
  134. return p_logger;
  135. }
  136. QString parent_name;
  137. int index = rName.lastIndexOf(name_separator);
  138. if (index >=0)
  139. parent_name = rName.left(index);
  140. p_logger = new Logger(this, Level::NULL_INT, rName, createLogger(parent_name));
  141. mLoggers.insert(rName, p_logger);
  142. return p_logger;
  143. }
  144. void Hierarchy::resetLogger(Logger *pLogger, Level level) const
  145. {
  146. // Q_ASSERT_X(, "Hierarchy::resetLogger", "Lock must be held by caller")
  147. pLogger->removeAllAppenders();
  148. pLogger->setAdditivity(true);
  149. pLogger->setLevel(level);
  150. }
  151. /**************************************************************************
  152. * Implementation: Operators, Helper
  153. **************************************************************************/
  154. } // namespace Log4Qt