ctkErrorLogQtMessageHandler.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QAtomicInt>
  16. #include <QCoreApplication>
  17. #include <QThread>
  18. #include <QVariant>
  19. // CTK includes
  20. #include "ctkErrorLogContext.h"
  21. #include "ctkErrorLogQtMessageHandler.h"
  22. #include <ctkUtils.h>
  23. // STD includes
  24. #include <iostream>
  25. // Handling log messages may generate log messages, which would cause infinite loop.
  26. // We stop handling log messages if the maximum recursion depth is reached.
  27. static QAtomicInt ctkErrorLogQtMessageHandler_CurrentRecursionDepth;
  28. //------------------------------------------------------------------------------
  29. QString ctkErrorLogQtMessageHandler::HandlerName = QLatin1String("Qt");
  30. //------------------------------------------------------------------------------
  31. //Q_DECLARE_METATYPE(QPointer<ctkErrorLogQtMessageHandler>)
  32. Q_DECLARE_METATYPE(ctkErrorLogQtMessageHandler*)
  33. // --------------------------------------------------------------------------
  34. ctkErrorLogQtMessageHandler::ctkErrorLogQtMessageHandler() : Superclass()
  35. {
  36. this->SavedQtMessageHandler = 0;
  37. QCoreApplication * coreApp = QCoreApplication::instance();
  38. // Keep track of all instantiated ctkErrorLogModel
  39. QList<QVariant> handlers = coreApp->property("ctkErrorLogQtMessageHandlers").toList();
  40. handlers << QVariant::fromValue(this);
  41. //handlers << QVariant::fromValue(QPointer<ctkErrorLogQtMessageHandler>(this));
  42. coreApp->setProperty("ctkErrorLogQtMessageHandlers", handlers);
  43. }
  44. namespace
  45. {
  46. //------------------------------------------------------------------------------
  47. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  48. void ctkErrorLogModelQtMessageOutput(QtMsgType type, const QMessageLogContext& context,
  49. const QString& msg)
  50. {
  51. ctkErrorLogQtMessageHandler_CurrentRecursionDepth.ref();
  52. // Allow a couple of recursion levels to get a hint about where and why recursion occurs,
  53. // so we stop processing the message if recursion depth is over 10.
  54. if (ctkErrorLogQtMessageHandler_CurrentRecursionDepth.load() > 10)
  55. {
  56. ctkErrorLogQtMessageHandler_CurrentRecursionDepth.deref();
  57. return;
  58. }
  59. //TODO: use context in the log message
  60. Q_UNUSED(context)
  61. // Warning: To avoid inifinite loop, do not use Q_ASSERT in this function.
  62. if (msg.isEmpty())
  63. {
  64. ctkErrorLogQtMessageHandler_CurrentRecursionDepth.deref();
  65. return;
  66. }
  67. ctkErrorLogLevel::LogLevel level = ctkErrorLogLevel::Unknown;
  68. if (type == QtDebugMsg)
  69. {
  70. level = ctkErrorLogLevel::Debug;
  71. }
  72. #if QT_VERSION >= QT_VERSION_CHECK(5,5,0)
  73. else if (type == QtInfoMsg)
  74. {
  75. level = ctkErrorLogLevel::Info;
  76. }
  77. #endif
  78. else if (type == QtWarningMsg)
  79. {
  80. level = ctkErrorLogLevel::Warning;
  81. }
  82. else if (type == QtCriticalMsg)
  83. {
  84. level = ctkErrorLogLevel::Critical;
  85. }
  86. else if (type == QtFatalMsg)
  87. {
  88. level = ctkErrorLogLevel::Fatal;
  89. }
  90. QCoreApplication * coreApp = QCoreApplication::instance();
  91. QList<QVariant> handlers = coreApp->property("ctkErrorLogQtMessageHandlers").toList();
  92. foreach(QVariant v, handlers)
  93. {
  94. ctkErrorLogQtMessageHandler* handler = v.value<ctkErrorLogQtMessageHandler*>();
  95. Q_ASSERT(handler);
  96. // //QPointer<ctkErrorLogQtMessageHandler> handler = v.value<QPointer<ctkErrorLogQtMessageHandler> >();
  97. // //if(handler.isNull())
  98. // // {
  99. // // continue;
  100. // // }
  101. handler->handleMessage(
  102. ctk::qtHandleToString(QThread::currentThreadId()),
  103. level, handler->handlerPrettyName(), ctkErrorLogContext(msg), msg);
  104. }
  105. ctkErrorLogQtMessageHandler_CurrentRecursionDepth.deref();
  106. }
  107. #else
  108. void ctkErrorLogModelQtMessageOutput(QtMsgType type, const char *msg)
  109. {
  110. ctkErrorLogQtMessageHandler_CurrentRecursionDepth.ref();
  111. // Allow a couple of recursion levels to get a hint about where and why recursion occurs,
  112. // so we stop processing the message if recursion depth is over 10.
  113. if (ctkErrorLogQtMessageHandler_CurrentRecursionDepth > 10)
  114. {
  115. ctkErrorLogQtMessageHandler_CurrentRecursionDepth.deref();
  116. return;
  117. }
  118. // Warning: To avoid inifinite loop, do not use Q_ASSERT in this function.
  119. if (QString(msg).isEmpty())
  120. {
  121. ctkErrorLogQtMessageHandler_CurrentRecursionDepth.deref();
  122. return;
  123. }
  124. ctkErrorLogLevel::LogLevel level = ctkErrorLogLevel::Unknown;
  125. if (type == QtDebugMsg)
  126. {
  127. level = ctkErrorLogLevel::Debug;
  128. }
  129. else if (type == QtWarningMsg)
  130. {
  131. level = ctkErrorLogLevel::Warning;
  132. }
  133. else if (type == QtCriticalMsg)
  134. {
  135. level = ctkErrorLogLevel::Critical;
  136. }
  137. else if (type == QtFatalMsg)
  138. {
  139. level = ctkErrorLogLevel::Fatal;
  140. }
  141. QCoreApplication * coreApp = QCoreApplication::instance();
  142. QList<QVariant> handlers = coreApp->property("ctkErrorLogQtMessageHandlers").toList();
  143. foreach(QVariant v, handlers)
  144. {
  145. ctkErrorLogQtMessageHandler* handler = v.value<ctkErrorLogQtMessageHandler*>();
  146. Q_ASSERT(handler);
  147. // //QPointer<ctkErrorLogQtMessageHandler> handler = v.value<QPointer<ctkErrorLogQtMessageHandler> >();
  148. // //if(handler.isNull())
  149. // // {
  150. // // continue;
  151. // // }
  152. handler->handleMessage(
  153. ctk::qtHandleToString(QThread::currentThreadId()),
  154. level, handler->handlerPrettyName(), ctkErrorLogContext(msg), msg);
  155. }
  156. ctkErrorLogQtMessageHandler_CurrentRecursionDepth.deref();
  157. }
  158. #endif
  159. }
  160. // --------------------------------------------------------------------------
  161. QString ctkErrorLogQtMessageHandler::handlerName()const
  162. {
  163. return ctkErrorLogQtMessageHandler::HandlerName;
  164. }
  165. // --------------------------------------------------------------------------
  166. void ctkErrorLogQtMessageHandler::setEnabledInternal(bool value)
  167. {
  168. if (value)
  169. {
  170. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  171. this->SavedQtMessageHandler = qInstallMessageHandler(ctkErrorLogModelQtMessageOutput);
  172. #else
  173. this->SavedQtMessageHandler = qInstallMsgHandler(ctkErrorLogModelQtMessageOutput);
  174. #endif
  175. }
  176. else
  177. {
  178. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  179. qInstallMessageHandler(this->SavedQtMessageHandler);
  180. #else
  181. qInstallMsgHandler(this->SavedQtMessageHandler);
  182. #endif
  183. }
  184. }