/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 | 1.18k | #define CPLE_None 0 |
107 | | /** Application defined error */ |
108 | 5.53k | #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 | 0 | #define CPLE_IllegalArg 5 |
117 | | /** Not supported */ |
118 | 0 | #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 | | #elif __cplusplus >= 201703L |
332 | | #define CPLDebugOnly(...) \ |
333 | | do \ |
334 | | { \ |
335 | | if constexpr (false) \ |
336 | | { \ |
337 | | CPLDebug(__VA_ARGS__); \ |
338 | | } \ |
339 | | } while (0) |
340 | | #else |
341 | | /** Same as CPLDebug(), but expands to nothing for non-DEBUG builds. |
342 | | * @since GDAL 3.1 |
343 | | */ |
344 | | #define CPLDebugOnly(...) \ |
345 | | do \ |
346 | | { \ |
347 | | } while (0) |
348 | | #endif |
349 | | |
350 | | void CPL_DLL CPL_STDCALL _CPLAssert(const char *, const char *, |
351 | | int) CPL_NO_RETURN; |
352 | | |
353 | | /** Assert on an expression, in DEBUG or release mode */ |
354 | | #define CPLAssertAlways(expr) \ |
355 | 106k | ((expr) ? (void)(0) : _CPLAssert(#expr, __FILE__, __LINE__)) |
356 | | |
357 | | #if defined(DEBUG) && !defined(CPPCHECK) |
358 | | /** Assert on an expression. Only enabled in DEBUG mode */ |
359 | 66.3k | #define CPLAssert(expr) CPLAssertAlways(expr) |
360 | | |
361 | | /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode |
362 | | * (useful to 'consume' a error return variable) */ |
363 | 0 | #define CPLAssertAlwaysEval(expr) CPLAssert(expr) |
364 | | #else |
365 | | /** Assert on an expression. Only enabled in DEBUG mode */ |
366 | | #define CPLAssert(expr) \ |
367 | | do \ |
368 | | { \ |
369 | | } while (0) |
370 | | #ifdef __cplusplus |
371 | | /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode |
372 | | * (useful to 'consume' a error return variable) */ |
373 | | #define CPLAssertAlwaysEval(expr) CPL_IGNORE_RET_VAL(expr) |
374 | | #else |
375 | | /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode |
376 | | * (useful to 'consume' a error return variable) */ |
377 | | #define CPLAssertAlwaysEval(expr) (void)(expr) |
378 | | #endif |
379 | | #endif |
380 | | |
381 | | CPL_C_END |
382 | | |
383 | | /*! @cond Doxygen_Suppress */ |
384 | | /* |
385 | | * Helper macros used for input parameters validation. |
386 | | */ |
387 | | #ifdef DEBUG |
388 | 0 | #define VALIDATE_POINTER_ERR CE_Fatal |
389 | | #else |
390 | | #define VALIDATE_POINTER_ERR CE_Failure |
391 | | #endif |
392 | | |
393 | | /*! @endcond */ |
394 | | |
395 | | #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) |
396 | | |
397 | | extern "C++" |
398 | | { |
399 | | /*! @cond Doxygen_Suppress */ |
400 | | template <class T> T *CPLAssertNotNull(T *x) CPL_RETURNS_NONNULL; |
401 | | |
402 | | template <class T> T *CPLAssertNotNull(T *x) |
403 | 0 | { |
404 | 0 | CPLAssert(x); |
405 | 0 | return x; |
406 | 0 | } Unexecuted instantiation: OGRGeometry* CPLAssertNotNull<OGRGeometry>(OGRGeometry*) Unexecuted instantiation: GDALAlgorithmArg* CPLAssertNotNull<GDALAlgorithmArg>(GDALAlgorithmArg*) Unexecuted instantiation: GDALDataset* CPLAssertNotNull<GDALDataset>(GDALDataset*) |
407 | | |
408 | | #include <memory> |
409 | | #include <string> |
410 | | |
411 | | /*! @endcond */ |
412 | | |
413 | | /** Class that installs a (thread-local) error handler on construction, and |
414 | | * restore the initial one on destruction. |
415 | | */ |
416 | | class CPL_DLL CPLErrorHandlerPusher |
417 | | { |
418 | | public: |
419 | | /** Constructor that installs a thread-local temporary error handler |
420 | | * (typically CPLQuietErrorHandler) |
421 | | */ |
422 | | explicit CPLErrorHandlerPusher(CPLErrorHandler hHandler) |
423 | 0 | { |
424 | 0 | CPLPushErrorHandler(hHandler); |
425 | 0 | } |
426 | | |
427 | | /** Constructor that installs a thread-local temporary error handler, |
428 | | * and its user data. |
429 | | */ |
430 | | CPLErrorHandlerPusher(CPLErrorHandler hHandler, void *user_data) |
431 | 0 | { |
432 | 0 | CPLPushErrorHandlerEx(hHandler, user_data); |
433 | 0 | } |
434 | | |
435 | | /** Destructor that restores the initial error handler. */ |
436 | | ~CPLErrorHandlerPusher() |
437 | 0 | { |
438 | 0 | CPLPopErrorHandler(); |
439 | 0 | } |
440 | | }; |
441 | | |
442 | | /** Class that saves the error state on construction, and |
443 | | * restores it on destruction. |
444 | | */ |
445 | | class CPL_DLL CPLErrorStateBackuper |
446 | | { |
447 | | CPLErrorNum m_nLastErrorNum; |
448 | | CPLErr m_nLastErrorType; |
449 | | std::string m_osLastErrorMsg; |
450 | | GUInt32 m_nLastErrorCounter; |
451 | | std::unique_ptr<CPLErrorHandlerPusher> m_poErrorHandlerPusher; |
452 | | |
453 | | public: |
454 | | /** Constructor that backs up the error state, and optionally installs |
455 | | * a thread-local temporary error handler (typically CPLQuietErrorHandler). |
456 | | */ |
457 | | explicit CPLErrorStateBackuper(CPLErrorHandler hHandler = nullptr); |
458 | | |
459 | | /** Destructor that restores the error state to its initial state |
460 | | * before construction. |
461 | | */ |
462 | | ~CPLErrorStateBackuper(); |
463 | | }; |
464 | | |
465 | | /** Class that turns errors into warning on construction, and |
466 | | * restores the previous state on destruction. |
467 | | */ |
468 | | class CPL_DLL CPLTurnFailureIntoWarningBackuper |
469 | | { |
470 | | public: |
471 | | CPLTurnFailureIntoWarningBackuper() |
472 | 0 | { |
473 | 0 | CPLTurnFailureIntoWarning(true); |
474 | 0 | } |
475 | | |
476 | | ~CPLTurnFailureIntoWarningBackuper() |
477 | 0 | { |
478 | 0 | CPLTurnFailureIntoWarning(false); |
479 | 0 | } |
480 | | }; |
481 | | } |
482 | | |
483 | | #ifdef GDAL_COMPILATION |
484 | | /*! @cond Doxygen_Suppress */ |
485 | | // internal only |
486 | | bool CPLIsDefaultErrorHandlerAndCatchDebug(); |
487 | | /*! @endcond */ |
488 | | #endif |
489 | | |
490 | | #endif |
491 | | |
492 | | /** Validate that a pointer is not NULL */ |
493 | | #define VALIDATE_POINTER0(ptr, func) \ |
494 | 0 | do \ |
495 | 0 | { \ |
496 | 0 | if (CPL_NULLPTR == ptr) \ |
497 | 0 | { \ |
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; \ |
502 | 0 | } \ |
503 | 0 | } while (0) |
504 | | |
505 | | /** Validate that a pointer is not NULL, and return rc if it is NULL */ |
506 | | #define VALIDATE_POINTER1(ptr, func, rc) \ |
507 | 0 | do \ |
508 | 0 | { \ |
509 | 0 | if (CPL_NULLPTR == ptr) \ |
510 | 0 | { \ |
511 | 0 | CPLErr const ret = VALIDATE_POINTER_ERR; \ |
512 | 0 | CPLError(ret, CPLE_ObjectNull, \ |
513 | 0 | "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func)); \ |
514 | 0 | return (rc); \ |
515 | 0 | } \ |
516 | 0 | } while (0) |
517 | | |
518 | | #endif /* CPL_ERROR_H_INCLUDED */ |