]> granicus.if.org Git - icinga2/blob - lib/base/exception.h
174a377fcf0b342da484d6a29fe4105cca931578
[icinga2] / lib / base / exception.h
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef EXCEPTION_H
21 #define EXCEPTION_H
22
23 #include "base/i2-base.h"
24 #include "base/stacktrace.h"
25 #include <sstream>
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>
30
31 #ifdef _WIN32
32 #       include <boost/algorithm/string/trim.hpp>
33 #endif /* _WIN32 */
34
35 namespace icinga
36 {
37
38 /**
39  * Base class for all exceptions.
40  *
41  * @ingroup base
42  */
43 class I2_BASE_API Exception
44 {
45 public:
46         static StackTrace *GetLastStackTrace(void);
47         static void SetLastStackTrace(const StackTrace& trace);
48
49 private:
50         static boost::thread_specific_ptr<StackTrace> m_LastStackTrace;
51 };
52
53 typedef boost::error_info<StackTrace, StackTrace> StackTraceErrorInfo;
54
55 class I2_BASE_API posix_error : virtual public std::exception, virtual public boost::exception { };
56
57 #ifdef _WIN32
58 class I2_BASE_API win32_error : virtual public std::exception, virtual public boost::exception { };
59
60 struct errinfo_win32_error_;
61 typedef boost::error_info<struct errinfo_win32_error_, int> errinfo_win32_error;
62
63 inline std::string to_string(const errinfo_win32_error& e)
64 {
65         std::ostringstream tmp;
66         int code = e.value();
67
68         char *message;
69         String result = "Unknown error.";
70
71         DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
72                 FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message,
73                 0, NULL);
74
75         if (rc != 0) {
76                 result = String(message);
77                 LocalFree(message);
78
79                 /* remove trailing new-line characters */
80                 boost::algorithm::trim_right(result);
81         }
82
83         tmp << code << ", \"" << result << "\"";
84         return tmp.str();
85 }
86 #endif /* _WIN32 */
87
88 }
89
90 #endif /* EXCEPTION_H */