Line data Source code
1 : /********************************************************************** 2 : * 3 : * Name: cpl_error_internal.h 4 : * Project: CPL - Common Portability Library 5 : * Purpose: CPL Error handling 6 : * Author: Even Rouault, <even.rouault at spatialys.com> 7 : * 8 : ********************************************************************** 9 : * Copyright (c) 2019, Even Rouault, <even.rouault at spatialys.com> 10 : * 11 : * SPDX-License-Identifier: MIT 12 : ****************************************************************************/ 13 : 14 : #ifndef CPL_ERROR_INTERNAL_H_INCLUDED 15 : #define CPL_ERROR_INTERNAL_H_INCLUDED 16 : 17 : #if defined(GDAL_COMPILATION) || defined(DOXYGEN_SKIP) 18 : // internal only 19 : 20 : #include "cpl_error.h" 21 : #include "cpl_string.h" 22 : 23 : #include <mutex> 24 : #include <vector> 25 : 26 : /************************************************************************/ 27 : /* CPLErrorHandlerAccumulatorStruct */ 28 : /************************************************************************/ 29 : 30 : /** Class that stores details about an emitted error. 31 : * 32 : * Returned by CPLErrorAccumulator::GetErrors() 33 : * 34 : * @since 3.11 35 : */ 36 : class CPL_DLL CPLErrorHandlerAccumulatorStruct 37 : { 38 : public: 39 : /** Error level */ 40 : CPLErr type; 41 : 42 : /** Error number */ 43 : CPLErrorNum no; 44 : 45 : /** Error message */ 46 : CPLString msg{}; 47 : 48 : /** Default constructor */ 49 : CPLErrorHandlerAccumulatorStruct() : type(CE_None), no(CPLE_None) 50 : { 51 : } 52 : 53 : /** Constructor */ 54 423 : CPLErrorHandlerAccumulatorStruct(CPLErr eErrIn, CPLErrorNum noIn, 55 : const char *msgIn) 56 423 : : type(eErrIn), no(noIn), msg(msgIn) 57 : { 58 423 : } 59 : }; 60 : 61 : /************************************************************************/ 62 : /* CPLErrorAccumulator */ 63 : /************************************************************************/ 64 : 65 : /** Class typically used by a worker thread to store errors emitted by their 66 : * worker functions, and replay them in the main thread. 67 : * 68 : * An instance of CPLErrorAccumulator can be shared by several 69 : * threads. Each thread calls InstallForCurrentScope() in its processing 70 : * function. The main thread may invoke ReplayErrors() to replay errors (and 71 : * warnings). 72 : * 73 : * @since 3.11 74 : */ 75 : class CPL_DLL CPLErrorAccumulator 76 : { 77 : public: 78 : /** Constructor */ 79 79066 : CPLErrorAccumulator() = default; 80 : 81 : /** Object returned by InstallForCurrentScope() during life-time of which, 82 : * errors are redirected to the CPLErrorAccumulator instance. 83 : */ 84 : struct CPL_DLL Context 85 : { 86 : ~Context(); 87 : }; 88 : 89 : /** Install a temporary error handler that will store errors and warnings. 90 : */ 91 : Context InstallForCurrentScope() CPL_WARN_UNUSED_RESULT; 92 : 93 : /** Return error list. */ 94 78286 : const std::vector<CPLErrorHandlerAccumulatorStruct> &GetErrors() const 95 : { 96 78286 : return errors; 97 : } 98 : 99 : /** Replay stored errors. */ 100 : void ReplayErrors(); 101 : 102 : private: 103 : std::mutex mutex{}; 104 : std::vector<CPLErrorHandlerAccumulatorStruct> errors{}; 105 : 106 : static void CPL_STDCALL Accumulator(CPLErr eErr, CPLErrorNum no, 107 : const char *msg); 108 : }; 109 : 110 : #endif 111 : 112 : #endif // CPL_ERROR_INTERNAL_H_INCLUDED