From be71b41513f2ce258f9e4e48cbd5332b2dd63c00 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sat, 6 Apr 2024 21:46:13 +0200 Subject: [PATCH 1/2] CPLErrorStateBackuper: restore error count, and allow specifying the error handler as CPLQuietErrorHandler is used > 95% of time combined with it --- port/cpl_error.cpp | 55 ++++++++++++++++++++++++++++++++++++---------- port/cpl_error.h | 53 +++++++++++++++++++++++++++++--------------- 2 files changed, 79 insertions(+), 29 deletions(-) diff --git a/port/cpl_error.cpp b/port/cpl_error.cpp index fd522b8338d9..48b18ee49b56 100644 --- a/port/cpl_error.cpp +++ b/port/cpl_error.cpp @@ -845,17 +845,8 @@ void CPL_STDCALL CPLErrorReset() * CPLErrorSetState() **********************************************************************/ -/** - * Restore an error state, without emitting an error. - * - * Can be useful if a routine might call CPLErrorReset() and one wants to - * preserve the previous error state. - * - * @since GDAL 2.0 - */ - -void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, - const char *pszMsg) +static void CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, + const char *pszMsg, GUInt32 *pnErrorCounter) { CPLErrorContext *psCtx = CPLGetErrorContext(); if (psCtx == nullptr) @@ -891,6 +882,23 @@ void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, memcpy(pszLastErrMsg, pszMsg, size); pszLastErrMsg[size] = '\0'; psCtx->eLastErrType = eErrClass; + if (pnErrorCounter) + psCtx->nErrorCounter = *pnErrorCounter; +} + +/** + * Restore an error state, without emitting an error. + * + * Can be useful if a routine might call CPLErrorReset() and one wants to + * preserve the previous error state. + * + * @since GDAL 2.0 + */ + +void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, + const char *pszMsg) +{ + CPLErrorSetState(eErrClass, err_no, pszMsg, nullptr); } /********************************************************************** @@ -1563,3 +1571,28 @@ void CPLUninstallErrorHandlerAccumulator() { CPLPopErrorHandler(); } + +/************************************************************************/ +/* CPLErrorStateBackuper::CPLErrorStateBackuper() */ +/************************************************************************/ + +CPLErrorStateBackuper::CPLErrorStateBackuper(CPLErrorHandler hHandler) + : m_nLastErrorNum(CPLGetLastErrorNo()), + m_nLastErrorType(CPLGetLastErrorType()), + m_osLastErrorMsg(CPLGetLastErrorMsg()), + m_nLastErrorCounter(CPLGetErrorCounter()), + m_poErrorHandlerPusher( + hHandler ? std::make_unique(hHandler) + : nullptr) +{ +} + +/************************************************************************/ +/* CPLErrorStateBackuper::~CPLErrorStateBackuper() */ +/************************************************************************/ + +CPLErrorStateBackuper::~CPLErrorStateBackuper() +{ + CPLErrorSetState(m_nLastErrorType, m_nLastErrorNum, + m_osLastErrorMsg.c_str(), &m_nLastErrorCounter); +} diff --git a/port/cpl_error.h b/port/cpl_error.h index 7bdfbd1b75bc..f9d50539c51b 100644 --- a/port/cpl_error.h +++ b/port/cpl_error.h @@ -237,11 +237,13 @@ CPL_C_END #define VALIDATE_POINTER_ERR CE_Failure #endif -#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && \ - !defined(DOXYGEN_SKIP) +/*! @endcond */ + +#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) extern "C++" { + /*! @cond Doxygen_Suppress */ template T *CPLAssertNotNull(T *x) CPL_RETURNS_NONNULL; template T *CPLAssertNotNull(T *x) @@ -250,58 +252,73 @@ extern "C++" return x; } +#include #include - class CPLErrorHandlerPusher + /*! @endcond */ + + /** Class that installs a (thread-local) error handler on construction, and + * restore the initial one on destruction. + */ + class CPL_DLL CPLErrorHandlerPusher { public: + /** Constructor that installs a thread-local temporary error handler + * (typically CPLQuietErrorHandler) + */ explicit CPLErrorHandlerPusher(CPLErrorHandler hHandler) { CPLPushErrorHandler(hHandler); } + /** Constructor that installs a thread-local temporary error handler, + * and its user data. + */ CPLErrorHandlerPusher(CPLErrorHandler hHandler, void *user_data) { CPLPushErrorHandlerEx(hHandler, user_data); } + /** Destructor that restores the initial error handler. */ ~CPLErrorHandlerPusher() { CPLPopErrorHandler(); } }; - class CPLErrorStateBackuper + /** Class that saves the error state on construction, and + * restores it on destruction. + */ + class CPL_DLL CPLErrorStateBackuper { CPLErrorNum m_nLastErrorNum; CPLErr m_nLastErrorType; std::string m_osLastErrorMsg; + GUInt32 m_nLastErrorCounter; + std::unique_ptr m_poErrorHandlerPusher; public: - CPLErrorStateBackuper() - : m_nLastErrorNum(CPLGetLastErrorNo()), - m_nLastErrorType(CPLGetLastErrorType()), - m_osLastErrorMsg(CPLGetLastErrorMsg()) - { - } - - ~CPLErrorStateBackuper() - { - CPLErrorSetState(m_nLastErrorType, m_nLastErrorNum, - m_osLastErrorMsg.c_str()); - } + /** Constructor that backs up the error state, and optionally installs + * a thread-local temporary error handler (typically CPLQuietErrorHandler). + */ + CPLErrorStateBackuper(CPLErrorHandler hHandler = nullptr); + + /** Destructor that restores the error state to its initial state + * before construction. + */ + ~CPLErrorStateBackuper(); }; } #ifdef GDAL_COMPILATION +/*! @cond Doxygen_Suppress */ // internal only bool CPLIsDefaultErrorHandlerAndCatchDebug(); +/*! @endcond */ #endif #endif -/*! @endcond */ - /** Validate that a pointer is not NULL */ #define VALIDATE_POINTER0(ptr, func) \ do \ From cd6e64e4b5889dc0b10e99d6c71010481aff7f7e Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Sat, 6 Apr 2024 21:46:30 +0200 Subject: [PATCH 2/2] Use combined CPLErrorStateBackuper + CPLQuietErrorHandler --- apps/gdalinfo_lib.cpp | 13 +++++-------- apps/gdalmdimtranslate_lib.cpp | 9 +++------ apps/gdalwarp_lib.cpp | 6 ++---- apps/ogrinfo_lib.cpp | 4 ++-- frmts/gtiff/gtiffdataset_write.cpp | 3 +-- frmts/nitf/nitfdataset.cpp | 6 ++---- frmts/ogcapi/gdalogcapidataset.cpp | 3 +-- frmts/rmf/rmfdataset.cpp | 3 +-- frmts/vrt/vrtsourcedrasterband.cpp | 6 ++---- frmts/zarr/zarr_array.cpp | 3 +-- frmts/zarr/zarr_v2_array.cpp | 3 +-- frmts/zarr/zarr_v2_group.cpp | 3 +-- gcore/gdaldriver.cpp | 6 ++---- gcore/gdaljp2abstractdataset.cpp | 3 +-- gcore/gdaljp2metadata.cpp | 3 +-- gcore/gdalmultidim.cpp | 10 ++++------ gcore/gdalpamdataset.cpp | 12 ++++-------- ogr/ogrct.cpp | 12 ++++-------- .../flatgeobuf/ogrflatgeobuflayer.cpp | 3 +-- ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp | 3 +-- ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp | 6 ++---- ogr/ogrsf_frmts/gpsbabel/ogrgpsbabeldriver.cpp | 3 +-- ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp | 17 ++++++----------- ogr/ogrsf_frmts/oci/ogrocidatasource.cpp | 3 +-- ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp | 3 +-- ogr/ogrsf_frmts/pgeo/ogrpgeodatasource.cpp | 3 +-- ogr/ogrsf_frmts/pmtiles/vsipmtiles.cpp | 6 ++---- ogr/ogrsf_frmts/sqlite/ogrsqliteselectlayer.cpp | 4 ++-- ogr/ogrsf_frmts/wfs/ogroapifdriver.cpp | 3 +-- ogr/ogrsf_frmts/wfs/ogrwfsdatasource.cpp | 4 ++-- ogr/ogrspatialreference.cpp | 3 +-- 31 files changed, 60 insertions(+), 109 deletions(-) diff --git a/apps/gdalinfo_lib.cpp b/apps/gdalinfo_lib.cpp index 38aa60f4faeb..ef98681c0ca0 100644 --- a/apps/gdalinfo_lib.cpp +++ b/apps/gdalinfo_lib.cpp @@ -428,8 +428,8 @@ char *GDALInfo(GDALDatasetH hDataset, const GDALInfoOptions *psOptions) } { // PROJJSON requires PROJ >= 6.2 - CPLErrorHandlerPusher oPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oCPLErrorHandlerPusher; + CPLErrorStateBackuper oCPLErrorHandlerPusher( + CPLQuietErrorHandler); char *pszProjJson = nullptr; OGRErr result = OSRExportToPROJJSON(hSRS, &pszProjJson, nullptr); @@ -721,8 +721,7 @@ char *GDALInfo(GDALDatasetH hDataset, const GDALInfoOptions *psOptions) { // Check that it looks like Earth before trying to reproject to wgs84... // OSRGetSemiMajor() may raise an error on CRS like Engineering CRS - CPLErrorHandlerPusher oPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oCPLErrorHandlerPusher; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); OGRErr eErr = OGRERR_NONE; if (fabs(OSRGetSemiMajor(hProj, &eErr) - 6378137.0) < 10000.0 && eErr == OGRERR_NONE) @@ -759,8 +758,7 @@ char *GDALInfo(GDALDatasetH hDataset, const GDALInfoOptions *psOptions) /* -------------------------------------------------------------------- */ if (bJson && GDALGetRasterXSize(hDataset)) { - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); json_object *poLinearRing = json_object_new_array(); json_object *poCornerCoordinates = json_object_new_object(); @@ -803,8 +801,7 @@ char *GDALInfo(GDALDatasetH hDataset, const GDALInfoOptions *psOptions) } else if (GDALGetRasterXSize(hDataset)) { - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); Concat(osStr, psOptions->bStdoutOutput, "Corner Coordinates:\n"); GDALInfoReportCorner(psOptions, hDataset, hTransform, "Upper Left", 0.0, diff --git a/apps/gdalmdimtranslate_lib.cpp b/apps/gdalmdimtranslate_lib.cpp index b6f6681204d0..10d6364fbc8f 100644 --- a/apps/gdalmdimtranslate_lib.cpp +++ b/apps/gdalmdimtranslate_lib.cpp @@ -904,8 +904,7 @@ static bool TranslateArray( std::shared_ptr dstDim; { - CPLErrorHandlerPusher oHandlerPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); if (!srcDimFullName.empty() && srcDimFullName[0] == '/') { dstDim = @@ -1083,8 +1082,7 @@ static bool TranslateArray( } } - CPLErrorHandlerPusher oHandlerPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); auto poDstIndexingVar(poDstGroup->OpenMDArray(newDimName)); if (poDstIndexingVar) dstDim->SetIndexingVariable(std::move(poDstIndexingVar)); @@ -1319,8 +1317,7 @@ static bool CopyGroup( mapSrcToDstDims.find(oIterDimName->second); if (oCorrespondingDimIter != mapSrcToDstDims.end()) { - CPLErrorHandlerPusher oHandlerPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); oCorrespondingDimIter->second->SetIndexingVariable( std::move(dstArray)); } diff --git a/apps/gdalwarp_lib.cpp b/apps/gdalwarp_lib.cpp index 09e0a00fed50..3c1c78b36ad6 100644 --- a/apps/gdalwarp_lib.cpp +++ b/apps/gdalwarp_lib.cpp @@ -365,8 +365,7 @@ static CPLString GetSrcDSProjection(GDALDatasetH hDS, CSLConstList papszTO) { char *pszWKT = nullptr; { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); if (OSRExportToWkt(hSRS, &pszWKT) != OGRERR_NONE) { CPLFree(pszWKT); @@ -4195,8 +4194,7 @@ static GDALDatasetH GDALWarpCreateOutput( { OGRSpatialReference oSrcSRS; OGRSpatialReference oDstSRS; - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); // DemoteTo2D requires PROJ >= 6.3 if (oSrcSRS.SetFromUserInput(osThisSourceSRS.c_str()) == OGRERR_NONE && diff --git a/apps/ogrinfo_lib.cpp b/apps/ogrinfo_lib.cpp index a78e38e0eb1b..813fe0bfb3c2 100644 --- a/apps/ogrinfo_lib.cpp +++ b/apps/ogrinfo_lib.cpp @@ -957,8 +957,8 @@ static void ReportOnLayer(CPLString &osRet, CPLJSONObject &oLayer, { char *pszProjJson = nullptr; // PROJJSON requires PROJ >= 6.2 - CPLErrorHandlerPusher oPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oCPLErrorHandlerPusher; + CPLErrorStateBackuper oCPLErrorHandlerPusher( + CPLQuietErrorHandler); CPL_IGNORE_RET_VAL( poSRS->exportToPROJJSON(&pszProjJson, nullptr)); if (pszProjJson) diff --git a/frmts/gtiff/gtiffdataset_write.cpp b/frmts/gtiff/gtiffdataset_write.cpp index ed31eb82b28d..d28ead80ca41 100644 --- a/frmts/gtiff/gtiffdataset_write.cpp +++ b/frmts/gtiff/gtiffdataset_write.cpp @@ -3506,8 +3506,7 @@ static bool IsSRSCompatibleOfGeoTIFF(const OGRSpatialReference *poSRS, } OGRErr eErr; { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); if (poSRS->IsDerivedGeographic() || (poSRS->IsProjected() && !poSRS->IsCompound() && poSRS->GetAxesCount() == 3)) diff --git a/frmts/nitf/nitfdataset.cpp b/frmts/nitf/nitfdataset.cpp index a17922108c7b..27901db2bb0d 100644 --- a/frmts/nitf/nitfdataset.cpp +++ b/frmts/nitf/nitfdataset.cpp @@ -1627,8 +1627,7 @@ NITFDataset *NITFDataset::OpenInternal(GDALOpenInfo *poOpenInfo, VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 && VSI_ISREG(sStatBuf.st_mode)) { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); CPLXMLNode *psTree = CPLParseXMLFile(pszPAMFilename); if (psTree) { @@ -1660,8 +1659,7 @@ NITFDataset *NITFDataset::OpenInternal(GDALOpenInfo *poOpenInfo, VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 && VSI_ISREG(sStatBuf.st_mode)) { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); CPLXMLNode *psTree = CPLParseXMLFile(pszPAMFilename); if (psTree) { diff --git a/frmts/ogcapi/gdalogcapidataset.cpp b/frmts/ogcapi/gdalogcapidataset.cpp index fda9057f78e7..c6c489af99db 100644 --- a/frmts/ogcapi/gdalogcapidataset.cpp +++ b/frmts/ogcapi/gdalogcapidataset.cpp @@ -1633,8 +1633,7 @@ ParseXMLSchema(const std::string &osURL, std::vector> &apoFields, OGRwkbGeometryType &eGeomType) { - CPLErrorHandlerPusher oErrorHandlerPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); std::vector apoClasses; bool bFullyUnderstood = false; diff --git a/frmts/rmf/rmfdataset.cpp b/frmts/rmf/rmfdataset.cpp index 57a5624fdddf..26b7321ef5a4 100644 --- a/frmts/rmf/rmfdataset.cpp +++ b/frmts/rmf/rmfdataset.cpp @@ -1197,8 +1197,7 @@ CPLErr RMFDataset::FlushCache(bool bAtClosing) { // ComputeRasterMinMax can setup error in case of dataset full // from NoData values, but it makes no sense here. - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); poBand->ComputeRasterMinMax(FALSE, sHeader.adfElevMinMax); bHeaderDirty = true; } diff --git a/frmts/vrt/vrtsourcedrasterband.cpp b/frmts/vrt/vrtsourcedrasterband.cpp index d5b9122fedb5..8b73263e3dcf 100644 --- a/frmts/vrt/vrtsourcedrasterband.cpp +++ b/frmts/vrt/vrtsourcedrasterband.cpp @@ -1007,8 +1007,7 @@ CPLErr VRTSourcedRasterBand::ComputeRasterMinMax(int bApproxOK, CPLErr eErr; std::string osLastErrorMsg; { - CPLErrorHandlerPusher oPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); CPLErrorReset(); eErr = ComputeStatistics(bApproxOK, &adfMinMax[0], &adfMinMax[1], @@ -1404,8 +1403,7 @@ CPLErr VRTSourcedRasterBand::ComputeStatistics(int bApproxOK, double *pdfMin, static_cast(poSimpleSourceBand->GetXSize()) * poSimpleSourceBand->GetYSize(); - CPLErrorHandlerPusher oPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); CPLErr eErr = poSimpleSourceBand->ComputeStatistics( psContext->bApproxOK, &psJob->dfMin, &psJob->dfMax, &psJob->dfMean, &psJob->dfStdDev, diff --git a/frmts/zarr/zarr_array.cpp b/frmts/zarr/zarr_array.cpp index d5ba467df92f..0d585ba9c4de 100644 --- a/frmts/zarr/zarr_array.cpp +++ b/frmts/zarr/zarr_array.cpp @@ -237,8 +237,7 @@ CPLJSONObject ZarrArray::SerializeSpecialAttributes() CPLFree(pszWKT); { - CPLErrorHandlerPusher quietError(CPLQuietErrorHandler); - CPLErrorStateBackuper errorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); char *projjson = nullptr; if (m_poSRS->exportToPROJJSON(&projjson, nullptr) == OGRERR_NONE && projjson != nullptr) diff --git a/frmts/zarr/zarr_v2_array.cpp b/frmts/zarr/zarr_v2_array.cpp index 0d3df25eb15f..7e7b149acb4b 100644 --- a/frmts/zarr/zarr_v2_array.cpp +++ b/frmts/zarr/zarr_v2_array.cpp @@ -1346,8 +1346,7 @@ ZarrV2Group::LoadArray(const std::string &osArrayName, CPLJSONDocument oDoc; const std::string osZattrsFilename(CPLFormFilename( CPLGetDirname(osZarrayFilename.c_str()), ".zattrs", nullptr)); - CPLErrorHandlerPusher quietError(CPLQuietErrorHandler); - CPLErrorStateBackuper errorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); if (oDoc.Load(osZattrsFilename)) { oAttributes = oDoc.GetRoot(); diff --git a/frmts/zarr/zarr_v2_group.cpp b/frmts/zarr/zarr_v2_group.cpp index 5ab555903d36..04c117935f2b 100644 --- a/frmts/zarr/zarr_v2_group.cpp +++ b/frmts/zarr/zarr_v2_group.cpp @@ -219,8 +219,7 @@ void ZarrV2Group::LoadAttributes() const CPLJSONDocument oDoc; const std::string osZattrsFilename( CPLFormFilename(m_osDirectoryName.c_str(), ".zattrs", nullptr)); - CPLErrorHandlerPusher quietError(CPLQuietErrorHandler); - CPLErrorStateBackuper errorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); if (!oDoc.Load(osZattrsFilename)) return; auto oRoot = oDoc.GetRoot(); diff --git a/gcore/gdaldriver.cpp b/gcore/gdaldriver.cpp index 325320780736..6bff5acd3f50 100644 --- a/gcore/gdaldriver.cpp +++ b/gcore/gdaldriver.cpp @@ -1574,8 +1574,7 @@ CPLErr GDALDriver::QuietDelete(const char *pszName, } else { - CPLErrorStateBackuper oBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); poDriver = GDALDriver::FromHandle(GDALIdentifyDriver(pszName, nullptr)); } @@ -1589,8 +1588,7 @@ CPLErr GDALDriver::QuietDelete(const char *pszName, poDriver->pfnDeleteDataSource == nullptr; if (bQuiet) { - CPLErrorStateBackuper oBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); return poDriver->Delete(pszName); } else diff --git a/gcore/gdaljp2abstractdataset.cpp b/gcore/gdaljp2abstractdataset.cpp index 28c64876fd2c..fc63d9e28578 100644 --- a/gcore/gdaljp2abstractdataset.cpp +++ b/gcore/gdaljp2abstractdataset.cpp @@ -655,8 +655,7 @@ char **GDALJP2AbstractDataset::GetMetadata(const char *pszDomain) m_aosImageStructureMetadata.Assign( CSLDuplicate(GDALGeorefPamDataset::GetMetadata(pszDomain)), true); - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); const char *pszReversibility = GDALGetJPEG2000Reversibility(GetDescription(), fp); if (pszReversibility) diff --git a/gcore/gdaljp2metadata.cpp b/gcore/gdaljp2metadata.cpp index e15e8aee6599..139c6940c27b 100644 --- a/gcore/gdaljp2metadata.cpp +++ b/gcore/gdaljp2metadata.cpp @@ -1270,8 +1270,7 @@ bool GDALJP2Metadata::IsSRSCompatible(const OGRSpatialReference *poSRS) return true; } - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); char *pszGMLDef = nullptr; const bool bRet = (poSRS->exportToXML(&pszGMLDef, nullptr) == OGRERR_NONE); CPLFree(pszGMLDef); diff --git a/gcore/gdalmultidim.cpp b/gcore/gdalmultidim.cpp index cc1c392d8fa5..3f9aa4275608 100644 --- a/gcore/gdalmultidim.cpp +++ b/gcore/gdalmultidim.cpp @@ -1130,8 +1130,8 @@ bool GDALGroup::CopyFrom(const std::shared_ptr &poDstRootGroup, mapExistingDstDims.find(oIterDimName->second); if (oCorrespondingDimIter != mapExistingDstDims.end()) { - CPLErrorHandlerPusher oHandlerPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper( + CPLQuietErrorHandler); oCorrespondingDimIter->second->SetIndexingVariable( std::move(dstArray)); } @@ -13516,8 +13516,7 @@ void GDALPamMultiDim::Load() pszProxyPam ? std::string(pszProxyPam) : d->m_osFilename + ".aux.xml"; CPLXMLTreeCloser oTree(nullptr); { - CPLErrorStateBackuper oStateBackuper; - CPLErrorHandlerPusher oErrorHandlerPusher(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); oTree.reset(CPLParseXMLFile(d->m_osPamFilename.c_str())); } if (!oTree) @@ -13638,8 +13637,7 @@ void GDALPamMultiDim::Save() { char *pszWKT = nullptr; { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); const char *const apszOptions[] = {"FORMAT=WKT2", nullptr}; kv.second.poSRS->exportToWkt(&pszWKT, apszOptions); } diff --git a/gcore/gdalpamdataset.cpp b/gcore/gdalpamdataset.cpp index ccfb46b222d1..acc2a96b616b 100644 --- a/gcore/gdalpamdataset.cpp +++ b/gcore/gdalpamdataset.cpp @@ -233,8 +233,7 @@ CPLXMLNode *GDALPamDataset::SerializeToXML(const char *pszUnused) { char *pszWKT = nullptr; { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); if (psPam->poSRS->exportToWkt(&pszWKT) != OGRERR_NONE) { CPLFree(pszWKT); @@ -905,8 +904,7 @@ CPLErr GDALPamDataset::TryLoadXML(char **papszSiblingFiles) papszSiblingFiles, CPLGetFilename(psPam->pszPamFilename)); if (iSibling >= 0) { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); psTree = CPLParseXMLFile(psPam->pszPamFilename); } } @@ -914,8 +912,7 @@ CPLErr GDALPamDataset::TryLoadXML(char **papszSiblingFiles) VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 && VSI_ISREG(sStatBuf.st_mode)) { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); psTree = CPLParseXMLFile(psPam->pszPamFilename); } @@ -1042,8 +1039,7 @@ CPLErr GDALPamDataset::TrySaveXML() VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0 && VSI_ISREG(sStatBuf.st_mode)) { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); psOldTree = CPLParseXMLFile(psPam->pszPamFilename); } diff --git a/ogr/ogrct.cpp b/ogr/ogrct.cpp index fb5bb613b9ce..baf3da169880 100644 --- a/ogr/ogrct.cpp +++ b/ogr/ogrct.cpp @@ -182,8 +182,7 @@ void OGRCoordinateTransformationOptions::Private::RefreshCheckWithInvertProj() static char *GetWktOrProjString(const OGRSpatialReference *poSRS) { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); const char *const apszOptionsWKT2_2018[] = {"FORMAT=WKT2_2018", nullptr}; // If there's a PROJ4 EXTENSION node in WKT1, then use // it. For example when dealing with "+proj=longlat +lon_wrap=180" @@ -1536,8 +1535,7 @@ int OGRProjCT::Initialize(const OGRSpatialReference *poSourceIn, const char *pszCENTER_LONG; { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); pszCENTER_LONG = poSRSSource ? poSRSSource->GetExtension("GEOGCS", "CENTER_LONG") : nullptr; @@ -1556,8 +1554,7 @@ int OGRProjCT::Initialize(const OGRSpatialReference *poSourceIn, } { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); pszCENTER_LONG = poSRSTarget ? poSRSTarget->GetExtension("GEOGCS", "CENTER_LONG") : nullptr; @@ -3358,8 +3355,7 @@ int OGRProjCT::TransformBounds(const double xmin, const double ymin, if (poSRSTarget->IsProjected()) { - CPLErrorHandlerPusher oErrorHandlerPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oBackuper; + CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); auto poBaseTarget = std::unique_ptr( poSRSTarget->CloneGeogCS()); diff --git a/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp b/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp index 217ca9e795a0..08f9266cf056 100644 --- a/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp +++ b/ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp @@ -163,8 +163,7 @@ OGRFlatGeobufLayer::OGRFlatGeobufLayer(const Header *poHeader, GByte *headerBuf, if (const auto metadata = poHeader->metadata()) { CPLJSONDocument oDoc; - CPLErrorHandlerPusher oQuietError(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); if (oDoc.LoadMemory(metadata->c_str()) && oDoc.GetRoot().GetType() == CPLJSONObject::Type::Object) { diff --git a/ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp b/ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp index cdf1dcdabbef..1c88d044b258 100644 --- a/ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp +++ b/ogr/ogrsf_frmts/generic/ogrlayerarrow.cpp @@ -7436,8 +7436,7 @@ bool OGRLayer::WriteArrowBatch(const struct ArrowSchema *schema, bool bTransactionOK; { - CPLErrorHandlerPusher oHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oBackuper; + CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); bTransactionOK = StartTransaction() == OGRERR_NONE; } diff --git a/ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp b/ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp index 421f6a66f670..ddd38dc881db 100644 --- a/ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp +++ b/ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp @@ -180,8 +180,7 @@ bool GMLASXSDCache::CacheAllGML321() // Download the later and unzip it for faster fetching of GML schemas. bool bSuccess = false; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); const char *pszHTTPZIP = "https://schemas.opengis.net/gml/gml-3_2_2.zip"; CPLHTTPResult *psResult = CPLHTTPFetch(pszHTTPZIP, nullptr); @@ -248,8 +247,7 @@ bool GMLASXSDCache::CacheAllISO20070417() // Download the later and unzip it for faster fetching of ISO schemas. bool bSuccess = false; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); const char *pszHTTPZIP = "https://schemas.opengis.net/iso/19139/iso19139-20070417.zip"; diff --git a/ogr/ogrsf_frmts/gpsbabel/ogrgpsbabeldriver.cpp b/ogr/ogrsf_frmts/gpsbabel/ogrgpsbabeldriver.cpp index 0af3ff31200f..eca3cc717701 100644 --- a/ogr/ogrsf_frmts/gpsbabel/ogrgpsbabeldriver.cpp +++ b/ogr/ogrsf_frmts/gpsbabel/ogrgpsbabeldriver.cpp @@ -113,8 +113,7 @@ OGRGPSBabelDriverIdentifyInternal(GDALOpenInfo *poOpenInfo, if (!bGPSBabelFound) #endif { - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); const char *const apszArgs[] = {"gpsbabel", "-V", nullptr}; CPLString osTmpFileName("/vsimem/gpsbabel_tmp.tmp"); VSILFILE *tmpfp = VSIFOpenL(osTmpFileName, "wb"); diff --git a/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp b/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp index ea4737dbf647..4887f26c7cfa 100644 --- a/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp +++ b/ogr/ogrsf_frmts/mvt/ogrmvtdataset.cpp @@ -3819,7 +3819,7 @@ bool OGRMVTWriterDataset::EncodeRepairedOuterRing( oOutPoly.addRingDirectly(poOutLinearRing.release()); int bIsValid; { - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); bIsValid = oOutPoly.IsValid(); } if (bIsValid) @@ -3953,8 +3953,7 @@ OGRErr OGRMVTWriterDataset::PreGenerateForTileReal( OGRPolygon oPoly; oPoly.addRingDirectly(poLR); - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); auto poTmp = poGeom->Intersection(&oPoly); poIntersection = poTmp; poIntersectionHolder.reset(poTmp); @@ -4169,8 +4168,7 @@ OGRErr OGRMVTWriterDataset::PreGenerateForTileReal( dfAreaOrLength); int bIsValid; { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); bIsValid = oOutPoly.IsValid(); } if (!bIsValid) @@ -4211,8 +4209,7 @@ OGRErr OGRMVTWriterDataset::PreGenerateForTileReal( } int bIsValid; { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); bIsValid = oOutMP.IsValid(); } if (!bIsValid) @@ -4693,8 +4690,7 @@ GetReducedPrecisionGeometry(MVTTileLayerFeature::GeomType eGeomType, poOutOuterRing = std::unique_ptr( poOutRing.release()); { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler( + CPLErrorStateBackuper oErrorStateBackuper( CPLQuietErrorHandler); bIsValid = oPoly.IsValid(); } @@ -4715,8 +4711,7 @@ GetReducedPrecisionGeometry(MVTTileLayerFeature::GeomType eGeomType, oPoly.addRing(poOutOuterRing.get()); oPoly.addRingDirectly(poOutRing.release()); { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler( + CPLErrorStateBackuper oErrorStateBackuper( CPLQuietErrorHandler); bIsValid = oPoly.IsValid(); } diff --git a/ogr/ogrsf_frmts/oci/ogrocidatasource.cpp b/ogr/ogrsf_frmts/oci/ogrocidatasource.cpp index 6bacb96f811e..37221441fa95 100644 --- a/ogr/ogrsf_frmts/oci/ogrocidatasource.cpp +++ b/ogr/ogrsf_frmts/oci/ogrocidatasource.cpp @@ -838,8 +838,7 @@ OGRSpatialReference *OGROCIDataSource::FetchSRS(int nId) if (nId < LARGEST_EPSG_CRS_CODE && papszResult[1] != nullptr && atoi(papszResult[1]) == nId) { - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); OGRSpatialReference oSRS_EPSG; oSRS_EPSG.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER); const char *const apszOptions[] = { diff --git a/ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp b/ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp index 6955eeaa1c31..99c8f81cdd75 100644 --- a/ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp +++ b/ogr/ogrsf_frmts/pg/ogrpgtablelayer.cpp @@ -254,8 +254,7 @@ void OGRPGTableLayer::LoadMetadata() "schema_name = %s AND table_name = %s", OGRPGEscapeString(hPGConn, pszSchemaName).c_str(), OGRPGEscapeString(hPGConn, pszTableName).c_str())); - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oBackuper; + CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); auto poSqlLyr = poDS->ExecuteSQL(osSQL.c_str(), nullptr, nullptr); if (poSqlLyr) { diff --git a/ogr/ogrsf_frmts/pgeo/ogrpgeodatasource.cpp b/ogr/ogrsf_frmts/pgeo/ogrpgeodatasource.cpp index 5888c8ced431..8f8b44f37660 100644 --- a/ogr/ogrsf_frmts/pgeo/ogrpgeodatasource.cpp +++ b/ogr/ogrsf_frmts/pgeo/ogrpgeodatasource.cpp @@ -658,8 +658,7 @@ bool OGRPGeoDataSource::CountStarWorking() const } #endif - CPLErrorHandlerPusher oErrorHandler(CPLErrorHandlerPusher); - CPLErrorStateBackuper oStateBackuper; + CPLErrorStateBackuper oStateBackuper(CPLErrorHandlerPusher); CPLODBCStatement oStmt(&oSession); oStmt.Append("SELECT COUNT(*) FROM GDB_GeomColumns"); diff --git a/ogr/ogrsf_frmts/pmtiles/vsipmtiles.cpp b/ogr/ogrsf_frmts/pmtiles/vsipmtiles.cpp index 1018936602fc..9e78e56e065b 100644 --- a/ogr/ogrsf_frmts/pmtiles/vsipmtiles.cpp +++ b/ogr/ogrsf_frmts/pmtiles/vsipmtiles.cpp @@ -259,8 +259,7 @@ VSIPMTilesFilesystemHandler::Open(const char *pszFilename, if (nComponents != 3) return nullptr; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oBackuper; + CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); OGRPMTilesTileIterator oIter(poDS.get(), nZ, nX, nY, nX, nY); auto sTile = oIter.GetNextTile(); @@ -320,8 +319,7 @@ int VSIPMTilesFilesystemHandler::Stat(const char *pszFilename, return 0; } - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); - CPLErrorStateBackuper oBackuper; + CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler); OGRPMTilesTileIterator oIter(poDS.get(), nZ, nX, nY, nX, nY); auto sTile = oIter.GetNextTile(); diff --git a/ogr/ogrsf_frmts/sqlite/ogrsqliteselectlayer.cpp b/ogr/ogrsf_frmts/sqlite/ogrsqliteselectlayer.cpp index 09fbbc7afa98..0de657090f08 100644 --- a/ogr/ogrsf_frmts/sqlite/ogrsqliteselectlayer.cpp +++ b/ogr/ogrsf_frmts/sqlite/ogrsqliteselectlayer.cpp @@ -141,8 +141,8 @@ OGRSQLiteSelectLayer::OGRSQLiteSelectLayer( sqlite3_column_table_name(m_hStmt, poGeomFieldDefn->m_iCol); if (pszTableName != nullptr) { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper( + CPLQuietErrorHandler); OGRSQLiteLayer *m_poLayer = cpl::down_cast( m_poDS->GetLayerByName(pszTableName)); diff --git a/ogr/ogrsf_frmts/wfs/ogroapifdriver.cpp b/ogr/ogrsf_frmts/wfs/ogroapifdriver.cpp index fd2b8076e760..b46e324c1234 100644 --- a/ogr/ogrsf_frmts/wfs/ogroapifdriver.cpp +++ b/ogr/ogrsf_frmts/wfs/ogroapifdriver.cpp @@ -1636,8 +1636,7 @@ void OGROAPIFLayer::GetSchema() if (m_osDescribedByURL.empty() || m_poDS->m_bIgnoreSchema) return; - CPLErrorHandlerPusher oErrorHandlerPusher(CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); if (m_bDescribedByIsXML) { diff --git a/ogr/ogrsf_frmts/wfs/ogrwfsdatasource.cpp b/ogr/ogrsf_frmts/wfs/ogrwfsdatasource.cpp index 3897126c982f..8e523309ae7f 100644 --- a/ogr/ogrsf_frmts/wfs/ogrwfsdatasource.cpp +++ b/ogr/ogrsf_frmts/wfs/ogrwfsdatasource.cpp @@ -1389,9 +1389,9 @@ int OGRWFSDataSource::Open(const char *pszFilename, int bUpdateIn, apoSupportedCRSList.emplace_back(std::move(poSRS)); } } - CPLErrorHandlerPusher oErrorHandlerPusher( + + CPLErrorStateBackuper oErrorStateBackuper( CPLQuietErrorHandler); - CPLErrorStateBackuper oErrorStateBackuper; for (const CPLXMLNode *psIter = psOtherSRS; psIter; psIter = psIter->psNext) { diff --git a/ogr/ogrspatialreference.cpp b/ogr/ogrspatialreference.cpp index a5421fc98ea4..8121d79687e0 100644 --- a/ogr/ogrspatialreference.cpp +++ b/ogr/ogrspatialreference.cpp @@ -369,8 +369,7 @@ void OGRSpatialReference::Private::refreshRootFromProjObj() const char *pszWKT; { - CPLErrorStateBackuper oErrorStateBackuper; - CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler); + CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler); pszWKT = proj_as_wkt(getPROJContext(), m_pj_crs, m_bMorphToESRI ? PJ_WKT1_ESRI : PJ_WKT1_GDAL, aosOptions.List());