ctkErrorLogQtMessageHandler.cpp 5.2 KB

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