icinga2/lib/base/exception.h

91 lines
3.0 KiB
C
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 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"
#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>
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
{
2012-04-22 16:45:31 +02:00
/**
* Base class for all exceptions.
2012-05-18 22:21:28 +02:00
*
* @ingroup base
2012-04-22 16:45:31 +02:00
*/
2013-03-16 21:18:53 +01:00
class I2_BASE_API Exception
{
2012-04-22 16:45:31 +02:00
public:
static StackTrace *GetLastStackTrace(void);
static void SetLastStackTrace(const StackTrace& trace);
private:
static boost::thread_specific_ptr<StackTrace> m_LastStackTrace;
};
typedef boost::error_info<StackTrace, StackTrace> StackTraceErrorInfo;
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 */
2012-04-16 16:27:41 +02:00
}
#endif /* EXCEPTION_H */