-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExceptions.h
More file actions
148 lines (112 loc) · 4.52 KB
/
Exceptions.h
File metadata and controls
148 lines (112 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*************************************************************************************
cpl - cross platform library - v. 0.1.0.
Copyright (C) 2017 Janus Lynggaard Thorborg (www.jthorborg.com)
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 3 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, see <http://www.gnu.org/licenses/>.
See \licenses\ for additional details on licenses associated with this program.
**************************************************************************************
file:Exceptions.h
Runtime exceptions, assertions, logging
*************************************************************************************/
#ifndef CPL_EXCEPTIONS_H
#define CPL_EXCEPTIONS_H
#include <string>
#include <string_view>
#include <exception>
#include <stdexcept>
#include <string>
#include <cerrno>
#include "MacroConstants.h"
#include "ProgramInfo.h"
#include "Core.h"
namespace cpl
{
int GetLastOSError();
std::string GetLastOSErrorMessage();
std::string GetLastOSErrorMessage(int errorToPrint);
const std::string& GetExceptionLogFilePath();
void CheckPruneExceptionLogFile();
void LogException(const string_ref errorMessage);
void CrashIfUserDoesntDebug(const string_ref errorMessage);
namespace Misc
{
bool IsBeingDebugged();
void OutputToDebugger(const string_ref message);
};
bool IsDebuggerAttached();
class CPLRuntimeException : public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
};
class CPLNotImplementedException : public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
};
class CPLAssertionException : public CPLRuntimeException
{
public:
using CPLRuntimeException::CPLRuntimeException;
};
#define CPL_EXPANDED_MESSAGE message
#define CPL_INTERNAL_EXCEPTION(msg, file, line, funcname, isassert, exceptionT, exceptionExpression) \
do \
{ \
std::string message = std::string("Runtime exception (" #exceptionT ") in ") + ::cpl::programInfo.name + " (" + ::cpl::programInfo.version.toString() + "): \"" + msg + "\" in " + file + ":" + ::std::to_string(line) + " -> " + funcname; \
auto e = exceptionExpression;\
cpl::Misc::OutputToDebugger(message + "\n"); \
cpl::LogException(message); \
if(cpl::IsDebuggerAttached()) DBG_BREAK(); \
bool doAbort = isassert; \
if(doAbort) \
std::abort(); \
else \
throw e; \
} while(0)
#define CPL_EVAL(a, b) a b
#define CPL_RUNTIME_EXCEPTION_SPECIFIC(msg, exceptionT) \
CPL_INTERNAL_EXCEPTION(msg, __FILE__, __LINE__, __func__, false, exceptionT, exceptionT (CPL_EXPANDED_MESSAGE))
#define CPL_RUNTIME_EXCEPTION(msg) \
CPL_RUNTIME_EXCEPTION_SPECIFIC(msg, cpl::CPLRuntimeException)
#define CPL_RUNTIME_EXCEPTION_SPECIFIC_ARGS(msg, exceptionT, args) \
CPL_INTERNAL_EXCEPTION(msg, \
__FILE__, \
__LINE__, \
__func__, \
false, \
exceptionT, \
CPL_EVAL(exceptionT,args) \
)\
/// <summary>
/// Throws a suitable system_error from errno with a what() message
/// </summary>
#define CPL_POSIX_EXCEPTION(msg) CPL_RUNTIME_EXCEPTION_SPECIFIC_ARGS(msg, std::system_error, (errno, std::generic_category(), CPL_EXPANDED_MESSAGE))
#ifdef CPL_WINDOWS
#define CPL_SYSTEM_EXCEPTION(msg) CPL_RUNTIME_EXCEPTION_SPECIFIC_ARGS(msg, \
std::system_error, (cpl::GetLastOSError(), std::system_category(), CPL_EXPANDED_MESSAGE))
#else
#define CPL_SYSTEM_EXCEPTION(msg) CPL_RUNTIME_EXCEPTION_SPECIFIC_ARGS(msg, \
std::system_error, (errno, std::system_category(), CPL_EXPANDED_MESSAGE))
#endif
#define CPL_RUNTIME_ASSERTION(expression) \
if(!(expression)) \
CPL_INTERNAL_EXCEPTION("Runtime assertion failed: " #expression, \
__FILE__, __LINE__, __func__, true, cpl::CPLAssertionException, cpl::CPLAssertionException (CPL_EXPANDED_MESSAGE))
#define CPL_NOTIMPLEMENTED_EXCEPTION() \
CPL_RUNTIME_EXCEPTION_SPECIFIC("The requested behaviour is not implemented (yet)", cpl::CPLNotImplementedException)
#ifdef CPL_MSVC
#define CPL_UNREACHABLE() __assume(0)
#else
#define CPL_UNREACHABLE() __builtin_unreachable()
#endif
}
#endif