LCOV - code coverage report
Current view: top level - port - cpl_error.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 15 15 100.0 %
Date: 2026-03-25 02:32:38 Functions: 5 6 83.3 %

          Line data    Source code
       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             : #define CPLE_None 0
     107             : /** Application defined error */
     108             : #define CPLE_AppDefined 1
     109             : /** Out of memory error */
     110             : #define CPLE_OutOfMemory 2
     111             : /** File I/O error */
     112             : #define CPLE_FileIO 3
     113             : /** Open failed */
     114             : #define CPLE_OpenFailed 4
     115             : /** Illegal argument */
     116             : #define CPLE_IllegalArg 5
     117             : /** Not supported */
     118             : #define CPLE_NotSupported 6
     119             : /** Assertion failed */
     120             : #define CPLE_AssertionFailed 7
     121             : /** No write access */
     122             : #define CPLE_NoWriteAccess 8
     123             : /** User interrupted */
     124             : #define CPLE_UserInterrupt 9
     125             : /** NULL object */
     126             : #define CPLE_ObjectNull 10
     127             : 
     128             : /*
     129             :  * Filesystem-specific errors
     130             :  */
     131             : /** HTTP response */
     132             : #define CPLE_HttpResponse 11
     133             : /** VSIE_BucketNotFound */
     134             : #define CPLE_BucketNotFound 12
     135             : /** VSIE_ObjectNotFound */
     136             : #define CPLE_ObjectNotFound 13
     137             : /** VSIE_AccessDenied */
     138             : #define CPLE_AccessDenied 14
     139             : /** VSIE_InvalidCredentials */
     140             : #define CPLE_InvalidCredentials 15
     141             : /** VSIE_SignatureDoesNotMatch */
     142             : #define CPLE_SignatureDoesNotMatch 16
     143             : /** VSIE_ObjectStorageGenericError */
     144             : #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             :     do                                                                         \
     202             :     {                                                                          \
     203             :         static bool lbCPLErrorOnce = false;                                    \
     204             :         if (!lbCPLErrorOnce)                                                   \
     205             :         {                                                                      \
     206             :             lbCPLErrorOnce = true;                                             \
     207             :             const char *lCPLErrorMsg = CPLSPrintf(__VA_ARGS__);                \
     208             :             const size_t lCPLErrorMsgLen = strlen(lCPLErrorMsg);               \
     209             :             const char *lCPLErrorMsgSuffix =                                   \
     210             :                 " Further messages of this type will be suppressed.";          \
     211             :             if (lCPLErrorMsgLen && lCPLErrorMsg[lCPLErrorMsgLen - 1] == '.')   \
     212             :                 CPLError((eErrClass), (err_no), "%s%s", lCPLErrorMsg,          \
     213             :                          lCPLErrorMsgSuffix);                                  \
     214             :             else                                                               \
     215             :                 CPLError((eErrClass), (err_no), "%s.%s", lCPLErrorMsg,         \
     216             :                          lCPLErrorMsgSuffix);                                  \
     217             :         }                                                                      \
     218             :     } 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             :     do                                                                         \
     305             :     {                                                                          \
     306             :         static bool lbCPLDebugOnce = false;                                    \
     307             :         if (!lbCPLDebugOnce)                                                   \
     308             :         {                                                                      \
     309             :             lbCPLDebugOnce = true;                                             \
     310             :             const char *lCPLDebugMsg = CPLSPrintf(__VA_ARGS__);                \
     311             :             const size_t lCPLErrorMsgLen = strlen(lCPLDebugMsg);               \
     312             :             const char *lCPLDebugMsgSuffix =                                   \
     313             :                 " Further messages of this type will be suppressed.";          \
     314             :             if (lCPLErrorMsgLen && lCPLDebugMsg[lCPLErrorMsgLen - 1] == '.')   \
     315             :                 CPLDebug((category), "%s%s", lCPLDebugMsg,                     \
     316             :                          lCPLDebugMsgSuffix);                                  \
     317             :             else                                                               \
     318             :                 CPLDebug((category), "%s.%s", lCPLDebugMsg,                    \
     319             :                          lCPLDebugMsgSuffix);                                  \
     320             :         }                                                                      \
     321             :     } 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             : #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             :     ((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             : #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             : #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           3 :     template <class T> T *CPLAssertNotNull(T *x)
     390             :     {
     391           3 :         CPLAssert(x);
     392           3 :         return x;
     393             :     }
     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      127960 :         explicit CPLErrorHandlerPusher(CPLErrorHandler hHandler)
     410             :         {
     411      127960 :             CPLPushErrorHandler(hHandler);
     412      127958 :         }
     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             :         {
     419             :             CPLPushErrorHandlerEx(hHandler, user_data);
     420             :         }
     421             : 
     422             :         /** Destructor that restores the initial error handler. */
     423      127944 :         ~CPLErrorHandlerPusher()
     424             :         {
     425      127944 :             CPLPopErrorHandler();
     426      127934 :         }
     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        5612 :         CPLTurnFailureIntoWarningBackuper()
     459             :         {
     460        5612 :             CPLTurnFailureIntoWarning(true);
     461        5612 :         }
     462             : 
     463        5612 :         ~CPLTurnFailureIntoWarningBackuper()
     464             :         {
     465        5612 :             CPLTurnFailureIntoWarning(false);
     466        5612 :         }
     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             :     do                                                                         \
     482             :     {                                                                          \
     483             :         if (CPL_NULLPTR == ptr)                                                \
     484             :         {                                                                      \
     485             :             CPLErr const ret = VALIDATE_POINTER_ERR;                           \
     486             :             CPLError(ret, CPLE_ObjectNull,                                     \
     487             :                      "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func));     \
     488             :             return;                                                            \
     489             :         }                                                                      \
     490             :     } 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             :     do                                                                         \
     495             :     {                                                                          \
     496             :         if (CPL_NULLPTR == ptr)                                                \
     497             :         {                                                                      \
     498             :             CPLErr const ret = VALIDATE_POINTER_ERR;                           \
     499             :             CPLError(ret, CPLE_ObjectNull,                                     \
     500             :                      "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func));     \
     501             :             return (rc);                                                       \
     502             :         }                                                                      \
     503             :     } while (0)
     504             : 
     505             : #endif /* CPL_ERROR_H_INCLUDED */

Generated by: LCOV version 1.14