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 
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  */
70 /* between CPLErr and CPLErrorNum */
71 typedef enum
72 {
73  CPLE_None,
90 } CPLErrorNum;
91 
92 #else
93 
95 typedef int CPLErrorNum;
96 
98 #define CPLE_None 0
100 #define CPLE_AppDefined 1
102 #define CPLE_OutOfMemory 2
104 #define CPLE_FileIO 3
106 #define CPLE_OpenFailed 4
108 #define CPLE_IllegalArg 5
110 #define CPLE_NotSupported 6
112 #define CPLE_AssertionFailed 7
114 #define CPLE_NoWriteAccess 8
116 #define CPLE_UserInterrupt 9
118 #define CPLE_ObjectNull 10
119 
120 /*
121  * Filesystem-specific errors
122  */
124 #define CPLE_HttpResponse 11
126 #define CPLE_AWSBucketNotFound 12
128 #define CPLE_AWSObjectNotFound 13
130 #define CPLE_AWSAccessDenied 14
132 #define CPLE_AWSInvalidCredentials 15
134 #define CPLE_AWSSignatureDoesNotMatch 16
136 #define CPLE_AWSError 17
137 
138 /* 100 - 299 reserved for GDAL */
139 
140 #endif
141 
142 void CPL_DLL CPLError(CPLErr eErrClass, CPLErrorNum err_no,
143  CPL_FORMAT_STRING(const char *fmt), ...)
144  CPL_PRINT_FUNC_FORMAT(3, 4);
145 void CPL_DLL CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list);
146 void CPL_DLL CPLEmergencyError(const char *) CPL_NO_RETURN;
147 void CPL_DLL CPL_STDCALL CPLErrorReset(void);
148 CPLErrorNum CPL_DLL CPL_STDCALL CPLGetLastErrorNo(void);
149 CPLErr CPL_DLL CPL_STDCALL CPLGetLastErrorType(void);
150 const char CPL_DLL *CPL_STDCALL CPLGetLastErrorMsg(void);
151 GUInt32 CPL_DLL CPL_STDCALL CPLGetErrorCounter(void);
152 void CPL_DLL *CPL_STDCALL CPLGetErrorHandlerUserData(void);
153 void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no,
154  const char *pszMsg);
155 void CPL_DLL CPLCallPreviousHandler(CPLErr eErrClass, CPLErrorNum err_no,
156  const char *pszMsg);
158 void CPL_DLL CPLCleanupErrorMutex(void);
162 typedef void(CPL_STDCALL *CPLErrorHandler)(CPLErr, CPLErrorNum, const char *);
163 
164 void CPL_DLL CPL_STDCALL CPLLoggingErrorHandler(CPLErr, CPLErrorNum,
165  const char *);
166 void CPL_DLL CPL_STDCALL CPLDefaultErrorHandler(CPLErr, CPLErrorNum,
167  const char *);
168 void CPL_DLL CPL_STDCALL CPLQuietErrorHandler(CPLErr, CPLErrorNum,
169  const char *);
170 void CPLTurnFailureIntoWarning(int bOn);
171 
172 CPLErrorHandler CPL_DLL CPLGetErrorHandler(void **ppUserData);
173 
176  void *);
177 void CPL_DLL CPL_STDCALL CPLPushErrorHandler(CPLErrorHandler);
178 void CPL_DLL CPL_STDCALL CPLPushErrorHandlerEx(CPLErrorHandler, void *);
179 void CPL_DLL CPL_STDCALL CPLSetCurrentErrorHandlerCatchDebug(int bCatchDebug);
180 void CPL_DLL CPL_STDCALL CPLPopErrorHandler(void);
181 
182 #ifdef WITHOUT_CPLDEBUG
183 #define CPLDebug(...) \
184  do \
185  { \
186  } while (0) /* Eat all CPLDebug calls. */
187 #define CPLDebugProgress(...) \
188  do \
189  { \
190  } while (0) /* Eat all CPLDebugProgress calls. */
191 #else
192 void CPL_DLL CPLDebug(const char *, CPL_FORMAT_STRING(const char *), ...)
193  CPL_PRINT_FUNC_FORMAT(2, 3);
194 void CPL_DLL CPLDebugProgress(const char *, CPL_FORMAT_STRING(const char *),
195  ...) CPL_PRINT_FUNC_FORMAT(2, 3);
196 #endif
197 
198 #ifdef DEBUG
202 #define CPLDebugOnly(...) CPLDebug(__VA_ARGS__)
203 #else
207 #define CPLDebugOnly(...) \
208  do \
209  { \
210  } while (0)
211 #endif
212 
213 void CPL_DLL CPL_STDCALL _CPLAssert(const char *, const char *,
214  int) CPL_NO_RETURN;
215 
216 #if defined(DEBUG) && !defined(CPPCHECK)
218 #define CPLAssert(expr) \
219  ((expr) ? (void)(0) : _CPLAssert(#expr, __FILE__, __LINE__))
222 #define CPLAssertAlwaysEval(expr) CPLAssert(expr)
223 #else
225 #define CPLAssert(expr) \
226  do \
227  { \
228  } while (0)
229 #ifdef __cplusplus
232 #define CPLAssertAlwaysEval(expr) CPL_IGNORE_RET_VAL(expr)
233 #else
236 #define CPLAssertAlwaysEval(expr) (void)(expr)
237 #endif
238 #endif
239 
240 CPL_C_END
241 
243 /*
244  * Helper macros used for input parameters validation.
245  */
246 #ifdef DEBUG
247 #define VALIDATE_POINTER_ERR CE_Fatal
248 #else
249 #define VALIDATE_POINTER_ERR CE_Failure
250 #endif
251 
254 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
255 
256 extern "C++"
257 {
259  template <class T> T *CPLAssertNotNull(T *x) CPL_RETURNS_NONNULL;
260 
261  template <class T> T *CPLAssertNotNull(T *x)
262  {
263  CPLAssert(x);
264  return x;
265  }
266 
267 #include <memory>
268 #include <string>
269 
275  class CPL_DLL CPLErrorHandlerPusher
276  {
277  public:
282  {
283  CPLPushErrorHandler(hHandler);
284  }
285 
289  CPLErrorHandlerPusher(CPLErrorHandler hHandler, void *user_data)
290  {
291  CPLPushErrorHandlerEx(hHandler, user_data);
292  }
293 
296  {
298  }
299  };
300 
304  class CPL_DLL CPLErrorStateBackuper
305  {
306  CPLErrorNum m_nLastErrorNum;
307  CPLErr m_nLastErrorType;
308  std::string m_osLastErrorMsg;
309  GUInt32 m_nLastErrorCounter;
310  std::unique_ptr<CPLErrorHandlerPusher> m_poErrorHandlerPusher;
311 
312  public:
316  explicit CPLErrorStateBackuper(CPLErrorHandler hHandler = nullptr);
317 
322  };
323 }
324 
325 #ifdef GDAL_COMPILATION
327 // internal only
328 bool CPLIsDefaultErrorHandlerAndCatchDebug();
330 #endif
331 
332 #endif
333 
335 #define VALIDATE_POINTER0(ptr, func) \
336  do \
337  { \
338  if (CPL_NULLPTR == ptr) \
339  { \
340  CPLErr const ret = VALIDATE_POINTER_ERR; \
341  CPLError(ret, CPLE_ObjectNull, \
342  "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func)); \
343  return; \
344  } \
345  } while (0)
346 
348 #define VALIDATE_POINTER1(ptr, func, rc) \
349  do \
350  { \
351  if (CPL_NULLPTR == ptr) \
352  { \
353  CPLErr const ret = VALIDATE_POINTER_ERR; \
354  CPLError(ret, CPLE_ObjectNull, \
355  "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func)); \
356  return (rc); \
357  } \
358  } while (0)
359 
360 #endif /* CPL_ERROR_H_INCLUDED */
Class that installs a (thread-local) error handler on construction, and restore the initial one on de...
Definition: cpl_error.h:276
CPLErrorHandlerPusher(CPLErrorHandler hHandler)
Constructor that installs a thread-local temporary error handler (typically CPLQuietErrorHandler)
Definition: cpl_error.h:281
CPLErrorHandlerPusher(CPLErrorHandler hHandler, void *user_data)
Constructor that installs a thread-local temporary error handler, and its user data.
Definition: cpl_error.h:289
~CPLErrorHandlerPusher()
Destructor that restores the initial error handler.
Definition: cpl_error.h:295
Class that saves the error state on construction, and restores it on destruction.
Definition: cpl_error.h:305
#define CPLE_IllegalArg
Illegal argument.
Definition: cpl_error.h:108
void CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, const char *pszMsg)
Restore an error state, without emitting an error.
Definition: cpl_error.cpp:898
GUInt32 CPLGetErrorCounter(void)
Get the error counter.
Definition: cpl_error.cpp:990
#define CPLE_AWSObjectNotFound
AWSObjectNotFound.
Definition: cpl_error.h:128
#define CPLE_AssertionFailed
Assertion failed.
Definition: cpl_error.h:112
void * CPLGetErrorHandlerUserData(void)
Fetch the user data for the error context.
Definition: cpl_error.cpp:167
void CPLPushErrorHandlerEx(CPLErrorHandler, void *)
Push a new CPLError handler with user data on the error context.
Definition: cpl_error.cpp:1356
#define CPLE_AWSSignatureDoesNotMatch
AWSSignatureDoesNotMatch.
Definition: cpl_error.h:134
#define CPLE_None
No error.
Definition: cpl_error.h:98
void CPLErrorReset(void)
Erase any traces of previous errors.
Definition: cpl_error.cpp:822
void CPLLoggingErrorHandler(CPLErr, CPLErrorNum, const char *)
Error handler that logs into the file defined by the CPL_LOG configuration option,...
Definition: cpl_error.cpp:1134
#define CPLE_NotSupported
Not supported.
Definition: cpl_error.h:110
#define CPLE_AWSInvalidCredentials
AWSInvalidCredentials.
Definition: cpl_error.h:132
#define CPLAssert(expr)
Assert on an expression.
Definition: cpl_error.h:225
CPLErr
Error category.
Definition: cpl_error.h:53
#define CPLE_FileIO
File I/O error.
Definition: cpl_error.h:104
CPLErrorNum CPLGetLastErrorNo(void)
Fetch the last error number.
Definition: cpl_error.cpp:919
void CPLDebugProgress(const char *, const char *,...)
Display a debugging message indicating a progression.
Definition: cpl_error.cpp:792
#define CPLE_AppDefined
Application defined error.
Definition: cpl_error.h:100
#define CPLE_OpenFailed
Open failed.
Definition: cpl_error.h:106
CPLErrorHandler CPLGetErrorHandler(void **ppUserData)
Fetch the current error handler for the current error context.
Definition: cpl_error.cpp:195
CPLErrorHandler CPLSetErrorHandler(CPLErrorHandler)
Install custom error handler.
Definition: cpl_error.cpp:1312
const char * CPLGetLastErrorMsg(void)
Get the last error message.
Definition: cpl_error.cpp:967
#define CPLE_AWSAccessDenied
AWSAccessDenied.
Definition: cpl_error.h:130
#define CPLE_ObjectNull
NULL object.
Definition: cpl_error.h:118
void CPLDefaultErrorHandler(CPLErr, CPLErrorNum, const char *)
Default error handler.
Definition: cpl_error.cpp:1024
CPLErr CPLGetLastErrorType(void)
Fetch the last error type.
Definition: cpl_error.cpp:943
int CPLErrorNum
Error number.
Definition: cpl_error.h:95
void CPLTurnFailureIntoWarning(int bOn)
Whether failures should be turned into warnings.
Definition: cpl_error.cpp:1207
void CPLCallPreviousHandler(CPLErr eErrClass, CPLErrorNum err_no, const char *pszMsg)
Call the previously installed error handler in the error handler stack.
Definition: cpl_error.cpp:1423
void CPLEmergencyError(const char *)
Fatal error when things are bad.
Definition: cpl_error.cpp:510
void(* CPLErrorHandler)(CPLErr, CPLErrorNum, const char *)
Callback for a custom error handler.
Definition: cpl_error.h:162
void _CPLAssert(const char *, const char *, int)
Report failure of a logical assertion.
Definition: cpl_error.cpp:1512
#define CPLE_HttpResponse
HTTP response.
Definition: cpl_error.h:124
#define CPLE_AWSBucketNotFound
AWSBucketNotFound.
Definition: cpl_error.h:126
void CPLPopErrorHandler(void)
Pop error handler off stack.
Definition: cpl_error.cpp:1390
void CPLDebug(const char *, const char *,...)
Display a debugging message.
Definition: cpl_error.cpp:747
void CPLPushErrorHandler(CPLErrorHandler)
Push a new CPLError handler.
Definition: cpl_error.cpp:1333
#define CPLE_NoWriteAccess
No write access.
Definition: cpl_error.h:114
#define CPLE_UserInterrupt
User interrupted.
Definition: cpl_error.h:116
void CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list)
Same as CPLError() but with a va_list.
Definition: cpl_error.cpp:346
void CPLQuietErrorHandler(CPLErr, CPLErrorNum, const char *)
Error handler that does not do anything, except for debug messages.
Definition: cpl_error.cpp:1119
#define CPLE_OutOfMemory
Out of memory error.
Definition: cpl_error.h:102
CPLErrorHandler CPLSetErrorHandlerEx(CPLErrorHandler, void *)
Install custom error handle with user's data.
Definition: cpl_error.cpp:1239
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:1478
void CPLError(CPLErr eErrClass, CPLErrorNum err_no, const char *fmt,...)
Report an error.
Definition: cpl_error.cpp:330
Core portability definitions for CPL.
#define CPL_NO_RETURN
Qualifier for a function that does not return at all (terminates the process)
Definition: cpl_port.h:995
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:299
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:295
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition: cpl_port.h:966
#define CPL_RETURNS_NONNULL
Qualifier for a function that does not return NULL.
Definition: cpl_port.h:1014
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:177
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:950