ctkVTKErrorLogMessageHandler.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "ctkErrorLogContext.h"
  18. #include "ctkVTKErrorLogMessageHandler.h"
  19. #include "ctkUtils.h"
  20. // VTK includes
  21. #include <vtkObjectFactory.h>
  22. #include <vtkOutputWindow.h>
  23. namespace {
  24. // --------------------------------------------------------------------------
  25. // ctkVTKOutputWindow
  26. // --------------------------------------------------------------------------
  27. class ctkVTKOutputWindow : public vtkOutputWindow
  28. {
  29. public:
  30. static ctkVTKOutputWindow *New();
  31. vtkTypeMacro(ctkVTKOutputWindow,vtkOutputWindow);
  32. void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
  33. ctkVTKOutputWindow()
  34. : MessageHandler(0)
  35. {}
  36. ~ctkVTKOutputWindow(){}
  37. virtual void DisplayText(const char*) VTK_OVERRIDE;
  38. virtual void DisplayErrorText(const char*) VTK_OVERRIDE;
  39. virtual void DisplayWarningText(const char*) VTK_OVERRIDE;
  40. virtual void DisplayGenericWarningText(const char*) VTK_OVERRIDE;
  41. virtual void DisplayDebugText(const char*) VTK_OVERRIDE;
  42. QString parseText(const QString &text, ctkErrorLogContext &context);
  43. ctkErrorLogAbstractMessageHandler * MessageHandler;
  44. };
  45. // --------------------------------------------------------------------------
  46. // ctkVTKOutputWindow methods
  47. // --------------------------------------------------------------------------
  48. vtkStandardNewMacro(ctkVTKOutputWindow);
  49. //----------------------------------------------------------------------------
  50. void ctkVTKOutputWindow::PrintSelf(ostream& os, vtkIndent indent)
  51. {
  52. this->Superclass::PrintSelf(os,indent);
  53. }
  54. //----------------------------------------------------------------------------
  55. void ctkVTKOutputWindow::DisplayText(const char* text)
  56. {
  57. Q_ASSERT(this->MessageHandler);
  58. ctkErrorLogContext context;
  59. QString textOnly = this->parseText(text, context);
  60. this->MessageHandler->handleMessage(
  61. ctk::qtHandleToString(QThread::currentThreadId()),
  62. ctkErrorLogLevel::Info,
  63. this->MessageHandler->handlerPrettyName(), context, textOnly);
  64. }
  65. //----------------------------------------------------------------------------
  66. void ctkVTKOutputWindow::DisplayErrorText(const char* text)
  67. {
  68. Q_ASSERT(this->MessageHandler);
  69. ctkErrorLogContext context;
  70. QString textOnly = this->parseText(text, context);
  71. this->MessageHandler->handleMessage(
  72. ctk::qtHandleToString(QThread::currentThreadId()),
  73. ctkErrorLogLevel::Error,
  74. this->MessageHandler->handlerPrettyName(), context, textOnly);
  75. }
  76. //----------------------------------------------------------------------------
  77. void ctkVTKOutputWindow::DisplayWarningText(const char* text)
  78. {
  79. Q_ASSERT(this->MessageHandler);
  80. ctkErrorLogContext context;
  81. this->parseText(text, context);
  82. this->MessageHandler->handleMessage(
  83. ctk::qtHandleToString(QThread::currentThreadId()),
  84. ctkErrorLogLevel::Warning,
  85. this->MessageHandler->handlerPrettyName(), context, text);
  86. }
  87. //----------------------------------------------------------------------------
  88. void ctkVTKOutputWindow::DisplayGenericWarningText(const char* text)
  89. {
  90. this->DisplayWarningText(text);
  91. }
  92. //----------------------------------------------------------------------------
  93. void ctkVTKOutputWindow::DisplayDebugText(const char* text)
  94. {
  95. Q_ASSERT(this->MessageHandler);
  96. ctkErrorLogContext context;
  97. this->parseText(text, context);
  98. this->MessageHandler->handleMessage(
  99. ctk::qtHandleToString(QThread::currentThreadId()),
  100. ctkErrorLogLevel::Debug,
  101. this->MessageHandler->handlerPrettyName(), context, text);
  102. }
  103. //----------------------------------------------------------------------------
  104. QString ctkVTKOutputWindow::parseText(const QString& text, ctkErrorLogContext& context)
  105. {
  106. context.Message = text;
  107. QRegExp contextRegExp("[a-zA-Z\\s]+: In (.+), line ([\\d]+)\\n(.+\\((?:0x)?[a-fA-F0-9]+\\))\\:\\s(.*)");
  108. if (contextRegExp.exactMatch(text))
  109. {
  110. context.File = contextRegExp.cap(1);
  111. context.Category = contextRegExp.cap(3);
  112. context.Line = contextRegExp.cap(2).toInt();
  113. context.Message = contextRegExp.cap(4);
  114. }
  115. return context.Message;
  116. }
  117. } // End of anonymous namespace
  118. // --------------------------------------------------------------------------
  119. // ctkVTKErrorLogMessageHandlerPrivate
  120. // --------------------------------------------------------------------------
  121. class ctkVTKErrorLogMessageHandlerPrivate
  122. {
  123. Q_DECLARE_PUBLIC(ctkVTKErrorLogMessageHandler);
  124. protected:
  125. ctkVTKErrorLogMessageHandler* const q_ptr;
  126. public:
  127. ctkVTKErrorLogMessageHandlerPrivate(ctkVTKErrorLogMessageHandler& object);
  128. ~ctkVTKErrorLogMessageHandlerPrivate();
  129. vtkOutputWindow * SavedVTKOutputWindow;
  130. ctkVTKOutputWindow * CTKVTKOutputWindow;
  131. };
  132. // --------------------------------------------------------------------------
  133. // ctkVTKErrorLogMessageHandlerPrivate methods
  134. // --------------------------------------------------------------------------
  135. ctkVTKErrorLogMessageHandlerPrivate::
  136. ctkVTKErrorLogMessageHandlerPrivate(ctkVTKErrorLogMessageHandler& object) : q_ptr(&object)
  137. {
  138. Q_Q(ctkVTKErrorLogMessageHandler);
  139. this->SavedVTKOutputWindow = 0;
  140. this->CTKVTKOutputWindow = ctkVTKOutputWindow::New();
  141. this->CTKVTKOutputWindow->MessageHandler = q;
  142. }
  143. // --------------------------------------------------------------------------
  144. ctkVTKErrorLogMessageHandlerPrivate::~ctkVTKErrorLogMessageHandlerPrivate()
  145. {
  146. if (this->SavedVTKOutputWindow)
  147. {
  148. this->SavedVTKOutputWindow->Delete();
  149. }
  150. this->CTKVTKOutputWindow->Delete();
  151. }
  152. // --------------------------------------------------------------------------
  153. // ctkVTKErrorLogMessageHandler methods
  154. // --------------------------------------------------------------------------
  155. QString ctkVTKErrorLogMessageHandler::HandlerName = QLatin1String("VTK");
  156. //----------------------------------------------------------------------------
  157. ctkVTKErrorLogMessageHandler::ctkVTKErrorLogMessageHandler() :
  158. Superclass(), d_ptr(new ctkVTKErrorLogMessageHandlerPrivate(*this))
  159. {
  160. }
  161. //----------------------------------------------------------------------------
  162. ctkVTKErrorLogMessageHandler::~ctkVTKErrorLogMessageHandler()
  163. {
  164. }
  165. //----------------------------------------------------------------------------
  166. QString ctkVTKErrorLogMessageHandler::handlerName()const
  167. {
  168. return ctkVTKErrorLogMessageHandler::HandlerName;
  169. }
  170. //----------------------------------------------------------------------------
  171. void ctkVTKErrorLogMessageHandler::setEnabledInternal(bool value)
  172. {
  173. Q_D(ctkVTKErrorLogMessageHandler);
  174. if (value)
  175. {
  176. d->SavedVTKOutputWindow = vtkOutputWindow::GetInstance();
  177. d->SavedVTKOutputWindow->Register(0);
  178. vtkOutputWindow::SetInstance(d->CTKVTKOutputWindow);
  179. }
  180. else
  181. {
  182. Q_ASSERT(d->SavedVTKOutputWindow);
  183. vtkOutputWindow::SetInstance(d->SavedVTKOutputWindow);
  184. d->SavedVTKOutputWindow->Delete();
  185. d->SavedVTKOutputWindow = 0;
  186. }
  187. }