ctkErrorLogQtMessageHandler.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <QCoreApplication>
  16. #include <QThread>
  17. #include <QVariant>
  18. // CTK includes
  19. #include "ctkErrorLogContext.h"
  20. #include "ctkErrorLogQtMessageHandler.h"
  21. #include <ctkUtils.h>
  22. // STD includes
  23. #include <iostream>
  24. //------------------------------------------------------------------------------
  25. QString ctkErrorLogQtMessageHandler::HandlerName = QLatin1String("Qt");
  26. //------------------------------------------------------------------------------
  27. //Q_DECLARE_METATYPE(QPointer<ctkErrorLogQtMessageHandler>)
  28. Q_DECLARE_METATYPE(ctkErrorLogQtMessageHandler*)
  29. // --------------------------------------------------------------------------
  30. ctkErrorLogQtMessageHandler::ctkErrorLogQtMessageHandler() : Superclass()
  31. {
  32. this->SavedQtMessageHandler = 0;
  33. QCoreApplication * coreApp = QCoreApplication::instance();
  34. // Keep track of all instantiated ctkErrorLogModel
  35. QList<QVariant> handlers = coreApp->property("ctkErrorLogQtMessageHandlers").toList();
  36. handlers << QVariant::fromValue(this);
  37. //handlers << QVariant::fromValue(QPointer<ctkErrorLogQtMessageHandler>(this));
  38. coreApp->setProperty("ctkErrorLogQtMessageHandlers", handlers);
  39. }
  40. namespace
  41. {
  42. //------------------------------------------------------------------------------
  43. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  44. void ctkErrorLogModelQtMessageOutput(QtMsgType type, const QMessageLogContext& context,
  45. const QString& msg)
  46. {
  47. //TODO: use context in the log message
  48. Q_UNUSED(context)
  49. // Warning: To avoid inifinite loop, do not use Q_ASSERT in this function.
  50. if (msg.isEmpty())
  51. {
  52. return;
  53. }
  54. ctkErrorLogLevel::LogLevel level = ctkErrorLogLevel::Unknown;
  55. if (type == QtDebugMsg)
  56. {
  57. level = ctkErrorLogLevel::Debug;
  58. }
  59. else if (type == QtWarningMsg)
  60. {
  61. level = ctkErrorLogLevel::Warning;
  62. }
  63. else if (type == QtCriticalMsg)
  64. {
  65. level = ctkErrorLogLevel::Critical;
  66. }
  67. else if (type == QtFatalMsg)
  68. {
  69. level = ctkErrorLogLevel::Fatal;
  70. }
  71. QCoreApplication * coreApp = QCoreApplication::instance();
  72. QList<QVariant> handlers = coreApp->property("ctkErrorLogQtMessageHandlers").toList();
  73. foreach(QVariant v, handlers)
  74. {
  75. ctkErrorLogQtMessageHandler* handler = v.value<ctkErrorLogQtMessageHandler*>();
  76. Q_ASSERT(handler);
  77. // //QPointer<ctkErrorLogQtMessageHandler> handler = v.value<QPointer<ctkErrorLogQtMessageHandler> >();
  78. // //if(handler.isNull())
  79. // // {
  80. // // continue;
  81. // // }
  82. handler->handleMessage(
  83. ctk::qtHandleToString(QThread::currentThreadId()),
  84. level, handler->handlerPrettyName(), ctkErrorLogContext(msg), msg);
  85. }
  86. }
  87. #else
  88. void ctkErrorLogModelQtMessageOutput(QtMsgType type, const char *msg)
  89. {
  90. // Warning: To avoid inifinite loop, do not use Q_ASSERT in this function.
  91. if (QString(msg).isEmpty())
  92. {
  93. return;
  94. }
  95. ctkErrorLogLevel::LogLevel level = ctkErrorLogLevel::Unknown;
  96. if (type == QtDebugMsg)
  97. {
  98. level = ctkErrorLogLevel::Debug;
  99. }
  100. else if (type == QtWarningMsg)
  101. {
  102. level = ctkErrorLogLevel::Warning;
  103. }
  104. else if (type == QtCriticalMsg)
  105. {
  106. level = ctkErrorLogLevel::Critical;
  107. }
  108. else if (type == QtFatalMsg)
  109. {
  110. level = ctkErrorLogLevel::Fatal;
  111. }
  112. QCoreApplication * coreApp = QCoreApplication::instance();
  113. QList<QVariant> handlers = coreApp->property("ctkErrorLogQtMessageHandlers").toList();
  114. foreach(QVariant v, handlers)
  115. {
  116. ctkErrorLogQtMessageHandler* handler = v.value<ctkErrorLogQtMessageHandler*>();
  117. Q_ASSERT(handler);
  118. // //QPointer<ctkErrorLogQtMessageHandler> handler = v.value<QPointer<ctkErrorLogQtMessageHandler> >();
  119. // //if(handler.isNull())
  120. // // {
  121. // // continue;
  122. // // }
  123. handler->handleMessage(
  124. ctk::qtHandleToString(QThread::currentThreadId()),
  125. level, handler->handlerPrettyName(), ctkErrorLogContext(msg), msg);
  126. }
  127. }
  128. #endif
  129. }
  130. // --------------------------------------------------------------------------
  131. QString ctkErrorLogQtMessageHandler::handlerName()const
  132. {
  133. return ctkErrorLogQtMessageHandler::HandlerName;
  134. }
  135. // --------------------------------------------------------------------------
  136. void ctkErrorLogQtMessageHandler::setEnabledInternal(bool value)
  137. {
  138. if (value)
  139. {
  140. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  141. this->SavedQtMessageHandler = qInstallMessageHandler(ctkErrorLogModelQtMessageOutput);
  142. #else
  143. this->SavedQtMessageHandler = qInstallMsgHandler(ctkErrorLogModelQtMessageOutput);
  144. #endif
  145. }
  146. else
  147. {
  148. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  149. qInstallMessageHandler(this->SavedQtMessageHandler);
  150. #else
  151. qInstallMsgHandler(this->SavedQtMessageHandler);
  152. #endif
  153. }
  154. }