1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
23 #include "base/i2-base.h"
24 #include "base/stacktrace.h"
26 #include <boost/thread/tss.hpp>
27 #include <boost/exception/errinfo_api_function.hpp>
28 #include <boost/exception/errinfo_errno.hpp>
29 #include <boost/exception/errinfo_file_name.hpp>
32 # include <boost/algorithm/string/trim.hpp>
39 * Base class for all exceptions.
43 class I2_BASE_API Exception
46 static StackTrace *GetLastStackTrace(void);
47 static void SetLastStackTrace(const StackTrace& trace);
50 static boost::thread_specific_ptr<StackTrace> m_LastStackTrace;
53 typedef boost::error_info<StackTrace, StackTrace> StackTraceErrorInfo;
55 class I2_BASE_API posix_error : virtual public std::exception, virtual public boost::exception { };
58 class I2_BASE_API win32_error : virtual public std::exception, virtual public boost::exception { };
60 struct errinfo_win32_error_;
61 typedef boost::error_info<struct errinfo_win32_error_, int> errinfo_win32_error;
63 inline std::string to_string(const errinfo_win32_error& e)
65 std::ostringstream tmp;
69 String result = "Unknown error.";
71 DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
72 FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message,
76 result = String(message);
79 /* remove trailing new-line characters */
80 boost::algorithm::trim_right(result);
83 tmp << code << ", \"" << result << "\"";
90 #endif /* EXCEPTION_H */