ctkErrorLogQtMessageHandler.cpp 5.2 KB

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