ObserveError
Repository source: ObserveError
Description¶
When vtk encounters an error or warning, by default the message is written to standard output. This example shows how to catch the error or warning. The example prints to standard output, but an application may wish to present the error or warning in a different manner, e.g. write to a log file or present the message in a window.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ObserveError.cxx
#include <string>
#include <vtkCommand.h>
#include <vtkImageData.h>
#include <vtkNew.h>
class ErrorObserver : public vtkCommand
{
public:
ErrorObserver()
: Error(false), Warning(false), ErrorMessage(""), WarningMessage("")
{
}
static ErrorObserver* New()
{
return new ErrorObserver;
}
bool GetError() const
{
return this->Error;
}
bool GetWarning() const
{
return this->Warning;
}
void Clear()
{
this->Error = false;
this->Warning = false;
this->ErrorMessage = "";
this->WarningMessage = "";
}
virtual void Execute(vtkObject* vtkNotUsed(caller), unsigned long event,
void* calldata)
{
switch (event)
{
case vtkCommand::ErrorEvent:
ErrorMessage = static_cast<char*>(calldata);
this->Error = true;
break;
case vtkCommand::WarningEvent:
WarningMessage = static_cast<char*>(calldata);
this->Warning = true;
break;
}
}
std::string GetErrorMessage()
{
return ErrorMessage;
}
std::string GetWarningMessage()
{
return WarningMessage;
}
private:
bool Error;
bool Warning;
std::string ErrorMessage;
std::string WarningMessage;
};
int main(int, char*[])
{
vtkNew<ErrorObserver> errorObserver;
vtkNew<vtkImageData> image;
image->AddObserver(vtkCommand::ErrorEvent, errorObserver);
image->AddObserver(vtkCommand::WarningEvent, errorObserver);
image->GetCell(1);
if (errorObserver->GetError())
{
std::cout << "Caught error! " << errorObserver->GetErrorMessage();
}
image->ShallowCopy(NULL);
if (errorObserver->GetWarning())
{
std::cout << "Caught warning! " << errorObserver->GetWarningMessage();
}
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ObserveError)
find_package(VTK COMPONENTS
CommonCore
CommonDataModel
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ObserveError: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(ObserveError MACOSX_BUNDLE ObserveError.cxx )
target_link_libraries(ObserveError PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ObserveError
MODULES ${VTK_LIBRARIES}
)
Download and Build ObserveError¶
Click here to download ObserveError and its CMakeLists.txt file. Once the tarball ObserveError.tar has been downloaded and extracted,
cd ObserveError/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./ObserveError
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.