AssertMessage.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions 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.apache.org/licenses/LICENSE-2.0
//
// 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 SURGSIM_FRAMEWORK_ASSERTMESSAGE_H
#define SURGSIM_FRAMEWORK_ASSERTMESSAGE_H
#include <memory>
#include "SurgSim/Framework/LogMessageBase.h"
namespace SurgSim
{
namespace Framework
{
class AssertionFailure : public std::runtime_error
{
public:
explicit AssertionFailure(const std::string& message) : std::runtime_error(message)
{
}
};
class AssertMessage : public LogMessageBase
{
public:
typedef void (*DeathCallback)(const std::string& message);
explicit AssertMessage(Logger* logger) : LogMessageBase(logger, LOG_LEVEL_CRITICAL)
{
}
explicit AssertMessage(const std::unique_ptr<Logger>& logger) : LogMessageBase(logger.get(), LOG_LEVEL_CRITICAL)
{
}
explicit AssertMessage(const std::shared_ptr<Logger>& logger) : LogMessageBase(logger.get(), LOG_LEVEL_CRITICAL)
{
}
#ifdef _MSC_VER
~AssertMessage() throw(...) // Visual Studio does not support noexcept. The throw(...) is optional.
#else
~AssertMessage() noexcept(false)
#endif
{
flush();
m_killMeNow(getMessage());
}
static void setFailureCallback(DeathCallback callback);
static DeathCallback getFailureCallback();
static void setFailureBehaviorToThrow()
{
setFailureCallback(throwException);
}
static void setFailureBehaviorToDeath()
{
setFailureCallback(killApplication);
}
private:
static void throwException(const std::string& errorMessage);
static void killApplication(const std::string& errorMessage);
static DeathCallback m_killMeNow;
};
}; // namespace Framework
}; // namespace SurgSim
#endif // SURGSIM_FRAMEWORK_ASSERTMESSAGE_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::AssertMessage::AssertMessage
AssertMessage(const std::shared_ptr< Logger > &logger)
Constructor.
Definition: AssertMessage.h:65
LogMessageBase.h
std
STL namespace.
SurgSim::Framework::AssertMessage::m_killMeNow
static DeathCallback m_killMeNow
The callback function that is triggered after an assertion has failed.
Definition: AssertMessage.h:117
SurgSim::Framework::AssertMessage::setFailureBehaviorToDeath
static void setFailureBehaviorToDeath()
After an assertion has failed, enter the debugger or kill the application in a system-dependent way...
Definition: AssertMessage.h:100
SurgSim::Framework::AssertionFailure::AssertionFailure
AssertionFailure(const std::string &message)
Constructor.
Definition: AssertMessage.h:37
SurgSim::Framework::AssertionFailure
An exception class thrown by SURGSIM_ASSERT() failures and SURGSIM_FAILURE().
Definition: AssertMessage.h:32
SurgSim::Framework::AssertMessage::~AssertMessage
~AssertMessage() noexcept(false)
Destructor, which may throw an exception if the failure behavior does.
Definition: AssertMessage.h:73
SurgSim::Framework::AssertMessage::AssertMessage
AssertMessage(Logger *logger)
Constructor.
Definition: AssertMessage.h:53
SurgSim::Framework::Logger
An object that can be used to control logging parameters, such as verbosity and log output destinatio...
Definition: Logger.h:51
SurgSim::Framework::LOG_LEVEL_CRITICAL
Used by assertion, after using this level the program will not be functional at all.
Definition: Logger.h:47
SurgSim::Framework::AssertMessage
An internal message class used for assertion failures.
Definition: AssertMessage.h:45
SurgSim::Framework::AssertMessage::AssertMessage
AssertMessage(const std::unique_ptr< Logger > &logger)
Constructor.
Definition: AssertMessage.h:59
SurgSim::Framework::AssertMessage::setFailureBehaviorToThrow
static void setFailureBehaviorToThrow()
After an assertion has failed, throw a C++ exception.
Definition: AssertMessage.h:93
SurgSim::Framework::LogMessageBase
LogMessageBase is a base class to be used to customize messages for logging textual information can b...
Definition: LogMessageBase.h:40