AssertMessage.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
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 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_ASSERTMESSAGE_H
17 #define SURGSIM_FRAMEWORK_ASSERTMESSAGE_H
18 
19 #include <memory>
20 
22 
23 
24 namespace SurgSim
25 {
26 namespace Framework
27 {
28 
29 
32 class AssertionFailure : public std::runtime_error
33 {
34 public:
37  explicit AssertionFailure(const std::string& message) : std::runtime_error(message)
38  {
39  }
40 };
41 
42 
46 {
47 public:
49  typedef void (*DeathCallback)(const std::string& message);
50 
54  {
55  }
56 
59  explicit AssertMessage(const std::unique_ptr<Logger>& logger) : LogMessageBase(logger.get(), LOG_LEVEL_CRITICAL)
60  {
61  }
62 
65  explicit AssertMessage(const std::shared_ptr<Logger>& logger) : LogMessageBase(logger.get(), LOG_LEVEL_CRITICAL)
66  {
67  }
68 
70 #ifdef _MSC_VER
71  ~AssertMessage() throw(...) // Visual Studio does not support noexcept. The throw(...) is optional.
72 #else
73  ~AssertMessage() noexcept(false)
74 #endif
75  {
76  flush();
78  }
79 
84  static void setFailureCallback(DeathCallback callback);
85 
90 
94  {
96  }
97 
101  {
103  }
104 
105 private:
108  static void throwException(const std::string& errorMessage);
109 
112  static void killApplication(const std::string& errorMessage);
113 
114 
118 };
119 
120 
121 }; // namespace Framework
122 }; // namespace SurgSim
123 
124 #endif // SURGSIM_FRAMEWORK_ASSERTMESSAGE_H
SurgSim::Framework::AssertMessage::~AssertMessage
~AssertMessage() noexcept(false)
Destructor, which may throw an exception if the failure behavior does.
Definition: AssertMessage.h:73
SurgSim::Framework::AssertionFailure
An exception class thrown by SURGSIM_ASSERT() failures and SURGSIM_FAILURE().
Definition: AssertMessage.h:32
SurgSim::Framework::AssertMessage
An internal message class used for assertion failures.
Definition: AssertMessage.h:45
SurgSim::Framework::AssertMessage::setFailureBehaviorToThrow
static void setFailureBehaviorToThrow()
After an assertion has failed, throw a C++ exception.
Definition: AssertMessage.h:93
SurgSim::Framework::AssertMessage::AssertMessage
AssertMessage(const std::shared_ptr< Logger > &logger)
Constructor.
Definition: AssertMessage.h:65
SurgSim::Framework::LogMessageBase::getMessage
std::string getMessage()
Definition: LogMessageBase.h:83
LogMessageBase.h
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
Definition: CompoundShapeToGraphics.cpp:29
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::AssertionFailure::AssertionFailure
AssertionFailure(const std::string &message)
Constructor.
Definition: AssertMessage.h:37
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::LogMessageBase
LogMessageBase is a base class to be used to customize messages for logging textual information can b...
Definition: LogMessageBase.h:40
SurgSim::Framework::AssertMessage::killApplication
static void killApplication(const std::string &errorMessage)
Enter the debugger or kill the application in a system-dependent way.
Definition: AssertMessage.cpp:46
SurgSim::Framework::AssertMessage::AssertMessage
AssertMessage(Logger *logger)
Constructor.
Definition: AssertMessage.h:53
SurgSim::Framework::AssertMessage::DeathCallback
void(* DeathCallback)(const std::string &message)
The type used for the callback function that is triggered after an assertion has failed.
Definition: AssertMessage.h:49
SurgSim::Framework::AssertMessage::getFailureCallback
static DeathCallback getFailureCallback()
Get the callback that will currently be called after an assertion has failed.
Definition: AssertMessage.cpp:36
SurgSim::Framework::AssertMessage::AssertMessage
AssertMessage(const std::unique_ptr< Logger > &logger)
Constructor.
Definition: AssertMessage.h:59
SurgSim::Framework::LogMessageBase::flush
void flush()
write the current message to the logger
Definition: LogMessageBase.h:89
SurgSim::Framework::AssertMessage::throwException
static void throwException(const std::string &errorMessage)
Kill the application by throwing an exception.
Definition: AssertMessage.cpp:41
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::AssertMessage::setFailureCallback
static void setFailureCallback(DeathCallback callback)
After an assertion has failed, call some arbitrary function.
Definition: AssertMessage.cpp:31