icinga2/lib/base/exception.h

114 lines
3.7 KiB
C
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2013-09-25 07:43:57 +02:00
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef EXCEPTION_H
#define EXCEPTION_H
2013-03-16 21:18:53 +01:00
#include "base/i2-base.h"
2013-03-18 22:40:40 +01:00
#include "base/qstring.h"
2013-03-16 21:18:53 +01:00
#include "base/stacktrace.h"
#include <sstream>
2013-03-15 18:21:29 +01:00
#include <boost/thread/tss.hpp>
2013-03-18 17:04:22 +01:00
#include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp>
#include <boost/exception/errinfo_file_name.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception_ptr.hpp>
2013-03-15 18:21:29 +01:00
2013-03-16 21:18:53 +01:00
#ifdef _WIN32
# include <boost/algorithm/string/trim.hpp>
#endif /* _WIN32 */
namespace icinga
{
I2_BASE_API StackTrace *GetLastExceptionStack(void);
I2_BASE_API void SetLastExceptionStack(const StackTrace& trace);
typedef boost::error_info<StackTrace, StackTrace> StackTraceErrorInfo;
template<typename T>
String DiagnosticInformation(const T& ex, StackTrace *trace = NULL)
{
std::ostringstream result;
result << boost::diagnostic_information(ex);
if (boost::get_error_info<StackTraceErrorInfo>(ex) == NULL) {
result << std::endl;
if (trace)
result << *trace;
else
result << *GetLastExceptionStack();
}
return result.str();
}
I2_BASE_API String DiagnosticInformation(boost::exception_ptr eptr);
2013-03-11 13:45:08 +01:00
class I2_BASE_API posix_error : virtual public std::exception, virtual public boost::exception { };
2012-05-18 22:21:28 +02:00
2012-04-22 16:45:31 +02:00
#ifdef _WIN32
2013-03-11 14:03:01 +01:00
class I2_BASE_API win32_error : virtual public std::exception, virtual public boost::exception { };
2013-03-18 17:04:22 +01:00
struct errinfo_win32_error_;
2013-03-11 13:45:08 +01:00
typedef boost::error_info<struct errinfo_win32_error_, int> errinfo_win32_error;
inline std::string to_string(const errinfo_win32_error& e)
2012-04-22 16:45:31 +02:00
{
2013-03-16 21:18:53 +01:00
std::ostringstream tmp;
2013-03-11 13:45:08 +01:00
int code = e.value();
char *message;
String result = "Unknown error.";
DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message,
0, NULL);
if (rc != 0) {
result = String(message);
LocalFree(message);
/* remove trailing new-line characters */
boost::algorithm::trim_right(result);
}
tmp << code << ", \"" << result << "\"";
return tmp.str();
}
2012-04-22 16:45:31 +02:00
#endif /* _WIN32 */
struct errinfo_getaddrinfo_error_;
typedef boost::error_info<struct errinfo_getaddrinfo_error_, int> errinfo_getaddrinfo_error;
inline std::string to_string(const errinfo_getaddrinfo_error& e)
{
return gai_strerror(e.value());
}
struct errinfo_message_;
typedef boost::error_info<struct errinfo_message_, std::string> errinfo_message;
2012-04-16 16:27:41 +02:00
}
#endif /* EXCEPTION_H */