ctkITKErrorLogMessageHandler.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <QThread>
  16. // CTK includes
  17. #include "ctkITKErrorLogMessageHandler.h"
  18. #include "ctkUtils.h"
  19. #ifdef __GNUC__
  20. // Disable warnings related to 'itkSmartPointer.h' file
  21. // See http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
  22. // Note: Ideally the incriminated functions and macros should be fixed upstream ...
  23. # pragma GCC diagnostic ignored "-Wold-style-cast"
  24. #endif
  25. // ITK includes
  26. #include <itkObjectFactory.h>
  27. #include <itkOutputWindow.h>
  28. namespace itk
  29. {
  30. // --------------------------------------------------------------------------
  31. // ctkITKOutputWindow
  32. // --------------------------------------------------------------------------
  33. class ctkITKOutputWindow : public OutputWindow
  34. {
  35. public:
  36. /** Standard class typedefs. */
  37. typedef ctkITKOutputWindow Self;
  38. typedef OutputWindow Superclass;
  39. typedef SmartPointer<Self> Pointer;
  40. typedef SmartPointer<const Self> ConstPointer;
  41. /** Standard New method. */
  42. itkNewMacro(ctkITKOutputWindow);
  43. /** Run-time type information (and related methods). */
  44. itkTypeMacro(ctkITKOutputWindow, OutputWindow);
  45. ctkITKOutputWindow():MessageHandler(0){}
  46. ~ctkITKOutputWindow(){}
  47. virtual void DisplayText(const char*);
  48. virtual void DisplayErrorText(const char*);
  49. virtual void DisplayWarningText(const char*);
  50. virtual void DisplayGenericWarningText(const char*);
  51. virtual void DisplayDebugText(const char*);
  52. ctkErrorLogAbstractMessageHandler * MessageHandler;
  53. };
  54. // --------------------------------------------------------------------------
  55. // ctkITKOutputWindow methods
  56. //----------------------------------------------------------------------------
  57. void ctkITKOutputWindow::DisplayText(const char* text)
  58. {
  59. Q_ASSERT(this->MessageHandler);
  60. this->MessageHandler->handleMessage(
  61. ctk::qtHandleToString(QThread::currentThreadId()),
  62. ctkErrorLogLevel::Info,
  63. this->MessageHandler->handlerPrettyName(), text);
  64. }
  65. //----------------------------------------------------------------------------
  66. void ctkITKOutputWindow::DisplayErrorText(const char* text)
  67. {
  68. Q_ASSERT(this->MessageHandler);
  69. this->MessageHandler->handleMessage(
  70. ctk::qtHandleToString(QThread::currentThreadId()),
  71. ctkErrorLogLevel::Error,
  72. this->MessageHandler->handlerPrettyName(), text);
  73. }
  74. //----------------------------------------------------------------------------
  75. void ctkITKOutputWindow::DisplayWarningText(const char* text)
  76. {
  77. Q_ASSERT(this->MessageHandler);
  78. this->MessageHandler->handleMessage(
  79. ctk::qtHandleToString(QThread::currentThreadId()),
  80. ctkErrorLogLevel::Warning,
  81. this->MessageHandler->handlerPrettyName(), text);
  82. }
  83. //----------------------------------------------------------------------------
  84. void ctkITKOutputWindow::DisplayGenericWarningText(const char* text)
  85. {
  86. this->DisplayWarningText(text);
  87. }
  88. //----------------------------------------------------------------------------
  89. void ctkITKOutputWindow::DisplayDebugText(const char* text)
  90. {
  91. Q_ASSERT(this->MessageHandler);
  92. this->MessageHandler->handleMessage(
  93. ctk::qtHandleToString(QThread::currentThreadId()),
  94. ctkErrorLogLevel::Debug,
  95. this->MessageHandler->handlerPrettyName(), text);
  96. }
  97. } // End of itk namespace
  98. // --------------------------------------------------------------------------
  99. // ctkITKErrorLogMessageHandlerPrivate
  100. // --------------------------------------------------------------------------
  101. class ctkITKErrorLogMessageHandlerPrivate
  102. {
  103. Q_DECLARE_PUBLIC(ctkITKErrorLogMessageHandler);
  104. protected:
  105. ctkITKErrorLogMessageHandler* const q_ptr;
  106. public:
  107. ctkITKErrorLogMessageHandlerPrivate(ctkITKErrorLogMessageHandler& object);
  108. ~ctkITKErrorLogMessageHandlerPrivate();
  109. itk::OutputWindow::Pointer SavedITKOutputWindow;
  110. itk::ctkITKOutputWindow::Pointer CTKITKOutputWindow;
  111. };
  112. // --------------------------------------------------------------------------
  113. // ctkITKErrorLogMessageHandlerPrivate methods
  114. // --------------------------------------------------------------------------
  115. ctkITKErrorLogMessageHandlerPrivate::
  116. ctkITKErrorLogMessageHandlerPrivate(ctkITKErrorLogMessageHandler& object) : q_ptr(&object)
  117. {
  118. Q_Q(ctkITKErrorLogMessageHandler);
  119. this->SavedITKOutputWindow = 0;
  120. this->CTKITKOutputWindow = itk::ctkITKOutputWindow::New();
  121. this->CTKITKOutputWindow->MessageHandler = q;
  122. }
  123. // --------------------------------------------------------------------------
  124. ctkITKErrorLogMessageHandlerPrivate::~ctkITKErrorLogMessageHandlerPrivate()
  125. {
  126. this->SavedITKOutputWindow = 0;
  127. this->CTKITKOutputWindow = 0;
  128. }
  129. // --------------------------------------------------------------------------
  130. // ctkITKErrorLogMessageHandler methods
  131. // --------------------------------------------------------------------------
  132. QString ctkITKErrorLogMessageHandler::HandlerName = QLatin1String("ITK");
  133. //----------------------------------------------------------------------------
  134. ctkITKErrorLogMessageHandler::ctkITKErrorLogMessageHandler() :
  135. Superclass(), d_ptr(new ctkITKErrorLogMessageHandlerPrivate(*this))
  136. {
  137. }
  138. //----------------------------------------------------------------------------
  139. ctkITKErrorLogMessageHandler::~ctkITKErrorLogMessageHandler()
  140. {
  141. }
  142. //----------------------------------------------------------------------------
  143. QString ctkITKErrorLogMessageHandler::handlerName()const
  144. {
  145. return ctkITKErrorLogMessageHandler::HandlerName;
  146. }
  147. //----------------------------------------------------------------------------
  148. void ctkITKErrorLogMessageHandler::setEnabledInternal(bool value)
  149. {
  150. Q_D(ctkITKErrorLogMessageHandler);
  151. if (value)
  152. {
  153. d->SavedITKOutputWindow = itk::OutputWindow::GetInstance();
  154. itk::OutputWindow::SetInstance(d->CTKITKOutputWindow);
  155. }
  156. else
  157. {
  158. Q_ASSERT(d->SavedITKOutputWindow.IsNotNull());
  159. itk::OutputWindow::SetInstance(d->SavedITKOutputWindow);
  160. d->SavedITKOutputWindow = 0;
  161. }
  162. }