File: cpl_error.cpp

package info (click to toggle)
gdal 3.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 89,664 kB
  • sloc: cpp: 1,136,033; ansic: 197,355; python: 35,910; java: 5,511; xml: 4,011; sh: 3,950; cs: 2,443; yacc: 1,047; makefile: 288
file content (1374 lines) | stat: -rw-r--r-- 47,737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374

/**********************************************************************
 *
 * Name:     cpl_error.cpp
 * Project:  CPL - Common Portability Library
 * Purpose:  Error handling functions.
 * Author:   Daniel Morissette, danmo@videotron.ca
 *
 **********************************************************************
 * Copyright (c) 1998, Daniel Morissette
 * Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#include "cpl_error.h"

#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>

#include "cpl_config.h"
#include "cpl_conv.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "cpl_error_internal.h"

#if !defined(va_copy) && defined(__va_copy)
#define va_copy __va_copy
#endif

#define TIMESTAMP_DEBUG
// #define MEMORY_DEBUG

static CPLMutex *hErrorMutex = nullptr;
static void *pErrorHandlerUserData = nullptr;
static CPLErrorHandler pfnErrorHandler = CPLDefaultErrorHandler;
static bool gbCatchDebug = true;

constexpr int DEFAULT_LAST_ERR_MSG_SIZE =
#if !defined(HAVE_VSNPRINTF)
    20000
#else
    500
#endif
    ;

typedef struct errHandler
{
    struct errHandler *psNext;
    void *pUserData;
    CPLErrorHandler pfnHandler;
    bool bCatchDebug;
} CPLErrorHandlerNode;

typedef struct
{
    CPLErrorNum nLastErrNo;
    CPLErr eLastErrType;
    CPLErrorHandlerNode *psHandlerStack;
    int nLastErrMsgMax;
    int nFailureIntoWarning;
    GUInt32 nErrorCounter;
    char szLastErrMsg[DEFAULT_LAST_ERR_MSG_SIZE];
    // Do not add anything here. szLastErrMsg must be the last field.
    // See CPLRealloc() below.
} CPLErrorContext;

constexpr CPLErrorContext sNoErrorContext = {0, CE_None, nullptr, 0, 0, 0, ""};

constexpr CPLErrorContext sWarningContext = {
    0, CE_Warning, nullptr, 0, 0, 0, "A warning was emitted"};

constexpr CPLErrorContext sFailureContext = {
    0, CE_Warning, nullptr, 0, 0, 0, "A failure was emitted"};

#define IS_PREFEFINED_ERROR_CTX(psCtxt)                                        \
    (psCtx == &sNoErrorContext || psCtx == &sWarningContext ||                 \
     psCtxt == &sFailureContext)

/************************************************************************/
/*                     CPLErrorContextGetString()                       */
/************************************************************************/

// Makes clang -fsanitize=undefined happy since it doesn't like
// dereferencing szLastErrMsg[i>=DEFAULT_LAST_ERR_MSG_SIZE]

static char *CPLErrorContextGetString(CPLErrorContext *psCtxt)
{
    return psCtxt->szLastErrMsg;
}

/************************************************************************/
/*                         CPLGetErrorContext()                         */
/************************************************************************/

static CPLErrorContext *CPLGetErrorContext()

{
    int bError = FALSE;
    CPLErrorContext *psCtx = reinterpret_cast<CPLErrorContext *>(
        CPLGetTLSEx(CTLS_ERRORCONTEXT, &bError));
    if (bError)
        return nullptr;

    if (psCtx == nullptr)
    {
        psCtx = static_cast<CPLErrorContext *>(
            VSICalloc(sizeof(CPLErrorContext), 1));
        if (psCtx == nullptr)
        {
            fprintf(stderr, "Out of memory attempting to report error.\n");
            return nullptr;
        }
        psCtx->eLastErrType = CE_None;
        psCtx->nLastErrMsgMax = sizeof(psCtx->szLastErrMsg);
        CPLSetTLS(CTLS_ERRORCONTEXT, psCtx, TRUE);
    }

    return psCtx;
}

/************************************************************************/
/*                         CPLGetErrorHandlerUserData()                 */
/************************************************************************/

/**
 * Fetch the user data for the error context
 *
 * Fetches the user data for the current error context.  You can
 * set the user data for the error context when you add your handler by
 * issuing CPLSetErrorHandlerEx() and CPLPushErrorHandlerEx().  Note that
 * user data is primarily intended for providing context within error handlers
 * themselves, but they could potentially be abused in other useful ways with
 * the usual caveat emptor understanding.
 *
 * @return the user data pointer for the error context
 */

void *CPL_STDCALL CPLGetErrorHandlerUserData(void)
{
    int bError = FALSE;

    // check if there is an active error being propagated through the handlers
    void **pActiveUserData = reinterpret_cast<void **>(
        CPLGetTLSEx(CTLS_ERRORHANDLERACTIVEDATA, &bError));
    if (bError)
        return nullptr;

    if (pActiveUserData != nullptr)
    {
        return *pActiveUserData;
    }

    // get the current threadlocal or global error context user data
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr || IS_PREFEFINED_ERROR_CTX(psCtx))
        abort();
    return reinterpret_cast<void *>(psCtx->psHandlerStack
                                        ? psCtx->psHandlerStack->pUserData
                                        : pErrorHandlerUserData);
}

static void ApplyErrorHandler(CPLErrorContext *psCtx, CPLErr eErrClass,
                              CPLErrorNum err_no, const char *pszMessage)
{
    void **pActiveUserData;
    bool bProcessed = false;

    // CTLS_ERRORHANDLERACTIVEDATA holds the active error handler userData

    if (psCtx->psHandlerStack != nullptr)
    {
        // iterate through the threadlocal handler stack
        if ((eErrClass != CE_Debug) || psCtx->psHandlerStack->bCatchDebug)
        {
            // call the error handler
            pActiveUserData = &(psCtx->psHandlerStack->pUserData);
            CPLSetTLS(CTLS_ERRORHANDLERACTIVEDATA, pActiveUserData, false);
            psCtx->psHandlerStack->pfnHandler(eErrClass, err_no, pszMessage);
            bProcessed = true;
        }
        else
        {
            // need to iterate to a parent handler for debug messages
            CPLErrorHandlerNode *psNode = psCtx->psHandlerStack->psNext;
            while (psNode != nullptr)
            {
                if (psNode->bCatchDebug)
                {
                    pActiveUserData = &(psNode->pUserData);
                    CPLSetTLS(CTLS_ERRORHANDLERACTIVEDATA, pActiveUserData,
                              false);
                    psNode->pfnHandler(eErrClass, err_no, pszMessage);
                    bProcessed = true;
                    break;
                }
                psNode = psNode->psNext;
            }
        }
    }

    if (!bProcessed)
    {
        // hit the global error handler
        CPLMutexHolderD(&hErrorMutex);
        if ((eErrClass != CE_Debug) || gbCatchDebug)
        {
            if (pfnErrorHandler != nullptr)
            {
                pActiveUserData = &pErrorHandlerUserData;
                CPLSetTLS(CTLS_ERRORHANDLERACTIVEDATA, pActiveUserData, false);
                pfnErrorHandler(eErrClass, err_no, pszMessage);
            }
        }
        else /* if( eErrClass == CE_Debug ) */
        {
            // for CPLDebug messages we propagate to the default error handler
            pActiveUserData = nullptr;
            CPLSetTLS(CTLS_ERRORHANDLERACTIVEDATA, pActiveUserData, false);
            CPLDefaultErrorHandler(eErrClass, err_no, pszMessage);
        }
    }
    CPLSetTLS(CTLS_ERRORHANDLERACTIVEDATA, nullptr, false);
}

/**********************************************************************
 *                          CPLError()
 **********************************************************************/

/**
 * Report an error.
 *
 * This function reports an error in a manner that can be hooked
 * and reported appropriate by different applications.
 *
 * The effect of this function can be altered by applications by installing
 * a custom error handling using CPLSetErrorHandler().
 *
 * The eErrClass argument can have the value CE_Warning indicating that the
 * message is an informational warning, CE_Failure indicating that the
 * action failed, but that normal recover mechanisms will be used or
 * CE_Fatal meaning that a fatal error has occurred, and that CPLError()
 * should not return.
 *
 * The default behavior of CPLError() is to report errors to stderr,
 * and to abort() after reporting a CE_Fatal error.  It is expected that
 * some applications will want to suppress error reporting, and will want to
 * install a C++ exception, or longjmp() approach to no local fatal error
 * recovery.
 *
 * Regardless of how application error handlers or the default error
 * handler choose to handle an error, the error number, and message will
 * be stored for recovery with CPLGetLastErrorNo() and CPLGetLastErrorMsg().
 *
 * @param eErrClass one of CE_Warning, CE_Failure or CE_Fatal.
 * @param err_no the error number (CPLE_*) from cpl_error.h.
 * @param fmt a printf() style format string.  Any additional arguments
 * will be treated as arguments to fill in this format in a manner
 * similar to printf().
 */

void CPLError(CPLErr eErrClass, CPLErrorNum err_no,
              CPL_FORMAT_STRING(const char *fmt), ...)
{
    va_list args;

    // Expand the error message.
    va_start(args, fmt);
    CPLErrorV(eErrClass, err_no, fmt, args);
    va_end(args);
}

/************************************************************************/
/*                             CPLErrorV()                              */
/************************************************************************/

/** Same as CPLError() but with a va_list */
void CPLErrorV(CPLErr eErrClass, CPLErrorNum err_no, const char *fmt,
               va_list args)
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr || IS_PREFEFINED_ERROR_CTX(psCtx))
    {
        int bMemoryError = FALSE;
        if (eErrClass == CE_Warning)
        {
            CPLSetTLSWithFreeFuncEx(
                CTLS_ERRORCONTEXT,
                reinterpret_cast<void *>(
                    const_cast<CPLErrorContext *>(&sWarningContext)),
                nullptr, &bMemoryError);
        }
        else if (eErrClass == CE_Failure)
        {
            CPLSetTLSWithFreeFuncEx(
                CTLS_ERRORCONTEXT,
                reinterpret_cast<void *>(
                    const_cast<CPLErrorContext *>(&sFailureContext)),
                nullptr, &bMemoryError);
        }

        // TODO: Is it possible to move the entire szShortMessage under the if
        // pfnErrorHandler?
        char szShortMessage[80] = {};
        CPLvsnprintf(szShortMessage, sizeof(szShortMessage), fmt, args);

        CPLMutexHolderD(&hErrorMutex);
        if (pfnErrorHandler != nullptr)
            pfnErrorHandler(eErrClass, err_no, szShortMessage);
        return;
    }

    if (psCtx->nFailureIntoWarning > 0 && eErrClass == CE_Failure)
        eErrClass = CE_Warning;

/* -------------------------------------------------------------------- */
/*      Expand the error message                                        */
/* -------------------------------------------------------------------- */
#if defined(HAVE_VSNPRINTF)
    {
        va_list wrk_args;

#ifdef va_copy
        va_copy(wrk_args, args);
#else
        wrk_args = args;
#endif

        /* --------------------------------------------------------------------
         */
        /*      If CPL_ACCUM_ERROR_MSG=ON accumulate the error messages, */
        /*      rather than just replacing the last error message. */
        /* --------------------------------------------------------------------
         */
        int nPreviousSize = 0;
        if (psCtx->psHandlerStack != nullptr &&
            EQUAL(CPLGetConfigOption("CPL_ACCUM_ERROR_MSG", ""), "ON"))
        {
            nPreviousSize = static_cast<int>(strlen(psCtx->szLastErrMsg));
            if (nPreviousSize)
            {
                if (nPreviousSize + 1 + 1 >= psCtx->nLastErrMsgMax)
                {
                    psCtx->nLastErrMsgMax *= 3;
                    psCtx = static_cast<CPLErrorContext *>(
                        CPLRealloc(psCtx, sizeof(CPLErrorContext) -
                                              DEFAULT_LAST_ERR_MSG_SIZE +
                                              psCtx->nLastErrMsgMax + 1));
                    CPLSetTLS(CTLS_ERRORCONTEXT, psCtx, TRUE);
                }
                char *pszLastErrMsg = CPLErrorContextGetString(psCtx);
                pszLastErrMsg[nPreviousSize] = '\n';
                pszLastErrMsg[nPreviousSize + 1] = '\0';
                nPreviousSize++;
            }
        }

        int nPR = 0;
        while (((nPR = CPLvsnprintf(psCtx->szLastErrMsg + nPreviousSize,
                                    psCtx->nLastErrMsgMax - nPreviousSize, fmt,
                                    wrk_args)) == -1 ||
                nPR >= psCtx->nLastErrMsgMax - nPreviousSize - 1) &&
               psCtx->nLastErrMsgMax < 1000000)
        {
#ifdef va_copy
            va_end(wrk_args);
            va_copy(wrk_args, args);
#else
            wrk_args = args;
#endif
            psCtx->nLastErrMsgMax *= 3;
            psCtx = static_cast<CPLErrorContext *>(CPLRealloc(
                psCtx, sizeof(CPLErrorContext) - DEFAULT_LAST_ERR_MSG_SIZE +
                           psCtx->nLastErrMsgMax + 1));
            CPLSetTLS(CTLS_ERRORCONTEXT, psCtx, TRUE);
        }

        va_end(wrk_args);
    }
#else
    // !HAVE_VSNPRINTF
    CPLvsnprintf(psCtx->szLastErrMsg, psCtx->nLastErrMsgMax, fmt, args);
#endif

    /* -------------------------------------------------------------------- */
    /*      Obfuscate any password in error message                         */
    /* -------------------------------------------------------------------- */
    char *pszPassword = strstr(psCtx->szLastErrMsg, "password=");
    if (pszPassword != nullptr)
    {
        char *pszIter = pszPassword + strlen("password=");
        while (*pszIter != ' ' && *pszIter != '\0')
        {
            *pszIter = 'X';
            pszIter++;
        }
    }

    /* -------------------------------------------------------------------- */
    /*      If the user provided an handling function, then                 */
    /*      call it, otherwise print the error to stderr and return.        */
    /* -------------------------------------------------------------------- */
    psCtx->nLastErrNo = err_no;
    psCtx->eLastErrType = eErrClass;
    if (psCtx->nErrorCounter == ~(0U))
        psCtx->nErrorCounter = 0;
    else
        psCtx->nErrorCounter++;

    if (CPLGetConfigOption("CPL_LOG_ERRORS", nullptr) != nullptr)
        CPLDebug("CPLError", "%s", psCtx->szLastErrMsg);

    /* -------------------------------------------------------------------- */
    /*      Invoke the current error handler.                               */
    /* -------------------------------------------------------------------- */
    ApplyErrorHandler(psCtx, eErrClass, err_no, psCtx->szLastErrMsg);

    if (eErrClass == CE_Fatal)
        abort();
}

/************************************************************************/
/*                         CPLEmergencyError()                          */
/************************************************************************/

/**
 * Fatal error when things are bad.
 *
 * This function should be called in an emergency situation where
 * it is unlikely that a regular error report would work.  This would
 * include in the case of heap exhaustion for even small allocations,
 * or any failure in the process of reporting an error (such as TLS
 * allocations).
 *
 * This function should never return.  After the error message has been
 * reported as best possible, the application will abort() similarly to how
 * CPLError() aborts on CE_Fatal class errors.
 *
 * @param pszMessage the error message to report.
 */

void CPLEmergencyError(const char *pszMessage)
{
    static bool bInEmergencyError = false;

    // If we are already in emergency error then one of the
    // following failed, so avoid them the second time through.
    if (!bInEmergencyError)
    {
        bInEmergencyError = true;
        CPLErrorContext *psCtx =
            static_cast<CPLErrorContext *>(CPLGetTLS(CTLS_ERRORCONTEXT));

        ApplyErrorHandler(psCtx, CE_Fatal, CPLE_AppDefined, pszMessage);
    }

    // Ultimate fallback.
    fprintf(stderr, "FATAL: %s\n", pszMessage);

    abort();
}

/************************************************************************/
/*                    CPLGetProcessMemorySize()                         */
/************************************************************************/

#ifdef MEMORY_DEBUG

#ifdef __linux
static int CPLGetProcessMemorySize()
{
    FILE *fp = fopen("/proc/self/status", "r");
    if (fp == nullptr)
        return -1;
    int nRet = -1;
    char szLine[128] = {};
    while (fgets(szLine, sizeof(szLine), fp) != nullptr)
    {
        if (STARTS_WITH(szLine, "VmSize:"))
        {
            const char *pszPtr = szLine;
            while (!(*pszPtr == '\0' || (*pszPtr >= '0' && *pszPtr <= '9')))
                pszPtr++;
            nRet = atoi(pszPtr);
            break;
        }
    }
    fclose(fp);
    return nRet;
}
#else
#error CPLGetProcessMemorySize() unimplemented for this OS
#endif

#endif  // def MEMORY_DEBUG

/************************************************************************/
/*                        CPLGettimeofday()                             */
/************************************************************************/

#if defined(_WIN32) && !defined(__CYGWIN__)
#include <sys/timeb.h>

namespace
{
struct CPLTimeVal
{
    time_t tv_sec; /* seconds */
    long tv_usec;  /* and microseconds */
};
}  // namespace

static int CPLGettimeofday(struct CPLTimeVal *tp, void * /* timezonep*/)
{
    struct _timeb theTime;

    _ftime(&theTime);
    tp->tv_sec = static_cast<time_t>(theTime.time);
    tp->tv_usec = theTime.millitm * 1000;
    return 0;
}
#else
#include <sys/time.h> /* for gettimeofday() */
#define CPLTimeVal timeval
#define CPLGettimeofday(t, u) gettimeofday(t, u)
#endif

/************************************************************************/
/*                              CPLDebug()                              */
/************************************************************************/

/**
 * Display a debugging message.
 *
 * The category argument is used in conjunction with the CPL_DEBUG
 * environment variable to establish if the message should be displayed.
 * If the CPL_DEBUG environment variable is not set, no debug messages
 * are emitted (use CPLError(CE_Warning, ...) to ensure messages are displayed).
 * If CPL_DEBUG is set, but is an empty string or the word "ON" then all
 * debug messages are shown.  Otherwise only messages whose category appears
 * somewhere within the CPL_DEBUG value are displayed (as determined by
 * strstr()).
 *
 * Categories are usually an identifier for the subsystem producing the
 * error.  For instance "GDAL" might be used for the GDAL core, and "TIFF"
 * for messages from the TIFF translator.
 *
 * @param pszCategory name of the debugging message category.
 * @param pszFormat printf() style format string for message to display.
 *        Remaining arguments are assumed to be for format.
 */

#ifdef WITHOUT_CPLDEBUG
// Do not include CPLDebug.  Only available in custom builds.
#else
void CPLDebug(const char *pszCategory, CPL_FORMAT_STRING(const char *pszFormat),
              ...)

{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr || IS_PREFEFINED_ERROR_CTX(psCtx))
        return;
    const char *pszDebug = CPLGetConfigOption("CPL_DEBUG", nullptr);

    /* -------------------------------------------------------------------- */
    /*      Does this message pass our current criteria?                    */
    /* -------------------------------------------------------------------- */
    if (pszDebug == nullptr)
        return;

    if (!EQUAL(pszDebug, "ON") && !EQUAL(pszDebug, ""))
    {
        const size_t nLen = strlen(pszCategory);

        size_t i = 0;
        for (i = 0; pszDebug[i] != '\0'; i++)
        {
            if (EQUALN(pszCategory, pszDebug + i, nLen))
                break;
        }

        if (pszDebug[i] == '\0')
            return;
    }

    /* -------------------------------------------------------------------- */
    /*    Allocate a block for the error.                                   */
    /* -------------------------------------------------------------------- */
    const int ERROR_MAX = 25000;
    char *pszMessage = static_cast<char *>(VSIMalloc(ERROR_MAX));
    if (pszMessage == nullptr)
        return;

    /* -------------------------------------------------------------------- */
    /*      Dal -- always log a timestamp as the first part of the line     */
    /*      to ensure one is looking at what one should be looking at!      */
    /* -------------------------------------------------------------------- */

    pszMessage[0] = '\0';
#ifdef TIMESTAMP_DEBUG
    if (CPLGetConfigOption("CPL_TIMESTAMP", nullptr) != nullptr)
    {
        static struct CPLTimeVal tvStart;
        static const auto unused = CPLGettimeofday(&tvStart, nullptr);
        CPL_IGNORE_RET_VAL(unused);
        struct CPLTimeVal tv;
        CPLGettimeofday(&tv, nullptr);
        strcpy(pszMessage, "[");
        strcat(pszMessage, VSICTime(static_cast<unsigned long>(tv.tv_sec)));

        // On windows anyway, ctime puts a \n at the end, but I'm not
        // convinced this is standard behavior, so we'll get rid of it
        // carefully

        if (pszMessage[strlen(pszMessage) - 1] == '\n')
        {
            pszMessage[strlen(pszMessage) - 1] = 0;  // blow it out
        }
        CPLsnprintf(pszMessage + strlen(pszMessage),
                    ERROR_MAX - strlen(pszMessage),
                    "].%04d, %03.04f: ", static_cast<int>(tv.tv_usec / 100),
                    tv.tv_sec + tv.tv_usec * 1e-6 -
                        (tvStart.tv_sec + tvStart.tv_usec * 1e-6));
    }
#endif

/* -------------------------------------------------------------------- */
/*      Add the process memory size.                                    */
/* -------------------------------------------------------------------- */
#ifdef MEMORY_DEBUG
    char szVmSize[32] = {};
    CPLsprintf(szVmSize, "[VmSize: %d] ", CPLGetProcessMemorySize());
    strcat(pszMessage, szVmSize);
#endif

    /* -------------------------------------------------------------------- */
    /*      Add the category.                                               */
    /* -------------------------------------------------------------------- */
    strcat(pszMessage, pszCategory);
    strcat(pszMessage, ": ");

    /* -------------------------------------------------------------------- */
    /*      Format the application provided portion of the debug message.   */
    /* -------------------------------------------------------------------- */
    va_list args;
    va_start(args, pszFormat);

    CPLvsnprintf(pszMessage + strlen(pszMessage),
                 ERROR_MAX - strlen(pszMessage), pszFormat, args);

    va_end(args);

    /* -------------------------------------------------------------------- */
    /*      Obfuscate any password in error message                         */
    /* -------------------------------------------------------------------- */

    char *pszPassword = strstr(pszMessage, "password=");
    if (pszPassword != nullptr)
    {
        char *pszIter = pszPassword + strlen("password=");
        while (*pszIter != ' ' && *pszIter != '\0')
        {
            *pszIter = 'X';
            pszIter++;
        }
    }

    /* -------------------------------------------------------------------- */
    /*      Invoke the current error handler.                               */
    /* -------------------------------------------------------------------- */
    ApplyErrorHandler(psCtx, CE_Debug, CPLE_None, pszMessage);

    VSIFree(pszMessage);
}
#endif  // !WITHOUT_CPLDEBUG

/**********************************************************************
 *                          CPLErrorReset()
 **********************************************************************/

/**
 * Erase any traces of previous errors.
 *
 * This is normally used to ensure that an error which has been recovered
 * from does not appear to be still in play with high level functions.
 */

void CPL_STDCALL CPLErrorReset()
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr)
        return;
    if (IS_PREFEFINED_ERROR_CTX(psCtx))
    {
        int bMemoryError = FALSE;
        CPLSetTLSWithFreeFuncEx(
            CTLS_ERRORCONTEXT,
            reinterpret_cast<void *>(
                const_cast<CPLErrorContext *>(&sNoErrorContext)),
            nullptr, &bMemoryError);
        return;
    }

    psCtx->nLastErrNo = CPLE_None;
    psCtx->szLastErrMsg[0] = '\0';
    psCtx->eLastErrType = CE_None;
    psCtx->nErrorCounter = 0;
}

/**********************************************************************
 *                       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)
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr)
        return;
    if (IS_PREFEFINED_ERROR_CTX(psCtx))
    {
        int bMemoryError = FALSE;
        if (eErrClass == CE_None)
            CPLSetTLSWithFreeFuncEx(
                CTLS_ERRORCONTEXT,
                reinterpret_cast<void *>(
                    const_cast<CPLErrorContext *>(&sNoErrorContext)),
                nullptr, &bMemoryError);
        else if (eErrClass == CE_Warning)
            CPLSetTLSWithFreeFuncEx(
                CTLS_ERRORCONTEXT,
                reinterpret_cast<void *>(
                    const_cast<CPLErrorContext *>(&sWarningContext)),
                nullptr, &bMemoryError);
        else if (eErrClass == CE_Failure)
            CPLSetTLSWithFreeFuncEx(
                CTLS_ERRORCONTEXT,
                reinterpret_cast<void *>(
                    const_cast<CPLErrorContext *>(&sFailureContext)),
                nullptr, &bMemoryError);
        return;
    }

    psCtx->nLastErrNo = err_no;
    const size_t size = std::min(static_cast<size_t>(psCtx->nLastErrMsgMax - 1),
                                 strlen(pszMsg));
    char *pszLastErrMsg = CPLErrorContextGetString(psCtx);
    memcpy(pszLastErrMsg, pszMsg, size);
    pszLastErrMsg[size] = '\0';
    psCtx->eLastErrType = eErrClass;
}

/**********************************************************************
 *                          CPLGetLastErrorNo()
 **********************************************************************/

/**
 * Fetch the last error number.
 *
 * Fetches the last error number posted with CPLError(), that hasn't
 * been cleared by CPLErrorReset().  This is the error number, not the error
 * class.
 *
 * @return the error number of the last error to occur, or CPLE_None (0)
 * if there are no posted errors.
 */

CPLErrorNum CPL_STDCALL CPLGetLastErrorNo()
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr)
        return 0;

    return psCtx->nLastErrNo;
}

/**********************************************************************
 *                          CPLGetLastErrorType()
 **********************************************************************/

/**
 * Fetch the last error type.
 *
 * Fetches the last error type posted with CPLError(), that hasn't
 * been cleared by CPLErrorReset().  This is the error class, not the error
 * number.
 *
 * @return the error type of the last error to occur, or CE_None (0)
 * if there are no posted errors.
 */

CPLErr CPL_STDCALL CPLGetLastErrorType()
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr)
        return CE_None;

    return psCtx->eLastErrType;
}

/**********************************************************************
 *                          CPLGetLastErrorMsg()
 **********************************************************************/

/**
 * Get the last error message.
 *
 * Fetches the last error message posted with CPLError(), that hasn't
 * been cleared by CPLErrorReset().  The returned pointer is to an internal
 * string that should not be altered or freed.
 *
 * @return the last error message, or NULL if there is no posted error
 * message.
 */

const char *CPL_STDCALL CPLGetLastErrorMsg()
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr)
        return "";

    return psCtx->szLastErrMsg;
}

/**********************************************************************
 *                          CPLGetErrorCounter()
 **********************************************************************/

/**
 * Get the error counter
 *
 * Fetches the number of errors emitted in the current error context,
 * since the last call to CPLErrorReset()
 *
 * @return the error counter.
 * @since GDAL 2.3
 */

GUInt32 CPL_STDCALL CPLGetErrorCounter()
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr)
        return 0;

    return psCtx->nErrorCounter;
}

/************************************************************************/
/*                       CPLDefaultErrorHandler()                       */
/************************************************************************/

static FILE *fpLog = stderr;
static bool bLogInit = false;

static FILE *CPLfopenUTF8(const char *pszFilename, const char *pszAccess)
{
    FILE *f;
#ifdef _WIN32
    wchar_t *pwszFilename =
        CPLRecodeToWChar(pszFilename, CPL_ENC_UTF8, CPL_ENC_UCS2);
    wchar_t *pwszAccess =
        CPLRecodeToWChar(pszAccess, CPL_ENC_UTF8, CPL_ENC_UCS2);
    f = _wfopen(pwszFilename, pwszAccess);
    VSIFree(pwszFilename);
    VSIFree(pwszAccess);
#else
    f = fopen(pszFilename, pszAccess);
#endif
    return f;
}

/** Default error handler. */
void CPL_STDCALL CPLDefaultErrorHandler(CPLErr eErrClass, CPLErrorNum nError,
                                        const char *pszErrorMsg)

{
    static int nCount = 0;
    static int nMaxErrors = -1;
    static const char *pszErrorSeparator = ":";

    if (eErrClass != CE_Debug)
    {
        if (nMaxErrors == -1)
        {
            nMaxErrors =
                atoi(CPLGetConfigOption("CPL_MAX_ERROR_REPORTS", "1000"));
            // If running GDAL as a CustomBuild Command os MSBuild, "ERROR bla:"
            // is considered as failing the job. This is rarely the intended
            // behavior
            pszErrorSeparator = CPLGetConfigOption("CPL_ERROR_SEPARATOR", ":");
        }

        nCount++;
        if (nCount > nMaxErrors && nMaxErrors > 0)
            return;
    }

    if (!bLogInit)
    {
        bLogInit = true;

        fpLog = stderr;
        const char *pszLog = CPLGetConfigOption("CPL_LOG", nullptr);
        if (pszLog != nullptr)
        {
            const bool bAppend =
                CPLGetConfigOption("CPL_LOG_APPEND", nullptr) != nullptr;
            const char *pszAccess = bAppend ? "at" : "wt";
            fpLog = CPLfopenUTF8(pszLog, pszAccess);
            if (fpLog == nullptr)
                fpLog = stderr;
        }
    }

    if (eErrClass == CE_Debug)
        fprintf(fpLog, "%s\n", pszErrorMsg);
    else if (eErrClass == CE_Warning)
        fprintf(fpLog, "Warning %d: %s\n", nError, pszErrorMsg);
    else
        fprintf(fpLog, "ERROR %d%s %s\n", nError, pszErrorSeparator,
                pszErrorMsg);

    if (eErrClass != CE_Debug && nMaxErrors > 0 && nCount == nMaxErrors)
    {
        fprintf(fpLog,
                "More than %d errors or warnings have been reported. "
                "No more will be reported from now.\n",
                nMaxErrors);
    }

    fflush(fpLog);
}

/************************************************************************/
/*                        CPLQuietErrorHandler()                        */
/************************************************************************/

/** Error handler that does not do anything, except for debug messages. */
void CPL_STDCALL CPLQuietErrorHandler(CPLErr eErrClass, CPLErrorNum nError,
                                      const char *pszErrorMsg)

{
    if (eErrClass == CE_Debug)
        CPLDefaultErrorHandler(eErrClass, nError, pszErrorMsg);
}

/************************************************************************/
/*                       CPLLoggingErrorHandler()                       */
/************************************************************************/

/** Error handler that logs into the file defined by the CPL_LOG configuration
 * option, or stderr otherwise.
 */
void CPL_STDCALL CPLLoggingErrorHandler(CPLErr eErrClass, CPLErrorNum nError,
                                        const char *pszErrorMsg)

{
    if (!bLogInit)
    {
        bLogInit = true;

        CPLSetConfigOption("CPL_TIMESTAMP", "ON");

        const char *cpl_log = CPLGetConfigOption("CPL_LOG", nullptr);

        fpLog = stderr;
        if (cpl_log != nullptr && EQUAL(cpl_log, "OFF"))
        {
            fpLog = nullptr;
        }
        else if (cpl_log != nullptr)
        {
            size_t nPathLen = strlen(cpl_log) + 20;
            char *pszPath = static_cast<char *>(CPLMalloc(nPathLen));
            strcpy(pszPath, cpl_log);

            int i = 0;
            while ((fpLog = CPLfopenUTF8(pszPath, "rt")) != nullptr)
            {
                fclose(fpLog);

                // Generate sequenced log file names, inserting # before ext.
                if (strrchr(cpl_log, '.') == nullptr)
                {
                    snprintf(pszPath, nPathLen, "%s_%d%s", cpl_log, i++,
                             ".log");
                }
                else
                {
                    size_t pos = 0;
                    char *cpl_log_base = CPLStrdup(cpl_log);
                    pos = strcspn(cpl_log_base, ".");
                    if (pos > 0)
                    {
                        cpl_log_base[pos] = '\0';
                    }
                    snprintf(pszPath, nPathLen, "%s_%d%s", cpl_log_base, i++,
                             ".log");
                    CPLFree(cpl_log_base);
                }
            }

            fpLog = CPLfopenUTF8(pszPath, "wt");
            CPLFree(pszPath);
        }
    }

    if (fpLog == nullptr)
        return;

    if (eErrClass == CE_Debug)
        fprintf(fpLog, "%s\n", pszErrorMsg);
    else if (eErrClass == CE_Warning)
        fprintf(fpLog, "Warning %d: %s\n", nError, pszErrorMsg);
    else
        fprintf(fpLog, "ERROR %d: %s\n", nError, pszErrorMsg);

    fflush(fpLog);
}

/**********************************************************************
 *                      CPLTurnFailureIntoWarning()                   *
 **********************************************************************/

/** Whether failures should be turned into warnings.
 */
void CPLTurnFailureIntoWarning(int bOn)
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr || IS_PREFEFINED_ERROR_CTX(psCtx))
    {
        fprintf(stderr, "CPLTurnFailureIntoWarning() failed.\n");
        return;
    }
    psCtx->nFailureIntoWarning += (bOn) ? 1 : -1;
    if (psCtx->nFailureIntoWarning < 0)
    {
        CPLDebug("CPL", "Wrong nesting of CPLTurnFailureIntoWarning(TRUE) / "
                        "CPLTurnFailureIntoWarning(FALSE)");
    }
}

/**********************************************************************
 *                          CPLSetErrorHandlerEx()                    *
 **********************************************************************/

/**
 * Install custom error handle with user's data. This method is
 * essentially CPLSetErrorHandler with an added pointer to pUserData.
 * The pUserData is not returned in the CPLErrorHandler, however, and
 * must be fetched via CPLGetErrorHandlerUserData.
 *
 * @param pfnErrorHandlerNew new error handler function.
 * @param pUserData User data to carry along with the error context.
 * @return returns the previously installed error handler.
 */

CPLErrorHandler CPL_STDCALL
CPLSetErrorHandlerEx(CPLErrorHandler pfnErrorHandlerNew, void *pUserData)
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    if (psCtx == nullptr || IS_PREFEFINED_ERROR_CTX(psCtx))
    {
        fprintf(stderr, "CPLSetErrorHandlerEx() failed.\n");
        return nullptr;
    }

    if (psCtx->psHandlerStack != nullptr)
    {
        CPLDebug("CPL", "CPLSetErrorHandler() called with an error handler on "
                        "the local stack.  New error handler will not be used "
                        "immediately.");
    }

    CPLErrorHandler pfnOldHandler = nullptr;
    {
        CPLMutexHolderD(&hErrorMutex);

        pfnOldHandler = pfnErrorHandler;

        pfnErrorHandler = pfnErrorHandlerNew;

        pErrorHandlerUserData = pUserData;
    }

    return pfnOldHandler;
}

/**********************************************************************
 *                          CPLSetErrorHandler()                      *
 **********************************************************************/

/**
 * Install custom error handler.
 *
 * Allow the library's user to specify an error handler function.
 * A valid error handler is a C function with the following prototype:
 *
 * <pre>
 *     void MyErrorHandler(CPLErr eErrClass, int err_no, const char *msg)
 * </pre>
 *
 * Pass NULL to come back to the default behavior.  The default behavior
 * (CPLDefaultErrorHandler()) is to write the message to stderr.
 *
 * The msg will be a partially formatted error message not containing the
 * "ERROR %d:" portion emitted by the default handler.  Message formatting
 * is handled by CPLError() before calling the handler.  If the error
 * handler function is passed a CE_Fatal class error and returns, then
 * CPLError() will call abort(). Applications wanting to interrupt this
 * fatal behavior will have to use longjmp(), or a C++ exception to
 * indirectly exit the function.
 *
 * Another standard error handler is CPLQuietErrorHandler() which doesn't
 * make any attempt to report the passed error or warning messages but
 * will process debug messages via CPLDefaultErrorHandler.
 *
 * Note that error handlers set with CPLSetErrorHandler() apply to all
 * threads in an application, while error handlers set with CPLPushErrorHandler
 * are thread-local.  However, any error handlers pushed with
 * CPLPushErrorHandler (and not removed with CPLPopErrorHandler) take
 * precedence over the global error handlers set with CPLSetErrorHandler().
 * Generally speaking CPLSetErrorHandler() would be used to set a desired
 * global error handler, while CPLPushErrorHandler() would be used to install
 * a temporary local error handler, such as CPLQuietErrorHandler() to suppress
 * error reporting in a limited segment of code.
 *
 * @param pfnErrorHandlerNew new error handler function.
 * @return returns the previously installed error handler.
 */
CPLErrorHandler CPL_STDCALL
CPLSetErrorHandler(CPLErrorHandler pfnErrorHandlerNew)
{
    return CPLSetErrorHandlerEx(pfnErrorHandlerNew, nullptr);
}

/************************************************************************/
/*                        CPLPushErrorHandler()                         */
/************************************************************************/

/**
 * Push a new CPLError handler.
 *
 * This pushes a new error handler on the thread-local error handler
 * stack.  This handler will be used until removed with CPLPopErrorHandler().
 *
 * The CPLSetErrorHandler() docs have further information on how
 * CPLError handlers work.
 *
 * @param pfnErrorHandlerNew new error handler function.
 */

void CPL_STDCALL CPLPushErrorHandler(CPLErrorHandler pfnErrorHandlerNew)

{
    CPLPushErrorHandlerEx(pfnErrorHandlerNew, nullptr);
}

/************************************************************************/
/*                        CPLPushErrorHandlerEx()                       */
/************************************************************************/

/**
 * Push a new CPLError handler with user data on the error context.
 *
 * This pushes a new error handler on the thread-local error handler
 * stack.  This handler will be used until removed with CPLPopErrorHandler().
 * Obtain the user data back by using CPLGetErrorContext().
 *
 * The CPLSetErrorHandler() docs have further information on how
 * CPLError handlers work.
 *
 * @param pfnErrorHandlerNew new error handler function.
 * @param pUserData User data to put on the error context.
 */
void CPL_STDCALL CPLPushErrorHandlerEx(CPLErrorHandler pfnErrorHandlerNew,
                                       void *pUserData)

{
    CPLErrorContext *psCtx = CPLGetErrorContext();

    if (psCtx == nullptr || IS_PREFEFINED_ERROR_CTX(psCtx))
    {
        fprintf(stderr, "CPLPushErrorHandlerEx() failed.\n");
        return;
    }

    CPLErrorHandlerNode *psNode = static_cast<CPLErrorHandlerNode *>(
        CPLMalloc(sizeof(CPLErrorHandlerNode)));
    psNode->psNext = psCtx->psHandlerStack;
    psNode->pfnHandler = pfnErrorHandlerNew;
    psNode->pUserData = pUserData;
    psNode->bCatchDebug = true;
    psCtx->psHandlerStack = psNode;
}

/************************************************************************/
/*                         CPLPopErrorHandler()                         */
/************************************************************************/

/**
 * Pop error handler off stack.
 *
 * Discards the current error handler on the error handler stack, and restores
 * the one in use before the last CPLPushErrorHandler() call.  This method
 * has no effect if there are no error handlers on the current threads error
 * handler stack.
 */

void CPL_STDCALL CPLPopErrorHandler()

{
    CPLErrorContext *psCtx = CPLGetErrorContext();

    if (psCtx == nullptr || IS_PREFEFINED_ERROR_CTX(psCtx))
    {
        fprintf(stderr, "CPLPopErrorHandler() failed.\n");
        return;
    }

    if (psCtx->psHandlerStack != nullptr)
    {
        CPLErrorHandlerNode *psNode = psCtx->psHandlerStack;

        psCtx->psHandlerStack = psNode->psNext;
        VSIFree(psNode);
    }
}

/************************************************************************/
/*                 CPLSetCurrentErrorHandlerCatchDebug()                */
/************************************************************************/

/**
 * Set if the current error handler should intercept debug messages, or if
 * they should be processed by the previous handler.
 *
 * By default when installing a custom error handler, this one intercepts
 * debug messages. In some cases, this might not be desirable and the user
 * would prefer that the previous installed handler (or the default one if no
 * previous installed handler exists in the stack) deal with it. In which
 * case, this function should be called with bCatchDebug = FALSE.
 *
 * @param bCatchDebug FALSE if the current error handler should not intercept
 * debug messages
 * @since GDAL 2.1
 */

void CPL_STDCALL CPLSetCurrentErrorHandlerCatchDebug(int bCatchDebug)
{
    CPLErrorContext *psCtx = CPLGetErrorContext();

    if (psCtx == nullptr || IS_PREFEFINED_ERROR_CTX(psCtx))
    {
        fprintf(stderr, "CPLSetCurrentErrorHandlerCatchDebug() failed.\n");
        return;
    }

    if (psCtx->psHandlerStack != nullptr)
        psCtx->psHandlerStack->bCatchDebug = CPL_TO_BOOL(bCatchDebug);
    else
        gbCatchDebug = CPL_TO_BOOL(bCatchDebug);
}

/************************************************************************/
/*                             _CPLAssert()                             */
/*                                                                      */
/*      This function is called only when an assertion fails.           */
/************************************************************************/

/**
 * Report failure of a logical assertion.
 *
 * Applications would normally use the CPLAssert() macro which expands
 * into code calling _CPLAssert() only if the condition fails.  _CPLAssert()
 * will generate a CE_Fatal error call to CPLError(), indicating the file
 * name, and line number of the failed assertion, as well as containing
 * the assertion itself.
 *
 * There is no reason for application code to call _CPLAssert() directly.
 */

void CPL_STDCALL _CPLAssert(const char *pszExpression, const char *pszFile,
                            int iLine)

{
    CPLError(CE_Fatal, CPLE_AssertionFailed,
             "Assertion `%s' failed "
             "in file `%s', line %d",
             pszExpression, pszFile, iLine);

    // Just to please compiler so it is aware the function does not return.
    abort();
}

/************************************************************************/
/*                       CPLCleanupErrorMutex()                         */
/************************************************************************/

void CPLCleanupErrorMutex()
{
    if (hErrorMutex != nullptr)
    {
        CPLDestroyMutex(hErrorMutex);
        hErrorMutex = nullptr;
    }
    if (fpLog != nullptr && fpLog != stderr)
    {
        fclose(fpLog);
        fpLog = nullptr;
        bLogInit = false;
    }
}

bool CPLIsDefaultErrorHandlerAndCatchDebug()
{
    CPLErrorContext *psCtx = CPLGetErrorContext();
    return (psCtx == nullptr || psCtx->psHandlerStack == nullptr) &&
           gbCatchDebug && pfnErrorHandler == CPLDefaultErrorHandler;
}

/************************************************************************/
/*                       CPLErrorHandlerAccumulator()                   */
/************************************************************************/

static void CPL_STDCALL CPLErrorHandlerAccumulator(CPLErr eErr, CPLErrorNum no,
                                                   const char *msg)
{
    std::vector<CPLErrorHandlerAccumulatorStruct> *paoErrors =
        static_cast<std::vector<CPLErrorHandlerAccumulatorStruct> *>(
            CPLGetErrorHandlerUserData());
    paoErrors->push_back(CPLErrorHandlerAccumulatorStruct(eErr, no, msg));
}

void CPLInstallErrorHandlerAccumulator(
    std::vector<CPLErrorHandlerAccumulatorStruct> &aoErrors)
{
    CPLPushErrorHandlerEx(CPLErrorHandlerAccumulator, &aoErrors);
}

void CPLUninstallErrorHandlerAccumulator()
{
    CPLPopErrorHandler();
}