opensurgsim
AssertMessage.h
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 
21 #include "SurgSim/Framework/LogMessageBase.h"
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  // As per https://docs.microsoft.com/en-us/cpp/cpp/exception-specifications-throw-cpp?view=vs-2019
71  // noexcept(false) has implementation from Visual Studio 2017 15.5
72 #if defined(_MSC_VER) && _MSC_VER < 1912
73  ~AssertMessage() throw(...)
74 #else
75  ~AssertMessage() noexcept(false)
76 #endif
77  {
78  flush();
79  m_killMeNow(getMessage());
80  }
81 
86  static void setFailureCallback(DeathCallback callback);
87 
91  static DeathCallback getFailureCallback();
92 
96  {
97  setFailureCallback(throwException);
98  }
99 
103  {
104  setFailureCallback(killApplication);
105  }
106 
107 private:
110  static void throwException(const std::string& errorMessage);
111 
114  static void killApplication(const std::string& errorMessage);
115 
116 
119  static DeathCallback m_killMeNow;
120 };
121 
122 
123 }; // namespace Framework
124 }; // namespace SurgSim
125 
126 #endif // SURGSIM_FRAMEWORK_ASSERTMESSAGE_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
AssertMessage(const std::shared_ptr< Logger > &logger)
Constructor.
Definition: AssertMessage.h:65
Definition: MockObjects.h:47
static void setFailureBehaviorToDeath()
After an assertion has failed, enter the debugger or kill the application in a system-dependent way...
Definition: AssertMessage.h:102
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
~AssertMessage() noexcept(false)
Destructor, which may throw an exception if the failure behavior does.
Definition: AssertMessage.h:75
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:95
LogMessageBase is a base class to be used to customize messages for logging textual information can b...
Definition: LogMessageBase.h:40