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 DPL_EXCEPTION_H
23 #define DPL_EXCEPTION_H
34 void LogUnhandledException(const std::string &str);
35 void LogUnhandledException(const std::string &str, const char *filename, int line, const char *function);
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);
53 m_lastException = exception;
56 static void UnRef(Exception* e)
58 if (m_lastException == e)
59 m_lastException = NULL;
63 if (!m_exceptionCount)
65 std::set_terminate(m_terminateHandler);
66 m_terminateHandler = NULL;
70 static void TerminateHandler()
72 if (m_lastException != NULL)
74 DisplayKnownException(*m_lastException);
79 DisplayUnknownException();
86 std::string m_function;
90 std::string m_message;
91 std::string m_className;
94 static std::string KnownExceptionToString(const Exception &e)
96 std::ostringstream message;
97 message << "\033[1;5;31m\n=== Unhandled DPL exception occurred ===\033[m\n\n";
98 message << "\033[1;33mException trace:\033[m\n\n";
99 message << e.DumpToString();
100 message << "\033[1;31m\n=== Will now abort ===\033[m\n";
102 return message.str();
105 static std::string UnknownExceptionToString()
107 std::ostringstream message;
108 message << "\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);
132 m_message = other.m_message;
133 m_path = other.m_path;
134 m_function = other.m_function;
135 m_line = other.m_line;
137 m_className = other.m_className;
142 const Exception &operator =(const Exception &other)
148 if (other.m_reason != NULL)
149 m_reason = new Exception(*other.m_reason);
153 m_message = other.m_message;
154 m_path = other.m_path;
155 m_function = other.m_function;
156 m_line = other.m_line;
158 m_className = other.m_className;
165 Exception(const char *path, const char *function, int line, const std::string &message)
168 m_function(function),
175 Exception(const char *path, const char *function, int line, const Exception &reason, const std::string &message)
176 : m_reason(new Exception(reason)),
178 m_function(function),
185 virtual ~Exception() throw()
187 if (m_reason != NULL)
199 if (m_reason != NULL)
202 // Afterward, dump exception
203 const char *file = strchr(m_path.c_str(), '/');
206 file = m_path.c_str();
210 printf("\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n",
214 m_message.empty() ? "<EMPTY>" : m_message.c_str());
217 std::string DumpToString() const
220 if (m_reason != NULL)
221 ret = m_reason->DumpToString();
223 const char *file = strchr(m_path.c_str(), '/');
226 file = m_path.c_str();
231 snprintf(buf, sizeof(buf), "\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n",
235 m_message.empty() ? "<EMPTY>" : m_message.c_str());
237 buf[sizeof(buf)-1] = '\n';
243 Exception *GetReason() const
248 std::string GetPath() const
253 std::string GetFunction() const
263 std::string GetMessage() const
268 std::string GetClassName() const
277 #define Throw(ClassName) \
278 throw ClassName(__FILE__, __FUNCTION__, __LINE__)
280 #define ThrowMsg(ClassName, Message) \
283 std::ostringstream dplLoggingStream; \
284 dplLoggingStream << Message; \
285 throw ClassName(__FILE__, __FUNCTION__, __LINE__, dplLoggingStream.str()); \
288 #define ReThrow(ClassName) \
289 throw ClassName(__FILE__, __FUNCTION__, __LINE__, _rethrown_exception)
291 #define ReThrowMsg(ClassName, Message) \
292 throw ClassName(__FILE__, __FUNCTION__, __LINE__, _rethrown_exception, Message)
294 #define Catch(ClassName) \
295 catch (const ClassName &_rethrown_exception)
297 #define DECLARE_EXCEPTION_TYPE(BaseClass, Class) \
302 Class(const char *path, const char *function, int line, const std::string &message = std::string()) \
303 : BaseClass(path, function, line, message) \
305 BaseClass::m_className = #Class; \
308 Class(const char *path, const char *function, int line, const DPL::Exception &reason, const std::string &message = std::string()) \
309 : BaseClass(path, function, line, reason, message) \
311 BaseClass::m_className = #Class; \
315 #define UNHANDLED_EXCEPTION_HANDLER_BEGIN try
317 #define UNHANDLED_EXCEPTION_HANDLER_END \
318 catch (const DPL::Exception &exception) \
320 std::ostringstream msg; \
321 msg << DPL::Exception::KnownExceptionToString(exception); \
322 DPL::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \
325 catch (std::exception& e) \
327 std::ostringstream msg; \
330 msg << DPL::Exception::UnknownExceptionToString(); \
331 DPL::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \
336 std::ostringstream msg; \
337 msg << DPL::Exception::UnknownExceptionToString(); \
338 DPL::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \
344 namespace CommonException
347 * Internal exception definitions
349 * These should normally not happen.
350 * Usually, exception trace with internal error includes
351 * important messages.
353 DECLARE_EXCEPTION_TYPE(Exception, InternalError) ///< Unexpected error from underlying libraries or kernel
357 #endif // DPL_EXCEPTION_H