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: 2025-03-28 11:40:40 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             : 
      33             : CPL_C_START
      34             : 
      35             : /** Error category */
      36             : typedef enum
      37             : {
      38             :     CE_None = 0,
      39             :     CE_Debug = 1,
      40             :     CE_Warning = 2,
      41             :     CE_Failure = 3,
      42             :     CE_Fatal = 4
      43             : } CPLErr;
      44             : 
      45             : /* ==================================================================== */
      46             : /*      Well known error codes.                                         */
      47             : /* ==================================================================== */
      48             : 
      49             : #ifdef STRICT_CPLERRORNUM_TYPE
      50             : 
      51             : /* This is not appropriate for the general case, as there are parts */
      52             : /* of GDAL which use custom error codes, but this can help diagnose confusions
      53             :  */
      54             : /* between CPLErr and CPLErrorNum */
      55             : typedef enum
      56             : {
      57             :     CPLE_None,
      58             :     CPLE_AppDefined,
      59             :     CPLE_OutOfMemory,
      60             :     CPLE_FileIO,
      61             :     CPLE_OpenFailed,
      62             :     CPLE_IllegalArg,
      63             :     CPLE_NotSupported,
      64             :     CPLE_AssertionFailed,
      65             :     CPLE_NoWriteAccess,
      66             :     CPLE_UserInterrupt,
      67             :     CPLE_ObjectNull,
      68             :     CPLE_HttpResponse,
      69             :     CPLE_AWSBucketNotFound,
      70             :     CPLE_AWSObjectNotFound,
      71             :     CPLE_AWSAccessDenied,
      72             :     CPLE_AWSInvalidCredentials,
      73             :     CPLE_AWSSignatureDoesNotMatch,
      74             : } CPLErrorNum;
      75             : 
      76             : #else
      77             : 
      78             : /** Error number */
      79             : typedef int CPLErrorNum;
      80             : 
      81             : /** No error */
      82             : #define CPLE_None 0
      83             : /** Application defined error */
      84             : #define CPLE_AppDefined 1
      85             : /** Out of memory error */
      86             : #define CPLE_OutOfMemory 2
      87             : /** File I/O error */
      88             : #define CPLE_FileIO 3
      89             : /** Open failed */
      90             : #define CPLE_OpenFailed 4
      91             : /** Illegal argument */
      92             : #define CPLE_IllegalArg 5
      93             : /** Not supported */
      94             : #define CPLE_NotSupported 6
      95             : /** Assertion failed */
      96             : #define CPLE_AssertionFailed 7
      97             : /** No write access */
      98             : #define CPLE_NoWriteAccess 8
      99             : /** User interrupted */
     100             : #define CPLE_UserInterrupt 9
     101             : /** NULL object */
     102             : #define CPLE_ObjectNull 10
     103             : 
     104             : /*
     105             :  * Filesystem-specific errors
     106             :  */
     107             : /** HTTP response */
     108             : #define CPLE_HttpResponse 11
     109             : /** AWSBucketNotFound */
     110             : #define CPLE_AWSBucketNotFound 12
     111             : /** AWSObjectNotFound */
     112             : #define CPLE_AWSObjectNotFound 13
     113             : /** AWSAccessDenied */
     114             : #define CPLE_AWSAccessDenied 14
     115             : /** AWSInvalidCredentials */
     116             : #define CPLE_AWSInvalidCredentials 15
     117             : /** AWSSignatureDoesNotMatch */
     118             : #define CPLE_AWSSignatureDoesNotMatch 16
     119             : /** VSIE_AWSError */
     120             : #define CPLE_AWSError 17
     121             : 
     122             : /* 100 - 299 reserved for GDAL */
     123             : 
     124             : #endif
     125             : 
     126             : void CPL_DLL CPLError(CPLErr eErrClass, CPLErrorNum err_no,
     127             :                       CPL_FORMAT_STRING(const char *fmt), ...)
     128             :     CPL_PRINT_FUNC_FORMAT(3, 4);
     129             : 
     130             : #ifdef GDAL_COMPILATION
     131             : 
     132             : const char CPL_DLL *CPLSPrintf(CPL_FORMAT_STRING(const char *fmt), ...)
     133             :     CPL_PRINT_FUNC_FORMAT(1, 2) CPL_WARN_UNUSED_RESULT;
     134             : 
     135             : /** Similar to CPLError(), but only execute it once during the life-time
     136             :  * of a process.
     137             :  *
     138             :  * @since 3.11
     139             :  */
     140             : #define CPLErrorOnce(eErrClass, err_no, ...)                                   \
     141             :     do                                                                         \
     142             :     {                                                                          \
     143             :         static bool lbCPLErrorOnce = false;                                    \
     144             :         if (!lbCPLErrorOnce)                                                   \
     145             :         {                                                                      \
     146             :             lbCPLErrorOnce = true;                                             \
     147             :             const char *lCPLErrorMsg = CPLSPrintf(__VA_ARGS__);                \
     148             :             const size_t lCPLErrorMsgLen = strlen(lCPLErrorMsg);               \
     149             :             const char *lCPLErrorMsgSuffix =                                   \
     150             :                 " Further messages of this type will be suppressed.";          \
     151             :             if (lCPLErrorMsgLen && lCPLErrorMsg[lCPLErrorMsgLen - 1] == '.')   \
     152             :                 CPLError((eErrClass), (err_no), "%s%s", lCPLErrorMsg,          \
     153             :                          lCPLErrorMsgSuffix);                                  \
     154             :             else                                                               \
     155             :                 CPLError((eErrClass), (err_no), "%s.%s", lCPLErrorMsg,         \
     156             :                          lCPLErrorMsgSuffix);                                  \
     157             :         }                                                                      \
     158             :     } while (0)
     159             : #endif
     160             : 
     161             : void CPL_DLL CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list);
     162             : void CPL_DLL CPLEmergencyError(const char *) CPL_NO_RETURN;
     163             : void CPL_DLL CPL_STDCALL CPLErrorReset(void);
     164             : CPLErrorNum CPL_DLL CPL_STDCALL CPLGetLastErrorNo(void);
     165             : CPLErr CPL_DLL CPL_STDCALL CPLGetLastErrorType(void);
     166             : const char CPL_DLL *CPL_STDCALL CPLGetLastErrorMsg(void);
     167             : GUInt32 CPL_DLL CPL_STDCALL CPLGetErrorCounter(void);
     168             : void CPL_DLL *CPL_STDCALL CPLGetErrorHandlerUserData(void);
     169             : void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no,
     170             :                               const char *pszMsg);
     171             : void CPL_DLL CPLCallPreviousHandler(CPLErr eErrClass, CPLErrorNum err_no,
     172             :                                     const char *pszMsg);
     173             : /*! @cond Doxygen_Suppress */
     174             : void CPL_DLL CPLCleanupErrorMutex(void);
     175             : /*! @endcond */
     176             : 
     177             : /** Callback for a custom error handler */
     178             : typedef void(CPL_STDCALL *CPLErrorHandler)(CPLErr, CPLErrorNum, const char *);
     179             : 
     180             : void CPL_DLL CPL_STDCALL CPLLoggingErrorHandler(CPLErr, CPLErrorNum,
     181             :                                                 const char *);
     182             : void CPL_DLL CPL_STDCALL CPLDefaultErrorHandler(CPLErr, CPLErrorNum,
     183             :                                                 const char *);
     184             : void CPL_DLL CPL_STDCALL CPLQuietErrorHandler(CPLErr, CPLErrorNum,
     185             :                                               const char *);
     186             : void CPL_DLL CPL_STDCALL CPLQuietWarningsErrorHandler(CPLErr, CPLErrorNum,
     187             :                                                       const char *);
     188             : void CPL_DLL CPLTurnFailureIntoWarning(int bOn);
     189             : 
     190             : CPLErrorHandler CPL_DLL CPLGetErrorHandler(void **ppUserData);
     191             : 
     192             : CPLErrorHandler CPL_DLL CPL_STDCALL CPLSetErrorHandler(CPLErrorHandler);
     193             : CPLErrorHandler CPL_DLL CPL_STDCALL CPLSetErrorHandlerEx(CPLErrorHandler,
     194             :                                                          void *);
     195             : void CPL_DLL CPL_STDCALL CPLPushErrorHandler(CPLErrorHandler);
     196             : void CPL_DLL CPL_STDCALL CPLPushErrorHandlerEx(CPLErrorHandler, void *);
     197             : void CPL_DLL CPL_STDCALL CPLSetCurrentErrorHandlerCatchDebug(int bCatchDebug);
     198             : void CPL_DLL CPL_STDCALL CPLPopErrorHandler(void);
     199             : 
     200             : #ifdef WITHOUT_CPLDEBUG
     201             : #define CPLDebug(...)                                                          \
     202             :     do                                                                         \
     203             :     {                                                                          \
     204             :     } while (0) /* Eat all CPLDebug calls. */
     205             : #define CPLDebugProgress(...)                                                  \
     206             :     do                                                                         \
     207             :     {                                                                          \
     208             :     } while (0) /* Eat all CPLDebugProgress calls. */
     209             : 
     210             : #ifdef GDAL_COMPILATION
     211             : /** Similar to CPLDebug(), but only execute it once during the life-time
     212             :  * of a process.
     213             :  *
     214             :  * @since 3.11
     215             :  */
     216             : #define CPLDebugOnce(...)                                                      \
     217             :     do                                                                         \
     218             :     {                                                                          \
     219             :     } while (0)
     220             : #endif
     221             : 
     222             : #else
     223             : void CPL_DLL CPLDebug(const char *, CPL_FORMAT_STRING(const char *), ...)
     224             :     CPL_PRINT_FUNC_FORMAT(2, 3);
     225             : void CPL_DLL CPLDebugProgress(const char *, CPL_FORMAT_STRING(const char *),
     226             :                               ...) CPL_PRINT_FUNC_FORMAT(2, 3);
     227             : 
     228             : #ifdef GDAL_COMPILATION
     229             : /** Similar to CPLDebug(), but only execute it once during the life-time
     230             :  * of a process.
     231             :  *
     232             :  * @since 3.11
     233             :  */
     234             : #define CPLDebugOnce(category, ...)                                            \
     235             :     do                                                                         \
     236             :     {                                                                          \
     237             :         static bool lbCPLDebugOnce = false;                                    \
     238             :         if (!lbCPLDebugOnce)                                                   \
     239             :         {                                                                      \
     240             :             lbCPLDebugOnce = true;                                             \
     241             :             const char *lCPLDebugMsg = CPLSPrintf(__VA_ARGS__);                \
     242             :             const size_t lCPLErrorMsgLen = strlen(lCPLDebugMsg);               \
     243             :             const char *lCPLDebugMsgSuffix =                                   \
     244             :                 " Further messages of this type will be suppressed.";          \
     245             :             if (lCPLErrorMsgLen && lCPLDebugMsg[lCPLErrorMsgLen - 1] == '.')   \
     246             :                 CPLDebug((category), "%s%s", lCPLDebugMsg,                     \
     247             :                          lCPLDebugMsgSuffix);                                  \
     248             :             else                                                               \
     249             :                 CPLDebug((category), "%s.%s", lCPLDebugMsg,                    \
     250             :                          lCPLDebugMsgSuffix);                                  \
     251             :         }                                                                      \
     252             :     } while (0)
     253             : #endif
     254             : 
     255             : #endif
     256             : 
     257             : #if defined(DEBUG) || defined(GDAL_DEBUG)
     258             : /** Same as CPLDebug(), but expands to nothing for non-DEBUG builds.
     259             :  * @since GDAL 3.1
     260             :  */
     261             : #define CPLDebugOnly(...) CPLDebug(__VA_ARGS__)
     262             : #else
     263             : /** Same as CPLDebug(), but expands to nothing for non-DEBUG builds.
     264             :  * @since GDAL 3.1
     265             :  */
     266             : #define CPLDebugOnly(...)                                                      \
     267             :     do                                                                         \
     268             :     {                                                                          \
     269             :     } while (0)
     270             : #endif
     271             : 
     272             : void CPL_DLL CPL_STDCALL _CPLAssert(const char *, const char *,
     273             :                                     int) CPL_NO_RETURN;
     274             : 
     275             : #if defined(DEBUG) && !defined(CPPCHECK)
     276             : /** Assert on an expression. Only enabled in DEBUG mode */
     277             : #define CPLAssert(expr)                                                        \
     278             :     ((expr) ? (void)(0) : _CPLAssert(#expr, __FILE__, __LINE__))
     279             : /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode
     280             :  * (useful to 'consume' a error return variable) */
     281             : #define CPLAssertAlwaysEval(expr) CPLAssert(expr)
     282             : #else
     283             : /** Assert on an expression. Only enabled in DEBUG mode */
     284             : #define CPLAssert(expr)                                                        \
     285             :     do                                                                         \
     286             :     {                                                                          \
     287             :     } while (0)
     288             : #ifdef __cplusplus
     289             : /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode
     290             :  * (useful to 'consume' a error return variable) */
     291             : #define CPLAssertAlwaysEval(expr) CPL_IGNORE_RET_VAL(expr)
     292             : #else
     293             : /** Assert on an expression in DEBUG mode. Evaluate it also in non-DEBUG mode
     294             :  * (useful to 'consume' a error return variable) */
     295             : #define CPLAssertAlwaysEval(expr) (void)(expr)
     296             : #endif
     297             : #endif
     298             : 
     299             : CPL_C_END
     300             : 
     301             : /*! @cond Doxygen_Suppress */
     302             : /*
     303             :  * Helper macros used for input parameters validation.
     304             :  */
     305             : #ifdef DEBUG
     306             : #define VALIDATE_POINTER_ERR CE_Fatal
     307             : #else
     308             : #define VALIDATE_POINTER_ERR CE_Failure
     309             : #endif
     310             : 
     311             : /*! @endcond */
     312             : 
     313             : #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
     314             : 
     315             : extern "C++"
     316             : {
     317             :     /*! @cond Doxygen_Suppress */
     318             :     template <class T> T *CPLAssertNotNull(T *x) CPL_RETURNS_NONNULL;
     319             : 
     320           3 :     template <class T> T *CPLAssertNotNull(T *x)
     321             :     {
     322           3 :         CPLAssert(x);
     323           3 :         return x;
     324             :     }
     325             : 
     326             : #include <memory>
     327             : #include <string>
     328             : 
     329             :     /*! @endcond */
     330             : 
     331             :     /** Class that installs a (thread-local) error handler on construction, and
     332             :      * restore the initial one on destruction.
     333             :      */
     334             :     class CPL_DLL CPLErrorHandlerPusher
     335             :     {
     336             :       public:
     337             :         /** Constructor that installs a thread-local temporary error handler
     338             :          * (typically CPLQuietErrorHandler)
     339             :          */
     340       81615 :         explicit CPLErrorHandlerPusher(CPLErrorHandler hHandler)
     341             :         {
     342       81615 :             CPLPushErrorHandler(hHandler);
     343       81622 :         }
     344             : 
     345             :         /** Constructor that installs a thread-local temporary error handler,
     346             :          * and its user data.
     347             :          */
     348             :         CPLErrorHandlerPusher(CPLErrorHandler hHandler, void *user_data)
     349             :         {
     350             :             CPLPushErrorHandlerEx(hHandler, user_data);
     351             :         }
     352             : 
     353             :         /** Destructor that restores the initial error handler. */
     354       81615 :         ~CPLErrorHandlerPusher()
     355             :         {
     356       81615 :             CPLPopErrorHandler();
     357       81610 :         }
     358             :     };
     359             : 
     360             :     /** Class that saves the error state on construction, and
     361             :      * restores it on destruction.
     362             :      */
     363             :     class CPL_DLL CPLErrorStateBackuper
     364             :     {
     365             :         CPLErrorNum m_nLastErrorNum;
     366             :         CPLErr m_nLastErrorType;
     367             :         std::string m_osLastErrorMsg;
     368             :         GUInt32 m_nLastErrorCounter;
     369             :         std::unique_ptr<CPLErrorHandlerPusher> m_poErrorHandlerPusher;
     370             : 
     371             :       public:
     372             :         /** Constructor that backs up the error state, and optionally installs
     373             :          * a thread-local temporary error handler (typically CPLQuietErrorHandler).
     374             :          */
     375             :         explicit CPLErrorStateBackuper(CPLErrorHandler hHandler = nullptr);
     376             : 
     377             :         /** Destructor that restores the error state to its initial state
     378             :          * before construction.
     379             :          */
     380             :         ~CPLErrorStateBackuper();
     381             :     };
     382             : 
     383             :     /** Class that turns errors into warning on construction, and
     384             :      *  restores the previous state on destruction.
     385             :      */
     386             :     class CPL_DLL CPLTurnFailureIntoWarningBackuper
     387             :     {
     388             :       public:
     389        4857 :         CPLTurnFailureIntoWarningBackuper()
     390             :         {
     391        4857 :             CPLTurnFailureIntoWarning(true);
     392        4857 :         }
     393             : 
     394        4857 :         ~CPLTurnFailureIntoWarningBackuper()
     395             :         {
     396        4857 :             CPLTurnFailureIntoWarning(false);
     397        4857 :         }
     398             :     };
     399             : }
     400             : 
     401             : #ifdef GDAL_COMPILATION
     402             : /*! @cond Doxygen_Suppress */
     403             : // internal only
     404             : bool CPLIsDefaultErrorHandlerAndCatchDebug();
     405             : /*! @endcond */
     406             : #endif
     407             : 
     408             : #endif
     409             : 
     410             : /** Validate that a pointer is not NULL */
     411             : #define VALIDATE_POINTER0(ptr, func)                                           \
     412             :     do                                                                         \
     413             :     {                                                                          \
     414             :         if (CPL_NULLPTR == ptr)                                                \
     415             :         {                                                                      \
     416             :             CPLErr const ret = VALIDATE_POINTER_ERR;                           \
     417             :             CPLError(ret, CPLE_ObjectNull,                                     \
     418             :                      "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func));     \
     419             :             return;                                                            \
     420             :         }                                                                      \
     421             :     } while (0)
     422             : 
     423             : /** Validate that a pointer is not NULL, and return rc if it is NULL */
     424             : #define VALIDATE_POINTER1(ptr, func, rc)                                       \
     425             :     do                                                                         \
     426             :     {                                                                          \
     427             :         if (CPL_NULLPTR == ptr)                                                \
     428             :         {                                                                      \
     429             :             CPLErr const ret = VALIDATE_POINTER_ERR;                           \
     430             :             CPLError(ret, CPLE_ObjectNull,                                     \
     431             :                      "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func));     \
     432             :             return (rc);                                                       \
     433             :         }                                                                      \
     434             :     } while (0)
     435             : 
     436             : #endif /* CPL_ERROR_H_INCLUDED */

Generated by: LCOV version 1.14