ctkVTKErrorLogMessageHandler.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "ctkVTKErrorLogMessageHandler.h"
  18. #include "ctkUtils.h"
  19. // VTK includes
  20. #include <vtkObjectFactory.h>
  21. #include <vtkOutputWindow.h>
  22. namespace {
  23. // --------------------------------------------------------------------------
  24. // ctkVTKOutputWindow
  25. // --------------------------------------------------------------------------
  26. class ctkVTKOutputWindow : public vtkOutputWindow
  27. {
  28. public:
  29. static ctkVTKOutputWindow *New();
  30. vtkTypeMacro(ctkVTKOutputWindow,vtkOutputWindow);
  31. void PrintSelf(ostream& os, vtkIndent indent);
  32. ctkVTKOutputWindow():MessageHandler(0){}
  33. ~ctkVTKOutputWindow(){}
  34. virtual void DisplayText(const char*);
  35. virtual void DisplayErrorText(const char*);
  36. virtual void DisplayWarningText(const char*);
  37. virtual void DisplayGenericWarningText(const char*);
  38. virtual void DisplayDebugText(const char*);
  39. ctkErrorLogAbstractMessageHandler * MessageHandler;
  40. };
  41. // --------------------------------------------------------------------------
  42. // ctkVTKOutputWindow methods
  43. // --------------------------------------------------------------------------
  44. vtkStandardNewMacro(ctkVTKOutputWindow);
  45. //----------------------------------------------------------------------------
  46. void ctkVTKOutputWindow::PrintSelf(ostream& os, vtkIndent indent)
  47. {
  48. this->Superclass::PrintSelf(os,indent);
  49. }
  50. //----------------------------------------------------------------------------
  51. void ctkVTKOutputWindow::DisplayText(const char* text)
  52. {
  53. Q_ASSERT(this->MessageHandler);
  54. this->MessageHandler->handleMessage(
  55. ctk::qtHandleToString(QThread::currentThreadId()),
  56. ctkErrorLogLevel::Info,
  57. this->MessageHandler->handlerPrettyName(), text);
  58. }
  59. //----------------------------------------------------------------------------
  60. void ctkVTKOutputWindow::DisplayErrorText(const char* text)
  61. {
  62. Q_ASSERT(this->MessageHandler);
  63. this->MessageHandler->handleMessage(
  64. ctk::qtHandleToString(QThread::currentThreadId()),
  65. ctkErrorLogLevel::Error,
  66. this->MessageHandler->handlerPrettyName(), text);
  67. }
  68. //----------------------------------------------------------------------------
  69. void ctkVTKOutputWindow::DisplayWarningText(const char* text)
  70. {
  71. Q_ASSERT(this->MessageHandler);
  72. this->MessageHandler->handleMessage(
  73. ctk::qtHandleToString(QThread::currentThreadId()),
  74. ctkErrorLogLevel::Warning,
  75. this->MessageHandler->handlerPrettyName(), text);
  76. }
  77. //----------------------------------------------------------------------------
  78. void ctkVTKOutputWindow::DisplayGenericWarningText(const char* text)
  79. {
  80. this->DisplayWarningText(text);
  81. }
  82. //----------------------------------------------------------------------------
  83. void ctkVTKOutputWindow::DisplayDebugText(const char* text)
  84. {
  85. Q_ASSERT(this->MessageHandler);
  86. this->MessageHandler->handleMessage(
  87. ctk::qtHandleToString(QThread::currentThreadId()),
  88. ctkErrorLogLevel::Debug,
  89. this->MessageHandler->handlerPrettyName(), text);
  90. }
  91. } // End of anonymous namespace
  92. // --------------------------------------------------------------------------
  93. // ctkVTKErrorLogMessageHandlerPrivate
  94. // --------------------------------------------------------------------------
  95. class ctkVTKErrorLogMessageHandlerPrivate
  96. {
  97. Q_DECLARE_PUBLIC(ctkVTKErrorLogMessageHandler);
  98. protected:
  99. ctkVTKErrorLogMessageHandler* const q_ptr;
  100. public:
  101. ctkVTKErrorLogMessageHandlerPrivate(ctkVTKErrorLogMessageHandler& object);
  102. ~ctkVTKErrorLogMessageHandlerPrivate();
  103. vtkOutputWindow * SavedVTKOutputWindow;
  104. ctkVTKOutputWindow * CTKVTKOutputWindow;
  105. };
  106. // --------------------------------------------------------------------------
  107. // ctkVTKErrorLogMessageHandlerPrivate methods
  108. // --------------------------------------------------------------------------
  109. ctkVTKErrorLogMessageHandlerPrivate::
  110. ctkVTKErrorLogMessageHandlerPrivate(ctkVTKErrorLogMessageHandler& object) : q_ptr(&object)
  111. {
  112. Q_Q(ctkVTKErrorLogMessageHandler);
  113. this->SavedVTKOutputWindow = 0;
  114. this->CTKVTKOutputWindow = ctkVTKOutputWindow::New();
  115. this->CTKVTKOutputWindow->MessageHandler = q;
  116. }
  117. // --------------------------------------------------------------------------
  118. ctkVTKErrorLogMessageHandlerPrivate::~ctkVTKErrorLogMessageHandlerPrivate()
  119. {
  120. if (this->SavedVTKOutputWindow)
  121. {
  122. this->SavedVTKOutputWindow->Delete();
  123. }
  124. this->CTKVTKOutputWindow->Delete();
  125. }
  126. // --------------------------------------------------------------------------
  127. // ctkVTKErrorLogMessageHandler methods
  128. // --------------------------------------------------------------------------
  129. QString ctkVTKErrorLogMessageHandler::HandlerName = QLatin1String("VTK");
  130. //----------------------------------------------------------------------------
  131. ctkVTKErrorLogMessageHandler::ctkVTKErrorLogMessageHandler() :
  132. Superclass(), d_ptr(new ctkVTKErrorLogMessageHandlerPrivate(*this))
  133. {
  134. }
  135. //----------------------------------------------------------------------------
  136. ctkVTKErrorLogMessageHandler::~ctkVTKErrorLogMessageHandler()
  137. {
  138. }
  139. //----------------------------------------------------------------------------
  140. QString ctkVTKErrorLogMessageHandler::handlerName()const
  141. {
  142. return ctkVTKErrorLogMessageHandler::HandlerName;
  143. }
  144. //----------------------------------------------------------------------------
  145. void ctkVTKErrorLogMessageHandler::setEnabledInternal(bool value)
  146. {
  147. Q_D(ctkVTKErrorLogMessageHandler);
  148. if (value)
  149. {
  150. d->SavedVTKOutputWindow = vtkOutputWindow::GetInstance();
  151. d->SavedVTKOutputWindow->Register(0);
  152. vtkOutputWindow::SetInstance(d->CTKVTKOutputWindow);
  153. }
  154. else
  155. {
  156. Q_ASSERT(d->SavedVTKOutputWindow);
  157. vtkOutputWindow::SetInstance(d->SavedVTKOutputWindow);
  158. d->SavedVTKOutputWindow->Delete();
  159. d->SavedVTKOutputWindow = 0;
  160. }
  161. }