ctkErrorLogQtMessageHandler.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // CTK includes
  17. #include "ctkErrorLogQtMessageHandler.h"
  18. // STD includes
  19. #include <iostream>
  20. //------------------------------------------------------------------------------
  21. QString ctkErrorLogQtMessageHandler::HandlerName = QLatin1String("Qt");
  22. //------------------------------------------------------------------------------
  23. //Q_DECLARE_METATYPE(QPointer<ctkErrorLogQtMessageHandler>)
  24. Q_DECLARE_METATYPE(ctkErrorLogQtMessageHandler*)
  25. // --------------------------------------------------------------------------
  26. ctkErrorLogQtMessageHandler::ctkErrorLogQtMessageHandler() : Superclass()
  27. {
  28. this->SavedQtMessageHandler = 0;
  29. QCoreApplication * coreApp = QCoreApplication::instance();
  30. // Keep track of all instantiated ctkErrorLogModel
  31. QList<QVariant> handlers = coreApp->property("ctkErrorLogQtMessageHandlers").toList();
  32. handlers << QVariant::fromValue(this);
  33. //handlers << QVariant::fromValue(QPointer<ctkErrorLogQtMessageHandler>(this));
  34. coreApp->setProperty("ctkErrorLogQtMessageHandlers", handlers);
  35. }
  36. namespace
  37. {
  38. //------------------------------------------------------------------------------
  39. void ctkErrorLogModelQtMessageOutput(QtMsgType type, const char *msg)
  40. {
  41. // Warning: To avoid inifinite loop, do not use Q_ASSERT in this function.
  42. if (QString(msg).isEmpty())
  43. {
  44. return;
  45. }
  46. ctkErrorLogModel::LogLevel level = ctkErrorLogModel::Unknown;
  47. if (type == QtDebugMsg)
  48. {
  49. level = ctkErrorLogModel::Debug;
  50. }
  51. else if (type == QtWarningMsg)
  52. {
  53. level = ctkErrorLogModel::Warning;
  54. }
  55. else if (type == QtCriticalMsg)
  56. {
  57. level = ctkErrorLogModel::Critical;
  58. }
  59. else if (type == QtFatalMsg)
  60. {
  61. level = ctkErrorLogModel::Fatal;
  62. }
  63. QCoreApplication * coreApp = QCoreApplication::instance();
  64. QList<QVariant> handlers = coreApp->property("ctkErrorLogQtMessageHandlers").toList();
  65. foreach(QVariant v, handlers)
  66. {
  67. ctkErrorLogQtMessageHandler* handler = v.value<ctkErrorLogQtMessageHandler*>();
  68. Q_ASSERT(handler);
  69. // //QPointer<ctkErrorLogQtMessageHandler> handler = v.value<QPointer<ctkErrorLogQtMessageHandler> >();
  70. // //if(handler.isNull())
  71. // // {
  72. // // continue;
  73. // // }
  74. if (!handler->errorLogModel())
  75. {
  76. std::cout << "ErrorLogModel is Null !" << std::endl;
  77. return;
  78. }
  79. handler->errorLogModel()->addEntry(level, handler->handlerPrettyName(), msg);
  80. }
  81. }
  82. }
  83. // --------------------------------------------------------------------------
  84. QString ctkErrorLogQtMessageHandler::handlerName()const
  85. {
  86. return ctkErrorLogQtMessageHandler::HandlerName;
  87. }
  88. // --------------------------------------------------------------------------
  89. void ctkErrorLogQtMessageHandler::setEnabledInternal(bool value)
  90. {
  91. if (value)
  92. {
  93. this->SavedQtMessageHandler = qInstallMsgHandler(ctkErrorLogModelQtMessageOutput);
  94. }
  95. else
  96. {
  97. qInstallMsgHandler(this->SavedQtMessageHandler);
  98. }
  99. }