ctkErrorLogQtMessageHandler.cpp 3.6 KB

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