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();
77  m_killMeNow(getMessage());
78  }
79 
84  static void setFailureCallback(DeathCallback callback);
85 
89  static DeathCallback getFailureCallback();
90 
94  {
95  setFailureCallback(throwException);
96  }
97 
101  {
102  setFailureCallback(killApplication);
103  }
104 
105 private:
108  static void throwException(const std::string& errorMessage);
109 
112  static void killApplication(const std::string& errorMessage);
113 
114 
117  static DeathCallback m_killMeNow;
118 };
119 
120 
121 }; // namespace Framework
122 }; // namespace SurgSim
123 
124 #endif // SURGSIM_FRAMEWORK_ASSERTMESSAGE_H
Definition: DriveElementFromInputBehavior.cpp:27
AssertMessage(const std::shared_ptr< Logger > &logger)
Constructor.
Definition: AssertMessage.h:65
STL namespace.
static DeathCallback m_killMeNow
The callback function that is triggered after an assertion has failed.
Definition: AssertMessage.h:117
static void setFailureBehaviorToDeath()
After an assertion has failed, enter the debugger or kill the application in a system-dependent way...
Definition: AssertMessage.h:100
AssertionFailure(const std::string &message)
Constructor.
Definition: AssertMessage.h:37
An exception class thrown by SURGSIM_ASSERT() failures and SURGSIM_FAILURE().
Definition: AssertMessage.h:32
string(TOUPPER ${DEVICE}DEVICE_UPPER_CASE) option(BUILD_DEVICE_ $
Definition: CMakeLists.txt:35
~AssertMessage() noexcept(false)
Destructor, which may throw an exception if the failure behavior does.
Definition: AssertMessage.h:73
AssertMessage(Logger *logger)
Constructor.
Definition: AssertMessage.h:53
An object that can be used to control logging parameters, such as verbosity and log output destinatio...
Definition: Logger.h:51
Used by assertion, after using this level the program will not be functional at all.
Definition: Logger.h:47
An internal message class used for assertion failures.
Definition: AssertMessage.h:45
AssertMessage(const std::unique_ptr< Logger > &logger)
Constructor.
Definition: AssertMessage.h:59
static void setFailureBehaviorToThrow()
After an assertion has failed, throw a C++ exception.
Definition: AssertMessage.h:93
LogMessageBase is a base class to be used to customize messages for logging textual information can b...
Definition: LogMessageBase.h:40