ctkITKErrorLogMessageHandler.cpp 7.5 KB

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