2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief Header file for base exception
22 #ifndef ValidationCore_EXCEPTION_H
23 #define ValidationCore_EXCEPTION_H
32 namespace ValidationCore {
33 void LogUnhandledException(const std::string &str);
34 void LogUnhandledException(const std::string &str,
37 const char *function);
40 namespace ValidationCore {
43 static unsigned int m_exceptionCount;
44 static Exception* m_lastException;
45 static void (*m_terminateHandler)();
47 static void AddRef(Exception* exception)
49 if (!m_exceptionCount) {
50 m_terminateHandler = std::set_terminate(&TerminateHandler);
54 m_lastException = exception;
57 static void UnRef(Exception* e)
59 if (m_lastException == e) {
60 m_lastException = NULL;
65 if (!m_exceptionCount) {
66 std::set_terminate(m_terminateHandler);
67 m_terminateHandler = NULL;
71 static void TerminateHandler()
73 if (m_lastException != NULL) {
74 DisplayKnownException(*m_lastException);
77 DisplayUnknownException();
84 std::string m_function;
88 std::string m_message;
89 std::string m_className;
92 static std::string KnownExceptionToString(const Exception &e)
94 std::ostringstream message;
96 "\033[1;5;31m\n=== Unhandled DPL exception occurred ===\033[m\n\n";
97 message << "\033[1;33mException trace:\033[m\n\n";
98 message << e.DumpToString();
99 message << "\033[1;31m\n=== Will now abort ===\033[m\n";
101 return message.str();
104 static std::string UnknownExceptionToString()
106 std::ostringstream message;
108 "\033[1;5;31m\n=== Unhandled non-DPL exception occurred ===\033[m\n\n";
109 message << "\033[1;31m\n=== Will now abort ===\033[m\n";
111 return message.str();
114 static void DisplayKnownException(const Exception& e)
116 LogUnhandledException(KnownExceptionToString(e).c_str());
119 static void DisplayUnknownException()
121 LogUnhandledException(UnknownExceptionToString().c_str());
124 Exception(const Exception &other)
127 if (other.m_reason != NULL) {
128 m_reason = new Exception(*other.m_reason);
133 m_message = other.m_message;
134 m_path = other.m_path;
135 m_function = other.m_function;
136 m_line = other.m_line;
138 m_className = other.m_className;
143 const Exception &operator =(const Exception &other)
145 if (this == &other) {
150 if (other.m_reason != NULL) {
151 m_reason = new Exception(*other.m_reason);
156 m_message = other.m_message;
157 m_path = other.m_path;
158 m_function = other.m_function;
159 m_line = other.m_line;
161 m_className = other.m_className;
168 Exception(const char *path,
169 const char *function,
171 const std::string &message) :
174 m_function(function),
181 Exception(const char *path,
182 const char *function,
184 const Exception &reason,
185 const std::string &message) :
186 m_reason(new Exception(reason)),
188 m_function(function),
195 virtual ~Exception() throw()
197 if (m_reason != NULL) {
208 if (m_reason != NULL) {
212 // Afterward, dump exception
213 const char *file = strchr(m_path.c_str(), '/');
216 file = m_path.c_str();
221 printf("\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n",
225 m_message.empty() ? "<EMPTY>" : m_message.c_str());
228 std::string DumpToString() const
231 if (m_reason != NULL) {
232 ret = m_reason->DumpToString();
235 const char *file = strchr(m_path.c_str(), '/');
238 file = m_path.c_str();
246 "\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n",
251 m_message.empty() ? "<EMPTY>" : m_message.c_str());
253 buf[sizeof(buf) - 1] = '\n';
259 Exception *GetReason() const
264 std::string GetPath() const
269 std::string GetFunction() const
279 std::string GetMessage() const
284 std::string GetClassName() const
289 } // namespace ValidationCore
293 #define VcoreThrow(ClassName) \
294 throw ClassName(__FILE__, __FUNCTION__, __LINE__)
296 #define VcoreThrowMsg(ClassName, Message) \
299 std::ostringstream dplLoggingStream; \
300 dplLoggingStream << Message; \
301 throw ClassName(__FILE__, __FUNCTION__, __LINE__, dplLoggingStream.str()); \
304 #define VcoreReThrow(ClassName) \
305 throw ClassName(__FILE__, __FUNCTION__, __LINE__, _rethrown_exception)
307 #define VcoreReThrowMsg(ClassName, Message) \
308 throw ClassName(__FILE__, \
311 _rethrown_exception, \
314 #define VcoreCatch(ClassName) \
315 catch (const ClassName &_rethrown_exception)
317 #define VCORE_DECLARE_EXCEPTION_TYPE(BaseClass, Class) \
318 class Class : public BaseClass { \
320 Class(const char *path, \
321 const char *function, \
323 const std::string & message = std::string()) \
324 : BaseClass(path, function, line, message) { \
326 BaseClass::m_className = #Class; \
329 Class(const char *path, \
330 const char *function, \
332 const ValidationCore::Exception & reason, \
333 const std::string & message = std::string()) \
334 : BaseClass(path, function, line, reason, message) { \
335 BaseClass::m_className = #Class; \
339 #define VCORE_UNHANDLED_EXCEPTION_HANDLER_BEGIN try
341 #define VCORE_UNHANDLED_EXCEPTION_HANDLER_END \
342 catch (const ValidationCore::Exception &exception) \
344 std::ostringstream msg; \
345 msg << ValidationCore::Exception::KnownExceptionToString(exception); \
346 ValidationCore::LogUnhandledException(msg.str(), \
352 catch (std::exception& e) \
354 std::ostringstream msg; \
357 msg << ValidationCore::Exception::UnknownExceptionToString(); \
358 ValidationCore::LogUnhandledException(msg.str(), \
366 std::ostringstream msg; \
367 msg << ValidationCore::Exception::UnknownExceptionToString(); \
368 ValidationCore::LogUnhandledException(msg.str(), \
375 namespace ValidationCore {
376 namespace CommonException {
378 * Internal exception definitions
380 * These should normally not happen.
381 * Usually, exception trace with internal error includes
382 * important messages.
384 VCORE_DECLARE_EXCEPTION_TYPE(Exception, InternalError) ///< Unexpected error from
385 // underlying libraries or
390 #endif // ValidationCore_EXCEPTION_H