Browse Source

Add ctkVTKErrorLogMessageHandler and associated test

Allows to catch messages printed using vtkWarningMacro, vtkDebugMacro, ...
Jean-Christophe Fillion-Robin 14 years ago
parent
commit
e3a18321bd

+ 2 - 0
Libs/Visualization/VTK/Core/CMakeLists.txt

@@ -20,6 +20,8 @@ SET(KIT_SRCS
   ctkVTKCompositeFunction.h
   ctkVTKCompositeFunction.h
   ctkVTKConnection.cpp
   ctkVTKConnection.cpp
   ctkVTKConnection.h
   ctkVTKConnection.h
+  ctkVTKErrorLogMessageHandler.cpp
+  ctkVTKErrorLogMessageHandler.h
   ctkVTKHistogram.cpp
   ctkVTKHistogram.cpp
   ctkVTKHistogram.h
   ctkVTKHistogram.h
   ctkVTKLookupTable.cpp
   ctkVTKLookupTable.cpp

+ 3 - 1
Libs/Visualization/VTK/Core/Testing/Cpp/CMakeLists.txt

@@ -5,6 +5,7 @@ SET(KIT ${PROJECT_NAME})
 #
 #
 SET(TEST_SOURCES
 SET(TEST_SOURCES
   ctkVTKConnectionTest1.cpp
   ctkVTKConnectionTest1.cpp
+  ctkVTKErrorLogModelTest1.cpp
   ctkVTKObjectTest1.cpp
   ctkVTKObjectTest1.cpp
   )
   )
 
 
@@ -68,8 +69,9 @@ ENDMACRO( SIMPLE_TEST  )
 # Add Tests
 # Add Tests
 #
 #
 
 
-SIMPLE_TEST( ctkVTKObjectTest1 )
 SIMPLE_TEST( ctkVTKConnectionTest1 )
 SIMPLE_TEST( ctkVTKConnectionTest1 )
+SIMPLE_TEST( ctkVTKErrorLogModelTest1 )
+SIMPLE_TEST( ctkVTKObjectTest1 )
 
 
 #
 #
 # Add Tests expecting CTKData to be set
 # Add Tests expecting CTKData to be set

+ 180 - 0
Libs/Visualization/VTK/Core/Testing/Cpp/ctkVTKErrorLogModelTest1.cpp

@@ -0,0 +1,180 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QCoreApplication>
+#include <QDebug>
+#include <QStringList>
+
+// CTK includes
+#include "ctkErrorLogModel.h"
+#include "ctkVTKErrorLogMessageHandler.h"
+#include "ctkModelTester.h"
+
+// VTK includes
+#include <vtkOutputWindow.h>
+
+// STL includes
+#include <cstdlib>
+#include <iostream>
+
+namespace
+{
+//-----------------------------------------------------------------------------
+// Utility function
+
+//-----------------------------------------------------------------------------
+QString checkRowCount(int line, int currentRowCount, int expectedRowCount)
+{
+  if (currentRowCount != expectedRowCount)
+    {
+    QString errorMsg("Line %1 - Expected rowCount: %2 - Current rowCount: %3\n");
+    return errorMsg.arg(line).arg(expectedRowCount).arg(currentRowCount);
+    }
+  return QString();
+}
+
+//-----------------------------------------------------------------------------
+QString checkTextMessages(int line, const ctkErrorLogModel& model, const QStringList& expectedMessages)
+{
+  for(int i=0; i < expectedMessages.count(); ++i)
+    {
+    QModelIndex descriptionIndex = model.index(i, ctkErrorLogModel::DescriptionColumn);
+    QString currentMessage = descriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
+    if (currentMessage.compare(expectedMessages.value(i)) != 0)
+      {
+      QString errorMsg("Line %1 - Problem with row%2 !\n"
+                       "\tExpected message [%3]\n"
+                       "\tCurrent message [%4]\n");
+      return errorMsg.arg(line).arg(i).arg(expectedMessages.value(i)).arg(currentMessage);
+      }
+    }
+  return QString();
+}
+
+//-----------------------------------------------------------------------------
+void printTextMessages(const ctkErrorLogModel& model)
+{
+  fprintf(stdout, "%s", "ErrorLogModel rows:\n");
+  QString text("\trow %1 => %2\n");
+  for (int i=0; i < model.rowCount(); ++i)
+    {
+    QString description =
+        model.index(0, ctkErrorLogModel::DescriptionColumn).data().toString();
+    fprintf(stdout, "%s", qPrintable(text.arg(i).arg(description)));
+    }
+  fflush(stdout);
+}
+
+//-----------------------------------------------------------------------------
+void printErrorMessage(const QString& errorMessage)
+{
+  fprintf(stderr, "%s", qPrintable(errorMessage));
+  fflush(stderr);
+}
+
+} // end namespace
+
+//-----------------------------------------------------------------------------
+int ctkVTKErrorLogModelTest1(int argc, char * argv [])
+{
+  QCoreApplication app(argc, argv);
+  Q_UNUSED(app);
+
+  ctkErrorLogModel model;
+  ctkModelTester modelTester;
+  modelTester.setVerbose(false);
+  QString errorMsg;
+
+  try
+    {
+    modelTester.setModel(&model);
+
+    // --------------------------------------------------------------------------
+    // Monitor VTK messages
+
+    model.registerMsgHandler(new ctkVTKErrorLogMessageHandler);
+    model.setMsgHandlerEnabled(ctkVTKErrorLogMessageHandler::HandlerName, true);
+
+    errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ 0);
+    if (!errorMsg.isEmpty())
+      {
+      model.disableAllMsgHandler();
+      printErrorMessage(errorMsg);
+      printTextMessages(model);
+      return EXIT_FAILURE;
+      }
+
+    QString vtkMessage0("This is a VTK debug message");
+    vtkOutputWindowDisplayDebugText(qPrintable(vtkMessage0));
+
+    QString vtkMessage1("This is a VTK warning message");
+    vtkOutputWindowDisplayWarningText(qPrintable(vtkMessage1));
+
+    QString vtkMessage2("This is a VTK error message");
+    vtkOutputWindowDisplayErrorText(qPrintable(vtkMessage2));
+
+    QStringList expectedVTKMessages;
+    expectedVTKMessages << vtkMessage0 << vtkMessage1 << vtkMessage2;
+
+    errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedVTKMessages.count());
+    if (!errorMsg.isEmpty())
+      {
+      model.disableAllMsgHandler();
+      printErrorMessage(errorMsg);
+      return EXIT_FAILURE;
+      }
+
+    errorMsg = checkTextMessages(__LINE__, model, expectedVTKMessages);
+    if (!errorMsg.isEmpty())
+      {
+      model.disableAllMsgHandler();
+      printErrorMessage(errorMsg);
+      return EXIT_FAILURE;
+      }
+
+    // Clear
+    model.clear();
+
+    // Disable VTK messages monitoring
+    model.setMsgHandlerEnabled(ctkVTKErrorLogMessageHandler::HandlerName, false);
+
+    vtkOutputWindowDisplayDebugText("This VTK debug message should appear in the console");
+    vtkOutputWindowDisplayWarningText("This VTK warning message should appear in the console");
+    vtkOutputWindowDisplayErrorText("This VTK error message should appear in the console");
+
+    errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ 0);
+    if (!errorMsg.isEmpty())
+      {
+      model.disableAllMsgHandler();
+      printErrorMessage(errorMsg);
+      printTextMessages(model);
+      return EXIT_FAILURE;
+      }
+    }
+  catch (const char* error)
+    {
+    model.disableAllMsgHandler();
+    std::cerr << error << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}

+ 184 - 0
Libs/Visualization/VTK/Core/ctkVTKErrorLogMessageHandler.cpp

@@ -0,0 +1,184 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkVTKErrorLogMessageHandler.h"
+
+// VTK includes
+#include <vtkObjectFactory.h>
+#include <vtkOutputWindow.h>
+
+namespace {
+
+// --------------------------------------------------------------------------
+// ctkVTKOutputWindow
+
+// --------------------------------------------------------------------------
+class ctkVTKOutputWindow : public vtkOutputWindow
+{
+public:
+  static ctkVTKOutputWindow *New();
+  vtkTypeMacro(ctkVTKOutputWindow,vtkOutputWindow);
+  void PrintSelf(ostream& os, vtkIndent indent);
+
+  ctkVTKOutputWindow():MessageHandler(0){}
+  ~ctkVTKOutputWindow(){}
+
+  virtual void DisplayText(const char*);
+  virtual void DisplayErrorText(const char*);
+  virtual void DisplayWarningText(const char*);
+  virtual void DisplayGenericWarningText(const char*);
+
+  virtual void DisplayDebugText(const char*);
+
+  ctkErrorLogAbstractMessageHandler * MessageHandler;
+};
+
+// --------------------------------------------------------------------------
+// ctkVTKOutputWindow methods
+
+// --------------------------------------------------------------------------
+vtkStandardNewMacro(ctkVTKOutputWindow);
+
+//----------------------------------------------------------------------------
+void ctkVTKOutputWindow::PrintSelf(ostream& os, vtkIndent indent)
+{
+  this->Superclass::PrintSelf(os,indent);
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKOutputWindow::DisplayText(const char* text)
+{
+  this->MessageHandler->errorLogModel()->addEntry(
+        ctkErrorLogModel::Info, this->MessageHandler->handlerPrettyName(), text);
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKOutputWindow::DisplayErrorText(const char* text)
+{
+  this->MessageHandler->errorLogModel()->addEntry(
+        ctkErrorLogModel::Error, this->MessageHandler->handlerPrettyName(), text);
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKOutputWindow::DisplayWarningText(const char* text)
+{
+  this->MessageHandler->errorLogModel()->addEntry(
+        ctkErrorLogModel::Warning, this->MessageHandler->handlerPrettyName(), text);
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKOutputWindow::DisplayGenericWarningText(const char* text)
+{
+  this->DisplayWarningText(text);
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKOutputWindow::DisplayDebugText(const char* text)
+{
+  this->MessageHandler->errorLogModel()->addEntry(
+        ctkErrorLogModel::Debug, this->MessageHandler->handlerPrettyName(), text);
+}
+
+} // End of anonymous namespace
+
+// --------------------------------------------------------------------------
+// ctkVTKErrorLogMessageHandlerPrivate
+
+// --------------------------------------------------------------------------
+class ctkVTKErrorLogMessageHandlerPrivate
+{
+  Q_DECLARE_PUBLIC(ctkVTKErrorLogMessageHandler);
+protected:
+  ctkVTKErrorLogMessageHandler* const q_ptr;
+public:
+  ctkVTKErrorLogMessageHandlerPrivate(ctkVTKErrorLogMessageHandler& object);
+  ~ctkVTKErrorLogMessageHandlerPrivate();
+
+  vtkOutputWindow * SavedVTKOutputWindow;
+  ctkVTKOutputWindow * CTKVTKOutputWindow;
+
+};
+
+// --------------------------------------------------------------------------
+// ctkVTKErrorLogMessageHandlerPrivate methods
+
+// --------------------------------------------------------------------------
+ctkVTKErrorLogMessageHandlerPrivate::
+ctkVTKErrorLogMessageHandlerPrivate(ctkVTKErrorLogMessageHandler& object) : q_ptr(&object)
+{
+  Q_Q(ctkVTKErrorLogMessageHandler);
+  this->SavedVTKOutputWindow = 0;
+  this->CTKVTKOutputWindow = ctkVTKOutputWindow::New();
+  this->CTKVTKOutputWindow->MessageHandler = q;
+}
+
+// --------------------------------------------------------------------------
+ctkVTKErrorLogMessageHandlerPrivate::~ctkVTKErrorLogMessageHandlerPrivate()
+{
+  if (this->SavedVTKOutputWindow)
+    {
+    this->SavedVTKOutputWindow->Delete();
+    }
+  this->CTKVTKOutputWindow->Delete();
+}
+
+// --------------------------------------------------------------------------
+// ctkVTKErrorLogMessageHandler methods
+
+// --------------------------------------------------------------------------
+QString ctkVTKErrorLogMessageHandler::HandlerName = QLatin1String("VTK");
+
+//----------------------------------------------------------------------------
+ctkVTKErrorLogMessageHandler::ctkVTKErrorLogMessageHandler() :
+  Superclass(), d_ptr(new ctkVTKErrorLogMessageHandlerPrivate(*this))
+{
+}
+
+//----------------------------------------------------------------------------
+ctkVTKErrorLogMessageHandler::~ctkVTKErrorLogMessageHandler()
+{
+}
+
+//----------------------------------------------------------------------------
+QString ctkVTKErrorLogMessageHandler::handlerName()const
+{
+  return ctkVTKErrorLogMessageHandler::HandlerName;
+}
+
+//----------------------------------------------------------------------------
+void ctkVTKErrorLogMessageHandler::setEnabledInternal(bool value)
+{
+  Q_D(ctkVTKErrorLogMessageHandler);
+  if (value)
+    {
+    d->SavedVTKOutputWindow = vtkOutputWindow::GetInstance();
+    d->SavedVTKOutputWindow->Register(0);
+    vtkOutputWindow::SetInstance(d->CTKVTKOutputWindow);
+    }
+  else
+    {
+    Q_ASSERT(d->SavedVTKOutputWindow);
+    vtkOutputWindow::SetInstance(d->SavedVTKOutputWindow);
+    d->SavedVTKOutputWindow->Delete();
+    d->SavedVTKOutputWindow = 0;
+    }
+}
+

+ 58 - 0
Libs/Visualization/VTK/Core/ctkVTKErrorLogMessageHandler.h

@@ -0,0 +1,58 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkVTKErrorLogMessageHandler_h
+#define __ctkVTKErrorLogMessageHandler_h
+
+// Qt includes
+#include <QtGlobal>
+#include <QScopedPointer>
+
+// CTK includes
+#include <ctkErrorLogModel.h>
+#include "ctkVisualizationVTKCoreExport.h"
+
+class ctkVTKErrorLogMessageHandlerPrivate;
+
+class CTK_VISUALIZATION_VTK_CORE_EXPORT ctkVTKErrorLogMessageHandler :
+    public ctkErrorLogAbstractMessageHandler
+{
+public:
+  typedef ctkErrorLogAbstractMessageHandler Superclass;
+
+  ctkVTKErrorLogMessageHandler();
+  virtual ~ctkVTKErrorLogMessageHandler();
+
+  static QString HandlerName;
+
+  virtual QString handlerName()const;
+
+  virtual void setEnabledInternal(bool value);
+
+protected:
+  QScopedPointer<ctkVTKErrorLogMessageHandlerPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkVTKErrorLogMessageHandler);
+  Q_DISABLE_COPY(ctkVTKErrorLogMessageHandler);
+
+};
+
+#endif