/src/gdal/port/cpl_error.h
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * Name: cpl_error.h |
4 | | * Project: CPL - Common Portability Library |
5 | | * Purpose: CPL Error handling |
6 | | * Author: Daniel Morissette, danmo@videotron.ca |
7 | | * |
8 | | ********************************************************************** |
9 | | * Copyright (c) 1998, Daniel Morissette |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #ifndef CPL_ERROR_H_INCLUDED |
15 | | #define CPL_ERROR_H_INCLUDED |
16 | | |
17 | | #include "cpl_port.h" |
18 | | |
19 | | #include <stdarg.h> |
20 | | #include <stdbool.h> |
21 | | #include <stddef.h> |
22 | | |
23 | | /*===================================================================== |
24 | | Error handling functions (cpl_error.c) |
25 | | =====================================================================*/ |
26 | | |
27 | | /** |
28 | | \file cpl_error.h |
29 | | |
30 | | CPL error handling services. |
31 | | |
32 | | \verbatim embed:rst |
33 | | See :ref:`error handling <error_handling>` for an introduction. |
34 | | \endverbatim |
35 | | */ |
36 | | |
37 | | CPL_C_START |
38 | | |
39 | | /** Error category / error level. |
40 | | * |
41 | | * Can be used either as return code for a number of functions of the GDAL API, |
42 | | * or as the error level in warning/errors raised by CPLError(). |
43 | | */ |
44 | | typedef enum |
45 | | { |
46 | | /** No error. Only used as the return value of a function */ |
47 | | CE_None = 0, |
48 | | |
49 | | /** Debug message. Emitted through CPLDebug(). */ |
50 | | CE_Debug = 1, |
51 | | |
52 | | /** Non-nominal situation that is worth bringing to the attention of the |
53 | | * user, but that does not prevent the ongoing operation to complete. |
54 | | */ |
55 | | CE_Warning = 2, |
56 | | |
57 | | /** Error that prevents the current operation to succeed. Other following |
58 | | * GDAL operations might succeed. |
59 | | */ |
60 | | CE_Failure = 3, |
61 | | |
62 | | /** Fatal unrecoverable error. The process is terminated with |
63 | | * abort() after it is emitted. |
64 | | */ |
65 | | CE_Fatal = 4 |
66 | | } CPLErr; |
67 | | |
68 | | /* ==================================================================== */ |
69 | | /* Well known error codes. */ |
70 | | /* ==================================================================== */ |
71 | | |
72 | | #ifdef STRICT_CPLERRORNUM_TYPE |
73 | | |
74 | | /* This is not appropriate for the general case, as there are parts */ |
75 | | /* of GDAL which use custom error codes, but this can help diagnose confusions |
76 | | */ |
77 | | /* between CPLErr and CPLErrorNum */ |
78 | | typedef enum |
79 | | { |
80 | | CPLE_None, |
81 | | CPLE_AppDefined, |
82 | | CPLE_OutOfMemory, |
83 | | CPLE_FileIO, |
84 | | CPLE_OpenFailed, |
85 | | CPLE_IllegalArg, |
86 | | CPLE_NotSupported, |
87 | | CPLE_AssertionFailed, |
88 | | CPLE_NoWriteAccess, |
89 | | CPLE_UserInterrupt, |
90 | | CPLE_ObjectNull, |
91 | | CPLE_HttpResponse, |
92 | | CPLE_BucketNotFound, |
93 | | CPLE_ObjectNotFound, |
94 | | CPLE_AccessDenied, |
95 | | CPLE_InvalidCredentials, |
96 | | CPLE_SignatureDoesNotMatch, |
97 | | CPLE_ObjectStorageGenericError, |
98 | | } CPLErrorNum; |
99 | | |
100 | | #else |
101 | | |
102 | | /** Error number */ |
103 | | typedef int CPLErrorNum; |
104 | | |
105 | | /** No error. Category used by CPLDebug() */ |
106 | 770 | #define CPLE_None 0 |
107 | | /** Application defined error */ |
108 | 6.44k | #define CPLE_AppDefined 1 |
109 | | /** Out of memory error */ |
110 | 0 | #define CPLE_OutOfMemory 2 |
111 | | /** File I/O error */ |
112 | 0 | #define CPLE_FileIO 3 |
113 | | /** Open failed */ |
114 | 0 | #define CPLE_OpenFailed 4 |
115 | | /** Illegal argument */ |
116 | 535 | #define CPLE_IllegalArg 5 |
117 | | /** Not supported */ |
118 | 10.5k | #define CPLE_NotSupported 6 |
119 | | /** Assertion failed */ |
120 | 0 | #define CPLE_AssertionFailed 7 |
121 | | /** No write access */ |
122 | 0 | #define CPLE_NoWriteAccess 8 |
123 | | /** User interrupted */ |
124 | 0 | #define CPLE_UserInterrupt 9 |
125 | | /** NULL object */ |
126 | 0 | #define CPLE_ObjectNull 10 |
127 | | |
128 | | /* |
129 | | * Filesystem-specific errors |
130 | | */ |
131 | | /** HTTP response */ |
132 | 0 | #define CPLE_HttpResponse 11 |
133 | | /** VSIE_BucketNotFound */ |
134 | 0 | #define CPLE_BucketNotFound 12 |
135 | | /** VSIE_ObjectNotFound */ |
136 | 0 | #define CPLE_ObjectNotFound 13 |
137 | | /** VSIE_AccessDenied */ |
138 | 0 | #define CPLE_AccessDenied 14 |
139 | | /** VSIE_InvalidCredentials */ |
140 | 0 | #define CPLE_InvalidCredentials 15 |
141 | | /** VSIE_SignatureDoesNotMatch */ |
142 | 0 | #define CPLE_SignatureDoesNotMatch 16 |
143 | | /** VSIE_ObjectStorageGenericError */ |
144 | 0 | #define CPLE_ObjectStorageGenericError 17 |
145 | | |
146 | | /* 100 - 299 reserved for GDAL */ |
147 | | |
148 | | #endif |
149 | | |
150 | | /** Deprecated alias for CPLE_BucketNotFound |
151 | | * |
152 | | * @deprecated since 3.12 |
153 | | */ |
154 | | #define CPLE_AWSBucketNotFound CPLE_BucketNotFound |
155 | | |
156 | | /** Deprecated alias for CPLE_ObjectNotFound |
157 | | * |
158 | | * @deprecated since 3.12 |
159 | | */ |
160 | | #define CPLE_AWSObjectNotFound CPLE_ObjectNotFound |
161 | | |
162 | | /** Deprecated alias for CPLE_AccessDenied |
163 | | * |
164 | | * @deprecated since 3.12 |
165 | | */ |
166 | | #define CPLE_AWSAccessDenied CPLE_AccessDenied |
167 | | |
168 | | /** Deprecated alias for CPLE_AWSInvalidCredentials |
169 | | * |
170 | | * @deprecated since 3.12 |
171 | | */ |
172 | | #define CPLE_AWSInvalidCredentials CPLE_InvalidCredentials |
173 | | |
174 | | /** Deprecated alias for CPLE_SignatureDoesNotMatch |
175 | | * |
176 | | * @deprecated since 3.12 |
177 | | */ |
178 | | #define CPLE_AWSSignatureDoesNotMatch CPLE_SignatureDoesNotMatch |
179 | | |
180 | | /** Deprecated alias for CPLE_ObjectStorageGenericError |
181 | | * |
182 | | * @deprecated since 3.12 |
183 | | */ |
184 | | #define CPLE_AWSError CPLE_ObjectStorageGenericError |
185 | | |
186 | | void CPL_DLL CPLError(CPLErr eErrClass, CPLErrorNum err_no, |
187 | | CPL_FORMAT_STRING(const char *fmt), ...) |
188 | | CPL_PRINT_FUNC_FORMAT(3, 4); |
189 | | |
190 | | #ifdef GDAL_COMPILATION |
191 | | |
192 | | const char CPL_DLL *CPLSPrintf(CPL_FORMAT_STRING(const char *fmt), ...) |
193 | | CPL_PRINT_FUNC_FORMAT(1, 2) CPL_WARN_UNUSED_RESULT; |
194 | | |
195 | | /** Similar to CPLError(), but only execute it once during the life-time |
196 | | * of a process. |
197 | | * |
198 | | * @since 3.11 |
199 | | */ |
200 | | #define CPLErrorOnce(eErrClass, err_no, ...) \ |
201 | 0 | do \ |
202 | 0 | { \ |
203 | 0 | static bool lbCPLErrorOnce = false; \ |
204 | 0 | if (!lbCPLErrorOnce) \ |
205 | 0 | { \ |
206 | 0 | lbCPLErrorOnce = true; \ |
207 | 0 | const char *lCPLErrorMsg = CPLSPrintf(__VA_ARGS__); \ |
208 | 0 | const size_t lCPLErrorMsgLen = strlen(lCPLErrorMsg); \ |
209 | 0 | const char *lCPLErrorMsgSuffix = \ |
210 | 0 | " Further messages of this type will be suppressed."; \ |
211 | 0 | if (lCPLErrorMsgLen && lCPLErrorMsg[lCPLErrorMsgLen - 1] == '.') \ |
212 | 0 | CPLError((eErrClass), (err_no), "%s%s", lCPLErrorMsg, \ |
213 | 0 | lCPLErrorMsgSuffix); \ |
214 | 0 | else \ |
215 | 0 | CPLError((eErrClass), (err_no), "%s.%s", lCPLErrorMsg, \ |
216 | 0 | lCPLErrorMsgSuffix); \ |
217 | 0 | } \ |
218 | 0 | } while (0) |
219 | | #endif |
220 | | |
221 | | void CPL_DLL CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list); |
222 | | void CPL_DLL CPLEmergencyError(const char *) CPL_NO_RETURN; |
223 | | void CPL_DLL CPL_STDCALL CPLErrorReset(void); |
224 | | CPLErrorNum CPL_DLL CPL_STDCALL CPLGetLastErrorNo(void); |
225 | | CPLErr CPL_DLL CPL_STDCALL CPLGetLastErrorType(void); |
226 | | const char CPL_DLL *CPL_STDCALL CPLGetLastErrorMsg(void); |
227 | | GUInt32 CPL_DLL CPL_STDCALL CPLGetErrorCounter(void); |
228 | | void CPL_DLL *CPL_STDCALL CPLGetErrorHandlerUserData(void); |
229 | | void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, |
230 | | const char *pszMsg); |
231 | | #if defined(GDAL_COMPILATION) && defined(__cplusplus) |
232 | | extern "C++" |
233 | | { |
234 | | void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, |
235 | | const char *pszMsg, |
236 | | const GUInt32 *pnErrorCounter); |
237 | | } |
238 | | #endif |
239 | | |
240 | | void CPL_DLL CPLCallPreviousHandler(CPLErr eErrClass, CPLErrorNum err_no, |
241 | | const char *pszMsg); |
242 | | /*! @cond Doxygen_Suppress */ |
243 | | void CPL_DLL CPLCleanupErrorMutex(void); |
244 | | /*! @endcond */ |
245 | | |
246 | | /** Callback for a custom error handler */ |
247 | | typedef void(CPL_STDCALL *CPLErrorHandler)(CPLErr, CPLErrorNum, const char *); |
248 | | |
249 | | void CPL_DLL CPL_STDCALL CPLLoggingErrorHandler(CPLErr, CPLErrorNum, |
250 | | const char *); |
251 | | void CPL_DLL CPL_STDCALL CPLDefaultErrorHandler(CPLErr, CPLErrorNum, |
252 | | const char *); |
253 | | void CPL_DLL CPL_STDCALL CPLQuietErrorHandler(CPLErr, CPLErrorNum, |
254 | | const char *); |
255 | | void CPL_DLL CPL_STDCALL CPLQuietWarningsErrorHandler(CPLErr, CPLErrorNum, |
256 | | const char *); |
257 | | void CPL_DLL CPLTurnFailureIntoWarning(int bOn); |
258 | | |
259 | | CPLErrorHandler CPL_DLL CPLGetErrorHandler(void **ppUserData); |
260 | | |
261 | | CPLErrorHandler CPL_DLL CPL_STDCALL CPLSetErrorHandler(CPLErrorHandler); |
262 | | CPLErrorHandler CPL_DLL CPL_STDCALL CPLSetErrorHandlerEx(CPLErrorHandler, |
263 | | void *); |
264 | | void CPL_DLL CPL_STDCALL CPLPushErrorHandler(CPLErrorHandler); |
265 | | void CPL_DLL CPL_STDCALL CPLPushErrorHandlerEx(CPLErrorHandler, void *); |
266 | | void CPL_DLL CPL_STDCALL CPLSetCurrentErrorHandlerCatchDebug(int bCatchDebug); |
267 | | void CPL_DLL CPL_STDCALL CPLPopErrorHandler(void); |
268 | | |
269 | | #ifdef WITHOUT_CPLDEBUG |
270 | | #define CPLDebug(...) \ |
271 | | do \ |
272 | | { \ |
273 | | } while (0) /* Eat all CPLDebug calls. */ |
274 | | #define CPLDebugProgress(...) \ |
275 | | do \ |
276 | | { \ |
277 | | } while (0) /* Eat all CPLDebugProgress calls. */ |
278 | | |
279 | | #ifdef GDAL_COMPILATION |
280 | | /** Similar to CPLDebug(), but only execute it once during the life-time |
281 | | * of a process. |
282 | | * |
283 | | * @since 3.11 |
284 | | */ |
285 | | #define CPLDebugOnce(...) \ |
286 | | do \ |
287 | | { \ |
288 | | } while (0) |
289 | | #endif |
290 | | |
291 | | #else |
292 | | void CPL_DLL CPLDebug(const char *, CPL_FORMAT_STRING(const char *), ...) |
293 | | CPL_PRINT_FUNC_FORMAT(2, 3); |
294 | | void CPL_DLL CPLDebugProgress(const char *, CPL_FORMAT_STRING(const char *), |
295 | | ...) CPL_PRINT_FUNC_FORMAT(2, 3); |
296 | | |
297 | | #ifdef GDAL_COMPILATION |
298 | | /** Similar to CPLDebug(), but only execute it once during the life-time |
299 | | * of a process. |
300 | | * |
301 | | * @since 3.11 |
302 | | */ |
303 | | #define CPLDebugOnce(category, ...) \ |
304 | 0 | do \ |
305 | 0 | { \ |
306 | 0 | static bool lbCPLDebugOnce = false; \ |
307 | 0 | if (!lbCPLDebugOnce) \ |
308 | 0 | { \ |
309 | 0 | lbCPLDebugOnce = true; \ |
310 | 0 | const char *lCPLDebugMsg = CPLSPrintf(__VA_ARGS__); \ |
311 | 0 | const size_t lCPLErrorMsgLen = strlen(lCPLDebugMsg); \ |
312 | 0 | const char *lCPLDebugMsgSuffix = \ |
313 | 0 | " Further messages of this type will be suppressed."; \ |
314 | 0 | if (lCPLErrorMsgLen && lCPLDebugMsg[lCPLErrorMsgLen - 1] == '.') \ |
315 | 0 | CPLDebug((category), "%s%s", lCPLDebugMsg, \ |
316 | 0 | lCPLDebugMsgSuffix); \ |
317 | 0 | else \ |
318 | 0 | CPLDebug((category), "%s.%s", lCPLDebugMsg, \ |
319 | 0 | lCPLDebugMsgSuffix); \ |
320 | 0 | } \ |
321 | 0 | } while (0) |
322 | | #endif |
323 | | |
324 | | #endif |
325 | | |
326 | | #if defined(DEBUG) || defined(GDAL_DEBUG) |
327 | | /** Same as CPLDebug(), but expands to nothing for non-DEBUG builds. |
328 | | * @since GDAL 3.1 |
329 | | */ |
330 | 0 | #define CPLDebugOnly(...) CPLDebug(__VA_ARGS__) |
331 | | #else |
332 | | /** Same as CPLDebug(), but expands to nothing for non-DEBUG builds. |
333 | | * @since GDAL 3.1 |
334 | | */ |
335 | | #define CPLDebugOnly(...) \ |
336 | | do \ |
337 | | { \ |
338 | | } while (0) |
339 | | #endif |
340 | | |
341 | | void CPL_DLL CPL_STDCALL _CPLAssert(const char *, const char *, |
342 | | int) CPL_NO_RETURN; |
343 | | |
344 | | #if defined(DEBUG) && !defined(CPPCHECK) |
345 | | /** Assert on an expression. Only enabled in DEBUG mode */ |
346 | | #define CPLAssert(expr) \ |
347 | 597k | ((expr) ? (void)(0) : _CPLAssert(#expr, __FILE__, __LINE__)) |
348 | | /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode |
349 | | * (useful to 'consume' a error return variable) */ |
350 | 0 | #define CPLAssertAlwaysEval(expr) CPLAssert(expr) |
351 | | #else |
352 | | /** Assert on an expression. Only enabled in DEBUG mode */ |
353 | | #define CPLAssert(expr) \ |
354 | | do \ |
355 | | { \ |
356 | | } while (0) |
357 | | #ifdef __cplusplus |
358 | | /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode |
359 | | * (useful to 'consume' a error return variable) */ |
360 | | #define CPLAssertAlwaysEval(expr) CPL_IGNORE_RET_VAL(expr) |
361 | | #else |
362 | | /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode |
363 | | * (useful to 'consume' a error return variable) */ |
364 | | #define CPLAssertAlwaysEval(expr) (void)(expr) |
365 | | #endif |
366 | | #endif |
367 | | |
368 | | CPL_C_END |
369 | | |
370 | | /*! @cond Doxygen_Suppress */ |
371 | | /* |
372 | | * Helper macros used for input parameters validation. |
373 | | */ |
374 | | #ifdef DEBUG |
375 | 0 | #define VALIDATE_POINTER_ERR CE_Fatal |
376 | | #else |
377 | | #define VALIDATE_POINTER_ERR CE_Failure |
378 | | #endif |
379 | | |
380 | | /*! @endcond */ |
381 | | |
382 | | #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) |
383 | | |
384 | | extern "C++" |
385 | | { |
386 | | /*! @cond Doxygen_Suppress */ |
387 | | template <class T> T *CPLAssertNotNull(T *x) CPL_RETURNS_NONNULL; |
388 | | |
389 | | template <class T> T *CPLAssertNotNull(T *x) |
390 | 0 | { |
391 | 0 | CPLAssert(x); |
392 | 0 | return x; |
393 | 0 | } |
394 | | |
395 | | #include <memory> |
396 | | #include <string> |
397 | | |
398 | | /*! @endcond */ |
399 | | |
400 | | /** Class that installs a (thread-local) error handler on construction, and |
401 | | * restore the initial one on destruction. |
402 | | */ |
403 | | class CPL_DLL CPLErrorHandlerPusher |
404 | | { |
405 | | public: |
406 | | /** Constructor that installs a thread-local temporary error handler |
407 | | * (typically CPLQuietErrorHandler) |
408 | | */ |
409 | | explicit CPLErrorHandlerPusher(CPLErrorHandler hHandler) |
410 | 0 | { |
411 | 0 | CPLPushErrorHandler(hHandler); |
412 | 0 | } |
413 | | |
414 | | /** Constructor that installs a thread-local temporary error handler, |
415 | | * and its user data. |
416 | | */ |
417 | | CPLErrorHandlerPusher(CPLErrorHandler hHandler, void *user_data) |
418 | 0 | { |
419 | 0 | CPLPushErrorHandlerEx(hHandler, user_data); |
420 | 0 | } |
421 | | |
422 | | /** Destructor that restores the initial error handler. */ |
423 | | ~CPLErrorHandlerPusher() |
424 | 0 | { |
425 | 0 | CPLPopErrorHandler(); |
426 | 0 | } |
427 | | }; |
428 | | |
429 | | /** Class that saves the error state on construction, and |
430 | | * restores it on destruction. |
431 | | */ |
432 | | class CPL_DLL CPLErrorStateBackuper |
433 | | { |
434 | | CPLErrorNum m_nLastErrorNum; |
435 | | CPLErr m_nLastErrorType; |
436 | | std::string m_osLastErrorMsg; |
437 | | GUInt32 m_nLastErrorCounter; |
438 | | std::unique_ptr<CPLErrorHandlerPusher> m_poErrorHandlerPusher; |
439 | | |
440 | | public: |
441 | | /** Constructor that backs up the error state, and optionally installs |
442 | | * a thread-local temporary error handler (typically CPLQuietErrorHandler). |
443 | | */ |
444 | | explicit CPLErrorStateBackuper(CPLErrorHandler hHandler = nullptr); |
445 | | |
446 | | /** Destructor that restores the error state to its initial state |
447 | | * before construction. |
448 | | */ |
449 | | ~CPLErrorStateBackuper(); |
450 | | }; |
451 | | |
452 | | /** Class that turns errors into warning on construction, and |
453 | | * restores the previous state on destruction. |
454 | | */ |
455 | | class CPL_DLL CPLTurnFailureIntoWarningBackuper |
456 | | { |
457 | | public: |
458 | | CPLTurnFailureIntoWarningBackuper() |
459 | 0 | { |
460 | 0 | CPLTurnFailureIntoWarning(true); |
461 | 0 | } |
462 | | |
463 | | ~CPLTurnFailureIntoWarningBackuper() |
464 | 0 | { |
465 | 0 | CPLTurnFailureIntoWarning(false); |
466 | 0 | } |
467 | | }; |
468 | | } |
469 | | |
470 | | #ifdef GDAL_COMPILATION |
471 | | /*! @cond Doxygen_Suppress */ |
472 | | // internal only |
473 | | bool CPLIsDefaultErrorHandlerAndCatchDebug(); |
474 | | /*! @endcond */ |
475 | | #endif |
476 | | |
477 | | #endif |
478 | | |
479 | | /** Validate that a pointer is not NULL */ |
480 | | #define VALIDATE_POINTER0(ptr, func) \ |
481 | 0 | do \ |
482 | 0 | { \ |
483 | 0 | if (CPL_NULLPTR == ptr) \ |
484 | 0 | { \ |
485 | 0 | CPLErr const ret = VALIDATE_POINTER_ERR; \ |
486 | 0 | CPLError(ret, CPLE_ObjectNull, \ |
487 | 0 | "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func)); \ |
488 | 0 | return; \ |
489 | 0 | } \ |
490 | 0 | } while (0) |
491 | | |
492 | | /** Validate that a pointer is not NULL, and return rc if it is NULL */ |
493 | | #define VALIDATE_POINTER1(ptr, func, rc) \ |
494 | 50.1k | do \ |
495 | 50.1k | { \ |
496 | 50.1k | if (CPL_NULLPTR == ptr) \ |
497 | 50.1k | { \ |
498 | 0 | CPLErr const ret = VALIDATE_POINTER_ERR; \ |
499 | 0 | CPLError(ret, CPLE_ObjectNull, \ |
500 | 0 | "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func)); \ |
501 | 0 | return (rc); \ |
502 | 0 | } \ |
503 | 50.1k | } while (0) |
504 | | |
505 | | #endif /* CPL_ERROR_H_INCLUDED */ |