GDAL

cpl_error.h
Go to the documentation of this file.
1 /**********************************************************************
2 * $Id$
3 *
4 * Name: cpl_error.h
5 * Project: CPL - Common Portability Library
6 * Purpose: CPL Error handling
7 * Author: Daniel Morissette, danmo@videotron.ca
8 *
9 **********************************************************************
10 * Copyright (c) 1998, Daniel Morissette
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 ****************************************************************************/
30
31 #ifndef CPL_ERROR_H_INCLUDED
32 #define CPL_ERROR_H_INCLUDED
33
34 #include "cpl_port.h"
35
36 #include <stdarg.h>
37 #include <stddef.h>
38
39 /*=====================================================================
40 Error handling functions (cpl_error.c)
41 =====================================================================*/
42
49 CPL_C_START
50
52 typedef enum
53 {
54 CE_None = 0,
55 CE_Debug = 1,
56 CE_Warning = 2,
57 CE_Failure = 3,
58 CE_Fatal = 4
59 } CPLErr;
60
61 /* ==================================================================== */
62 /* Well known error codes. */
63 /* ==================================================================== */
64
65 #ifdef STRICT_CPLERRORNUM_TYPE
66
67 /* This is not appropriate for the general case, as there are parts */
68 /* of GDAL which use custom error codes, but this can help diagnose confusions */
69 /* between CPLErr and CPLErrorNum */
70 typedef enum
71 {
72 CPLE_None,
73 CPLE_AppDefined,
74 CPLE_OutOfMemory,
75 CPLE_FileIO,
76 CPLE_OpenFailed,
77 CPLE_IllegalArg,
78 CPLE_NotSupported,
79 CPLE_AssertionFailed,
80 CPLE_NoWriteAccess,
81 CPLE_UserInterrupt,
82 CPLE_ObjectNull,
83 CPLE_HttpResponse,
84 CPLE_AWSBucketNotFound,
85 CPLE_AWSObjectNotFound,
86 CPLE_AWSAccessDenied,
87 CPLE_AWSInvalidCredentials,
88 CPLE_AWSSignatureDoesNotMatch,
89 } CPLErrorNum;
90
91 #else
92
94 typedef int CPLErrorNum;
95
97 #define CPLE_None 0
99 #define CPLE_AppDefined 1
101 #define CPLE_OutOfMemory 2
103 #define CPLE_FileIO 3
105 #define CPLE_OpenFailed 4
107 #define CPLE_IllegalArg 5
109 #define CPLE_NotSupported 6
111 #define CPLE_AssertionFailed 7
113 #define CPLE_NoWriteAccess 8
115 #define CPLE_UserInterrupt 9
117 #define CPLE_ObjectNull 10
118
119 /*
120 * Filesystem-specific errors
121 */
123 #define CPLE_HttpResponse 11
125 #define CPLE_AWSBucketNotFound 12
127 #define CPLE_AWSObjectNotFound 13
129 #define CPLE_AWSAccessDenied 14
131 #define CPLE_AWSInvalidCredentials 15
133 #define CPLE_AWSSignatureDoesNotMatch 16
135 #define CPLE_AWSError 17
136
137 /* 100 - 299 reserved for GDAL */
138
139 #endif
140
141 void CPL_DLL CPLError(CPLErr eErrClass, CPLErrorNum err_no, CPL_FORMAT_STRING(const char *fmt), ...) CPL_PRINT_FUNC_FORMAT (3, 4);
142 void CPL_DLL CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list );
143 void CPL_DLL CPLEmergencyError( const char * ) CPL_NO_RETURN;
144 void CPL_DLL CPL_STDCALL CPLErrorReset( void );
145 CPLErrorNum CPL_DLL CPL_STDCALL CPLGetLastErrorNo( void );
146 CPLErr CPL_DLL CPL_STDCALL CPLGetLastErrorType( void );
147 const char CPL_DLL * CPL_STDCALL CPLGetLastErrorMsg( void );
148 GUInt32 CPL_DLL CPL_STDCALL CPLGetErrorCounter( void );
149 void CPL_DLL * CPL_STDCALL CPLGetErrorHandlerUserData(void);
150 void CPL_DLL CPLErrorSetState( CPLErr eErrClass, CPLErrorNum err_no, const char* pszMsg );
152 void CPL_DLL CPLCleanupErrorMutex( void );
156 typedef void (CPL_STDCALL *CPLErrorHandler)(CPLErr, CPLErrorNum, const char*);
157
158 void CPL_DLL CPL_STDCALL CPLLoggingErrorHandler( CPLErr, CPLErrorNum, const char * );
159 void CPL_DLL CPL_STDCALL CPLDefaultErrorHandler( CPLErr, CPLErrorNum, const char * );
160 void CPL_DLL CPL_STDCALL CPLQuietErrorHandler( CPLErr, CPLErrorNum, const char * );
161 void CPLTurnFailureIntoWarning( int bOn );
162
163 CPLErrorHandler CPL_DLL CPL_STDCALL CPLSetErrorHandler( CPLErrorHandler );
164 CPLErrorHandler CPL_DLL CPL_STDCALL CPLSetErrorHandlerEx( CPLErrorHandler, void* );
165 void CPL_DLL CPL_STDCALL CPLPushErrorHandler( CPLErrorHandler );
166 void CPL_DLL CPL_STDCALL CPLPushErrorHandlerEx( CPLErrorHandler, void* );
167 void CPL_DLL CPL_STDCALL CPLSetCurrentErrorHandlerCatchDebug( int bCatchDebug );
168 void CPL_DLL CPL_STDCALL CPLPopErrorHandler(void);
169
170 #ifdef WITHOUT_CPLDEBUG
171 #define CPLDebug(...) /* Eat all CPLDebug calls. */
172 #else
173 void CPL_DLL CPL_STDCALL CPLDebug(const char *, CPL_FORMAT_STRING(const char *), ...)
174 CPL_PRINT_FUNC_FORMAT(2, 3);
175 #endif
176
177 #ifdef DEBUG
181 #define CPLDebugOnly(...) CPLDebug(__VA_ARGS__)
182 #else
186 #define CPLDebugOnly(...)
187 #endif
188
189 void CPL_DLL CPL_STDCALL _CPLAssert( const char *, const char *, int ) CPL_NO_RETURN;
190
191 #if defined(DEBUG) && !defined(CPPCHECK)
193 # define CPLAssert(expr) ((expr) ? (void)(0) : _CPLAssert(#expr,__FILE__,__LINE__))
195 # define CPLAssertAlwaysEval(expr) CPLAssert(expr)
196 #else
198 # define CPLAssert(expr)
199 #ifdef __cplusplus
201 # define CPLAssertAlwaysEval(expr) CPL_IGNORE_RET_VAL(expr)
202 #else
204 # define CPLAssertAlwaysEval(expr) (void)(expr)
205 #endif
206 #endif
207
208 CPL_C_END
209
211 /*
212 * Helper macros used for input parameters validation.
213 */
214 #ifdef DEBUG
215 # define VALIDATE_POINTER_ERR CE_Fatal
216 #else
217 # define VALIDATE_POINTER_ERR CE_Failure
218 #endif
219
220
221 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && !defined(DOXYGEN_SKIP)
222
223 extern "C++"
224 {
225
226 #include <string>
227
228 class CPLErrorHandlerPusher
229 {
230 public:
231 explicit CPLErrorHandlerPusher(CPLErrorHandler hHandler)
232 {
233 CPLPushErrorHandler(hHandler);
234 }
235
236 CPLErrorHandlerPusher(CPLErrorHandler hHandler, void* user_data)
237 {
238 CPLPushErrorHandlerEx(hHandler, user_data);
239 }
240
241 ~CPLErrorHandlerPusher()
242 {
243 CPLPopErrorHandler();
244 }
245 };
246
247 class CPLErrorStateBackuper
248 {
249 CPLErrorNum m_nLastErrorNum;
250 CPLErr m_nLastErrorType;
251 std::string m_osLastErrorMsg;
252
253 public:
254 CPLErrorStateBackuper() :
255 m_nLastErrorNum(CPLGetLastErrorNo()),
256 m_nLastErrorType(CPLGetLastErrorType()),
257 m_osLastErrorMsg(CPLGetLastErrorMsg())
258 {}
259
260 ~CPLErrorStateBackuper()
261 {
262 CPLErrorSetState(m_nLastErrorType, m_nLastErrorNum,
263 m_osLastErrorMsg.c_str());
264 }
265 };
266
267 }
268
269 #ifdef GDAL_COMPILATION
270 // internal only
271 bool CPLIsDefaultErrorHandlerAndCatchDebug();
272 #endif
273
274 #endif
275
279 #define VALIDATE_POINTER0(ptr, func) \
280 do { if( CPL_NULLPTR == ptr ) \
281 { \
282 CPLErr const ret = VALIDATE_POINTER_ERR; \
283 CPLError( ret, CPLE_ObjectNull, \
284 "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func)); \
285 return; }} while(0)
286
288 #define VALIDATE_POINTER1(ptr, func, rc) \
289 do { if( CPL_NULLPTR == ptr ) \
290 { \
291 CPLErr const ret = VALIDATE_POINTER_ERR; \
292 CPLError( ret, CPLE_ObjectNull, \
293 "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func)); \
294 return (rc); }} while(0)
295
296 #endif /* CPL_ERROR_H_INCLUDED */
CPLE_IllegalArg
#define CPLE_IllegalArg
Illegal argument.
Definition: cpl_error.h:107
CPLErrorSetState
void CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, const char *pszMsg)
Restore an error state, without emitting an error.
Definition: cpl_error.cpp:775
CPLGetErrorCounter
GUInt32 CPLGetErrorCounter(void)
Get the error counter.
Definition: cpl_error.cpp:900
CPLE_AWSObjectNotFound
#define CPLE_AWSObjectNotFound
AWSObjectNotFound.
Definition: cpl_error.h:127
CPLE_AssertionFailed
#define CPLE_AssertionFailed
Assertion failed.
Definition: cpl_error.h:111
CPLGetErrorHandlerUserData
void * CPLGetErrorHandlerUserData(void)
Fetch the user data for the error context.
Definition: cpl_error.cpp:188
CPLPushErrorHandlerEx
void CPLPushErrorHandlerEx(CPLErrorHandler, void *)
Push a new CPLError handler with user data on the error context.
Definition: cpl_error.cpp:1219
CPLE_AWSSignatureDoesNotMatch
#define CPLE_AWSSignatureDoesNotMatch
AWSSignatureDoesNotMatch.
Definition: cpl_error.h:133
CPLE_None
#define CPLE_None
No error.
Definition: cpl_error.h:97
CPLErrorReset
void CPLErrorReset(void)
Erase any traces of previous errors.
Definition: cpl_error.cpp:740
CPLLoggingErrorHandler
void CPLLoggingErrorHandler(CPLErr, CPLErrorNum, const char *)
Error handler that logs into the file defined by the CPL_LOG configuration option,...
Definition: cpl_error.cpp:993
CPLE_NotSupported
#define CPLE_NotSupported
Not supported.
Definition: cpl_error.h:109
CPLE_AWSInvalidCredentials
#define CPLE_AWSInvalidCredentials
AWSInvalidCredentials.
Definition: cpl_error.h:131
CPLErr
CPLErr
Error category.
Definition: cpl_error.h:53
CPLE_FileIO
#define CPLE_FileIO
File I/O error.
Definition: cpl_error.h:103
CPLGetLastErrorNo
CPLErrorNum CPLGetLastErrorNo(void)
Fetch the last error number.
Definition: cpl_error.cpp:829
CPLE_AppDefined
#define CPLE_AppDefined
Application defined error.
Definition: cpl_error.h:99
CPLE_OpenFailed
#define CPLE_OpenFailed
Open failed.
Definition: cpl_error.h:105
CPLSetErrorHandler
CPLErrorHandler CPLSetErrorHandler(CPLErrorHandler)
Install custom error handler.
Definition: cpl_error.cpp:1175
CPLGetLastErrorMsg
const char * CPLGetLastErrorMsg(void)
Get the last error message.
Definition: cpl_error.cpp:877
CPLE_AWSAccessDenied
#define CPLE_AWSAccessDenied
AWSAccessDenied.
Definition: cpl_error.h:129
CPLE_ObjectNull
#define CPLE_ObjectNull
NULL object.
Definition: cpl_error.h:117
CPLDefaultErrorHandler
void CPLDefaultErrorHandler(CPLErr, CPLErrorNum, const char *)
Default error handler.
Definition: cpl_error.cpp:914
CPLGetLastErrorType
CPLErr CPLGetLastErrorType(void)
Fetch the last error type.
Definition: cpl_error.cpp:853
CPLErrorNum
int CPLErrorNum
Error number.
Definition: cpl_error.h:94
CPLTurnFailureIntoWarning
void CPLTurnFailureIntoWarning(int bOn)
Whether failures should be turned into warnings.
Definition: cpl_error.cpp:1069
CPLEmergencyError
void CPLEmergencyError(const char *)
Fatal error when things are bad.
Definition: cpl_error.cpp:491
CPLErrorHandler
void(* CPLErrorHandler)(CPLErr, CPLErrorNum, const char *)
Callback for a custom error handler.
Definition: cpl_error.h:156
_CPLAssert
void _CPLAssert(const char *, const char *, int)
Report failure of a logical assertion.
Definition: cpl_error.cpp:1326
CPLE_HttpResponse
#define CPLE_HttpResponse
HTTP response.
Definition: cpl_error.h:123
CPLE_AWSBucketNotFound
#define CPLE_AWSBucketNotFound
AWSBucketNotFound.
Definition: cpl_error.h:125
CPLPopErrorHandler
void CPLPopErrorHandler(void)
Pop error handler off stack.
Definition: cpl_error.cpp:1253
CPLDebug
void CPLDebug(const char *, const char *,...)
Display a debugging message.
Definition: cpl_error.cpp:607
CPLPushErrorHandler
void CPLPushErrorHandler(CPLErrorHandler)
Push a new CPLError handler.
Definition: cpl_error.cpp:1196
CPLE_NoWriteAccess
#define CPLE_NoWriteAccess
No write access.
Definition: cpl_error.h:113
CPLE_UserInterrupt
#define CPLE_UserInterrupt
User interrupted.
Definition: cpl_error.h:115
CPLErrorV
void CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list)
Same as CPLError() but with a va_list.
Definition: cpl_error.cpp:326
CPLQuietErrorHandler
void CPLQuietErrorHandler(CPLErr, CPLErrorNum, const char *)
Error handler that does not do anything, except for debug messages.
Definition: cpl_error.cpp:978
CPLE_OutOfMemory
#define CPLE_OutOfMemory
Out of memory error.
Definition: cpl_error.h:101
CPLSetErrorHandlerEx
CPLErrorHandler CPLSetErrorHandlerEx(CPLErrorHandler, void *)
Install custom error handle with user's data.
Definition: cpl_error.cpp:1101
CPLSetCurrentErrorHandlerCatchDebug
void CPLSetCurrentErrorHandlerCatchDebug(int bCatchDebug)
Set if the current error handler should intercept debug messages, or if they should be processed by t...
Definition: cpl_error.cpp:1292
CPLError
void CPLError(CPLErr eErrClass, CPLErrorNum err_no, const char *fmt,...)
Report an error.
Definition: cpl_error.cpp:310
cpl_port.h
Core portability definitions for CPL.
CPL_NO_RETURN
#define CPL_NO_RETURN
Qualifier for a function that does not return at all (terminates the process)
Definition: cpl_port.h:963
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:339
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:337
CPL_FORMAT_STRING
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition: cpl_port.h:936
GUInt32
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:207
CPL_PRINT_FUNC_FORMAT
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:921