ctkErrorLogQtMessageHandler.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. void ctkErrorLogModelQtMessageOutput(QtMsgType type,
  42. const QMessageLogContext& context,
  43. const QString& msg)
  44. {
  45. // Warning: To avoid inifinite loop, do not use Q_ASSERT in this function.
  46. if (msg.isEmpty())
  47. {
  48. return;
  49. }
  50. ctkErrorLogLevel::LogLevel level = ctkErrorLogLevel::Unknown;
  51. if (type == QtDebugMsg)
  52. {
  53. level = ctkErrorLogLevel::Debug;
  54. }
  55. else if (type == QtWarningMsg)
  56. {
  57. level = ctkErrorLogLevel::Warning;
  58. }
  59. else if (type == QtCriticalMsg)
  60. {
  61. level = ctkErrorLogLevel::Critical;
  62. }
  63. else if (type == QtFatalMsg)
  64. {
  65. level = ctkErrorLogLevel::Fatal;
  66. }
  67. QCoreApplication * coreApp = QCoreApplication::instance();
  68. QList<QVariant> handlers = coreApp->property("ctkErrorLogQtMessageHandlers").toList();
  69. foreach(QVariant v, handlers)
  70. {
  71. ctkErrorLogQtMessageHandler* handler = v.value<ctkErrorLogQtMessageHandler*>();
  72. Q_ASSERT(handler);
  73. // //QPointer<ctkErrorLogQtMessageHandler> handler = v.value<QPointer<ctkErrorLogQtMessageHandler> >();
  74. // //if(handler.isNull())
  75. // // {
  76. // // continue;
  77. // // }
  78. handler->handleMessage(
  79. ctk::qtHandleToString(QThread::currentThreadId()),
  80. level, handler->handlerPrettyName(), msg);
  81. }
  82. }
  83. }
  84. // --------------------------------------------------------------------------
  85. QString ctkErrorLogQtMessageHandler::handlerName()const
  86. {
  87. return ctkErrorLogQtMessageHandler::HandlerName;
  88. }
  89. // --------------------------------------------------------------------------
  90. void ctkErrorLogQtMessageHandler::setEnabledInternal(bool value)
  91. {
  92. if (value)
  93. {
  94. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  95. this->SavedQtMessageHandler = qInstallMessageHandler(ctkErrorLogModelQtMessageOutput);
  96. #else
  97. this->SavedQtMessageHandler = qInstallMsgHandler(ctkErrorLogModelQtMessageOutput);
  98. #endif
  99. }
  100. else
  101. {
  102. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  103. qInstallMessageHandler(this->SavedQtMessageHandler);
  104. #else
  105. qInstallMsgHandler(this->SavedQtMessageHandler);
  106. #endif
  107. }
  108. }