Coverage Report

Created: 2026-04-01 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tesseract/src/ccutil/errcode.cpp
Line
Count
Source
1
/**********************************************************************
2
 * File:        errcode.cpp  (Formerly error.c)
3
 * Description: Generic error handler function
4
 * Author:      Ray Smith
5
 *
6
 * (C) Copyright 1989, Hewlett-Packard Ltd.
7
 ** Licensed under the Apache License, Version 2.0 (the "License");
8
 ** you may not use this file except in compliance with the License.
9
 ** You may obtain a copy of the License at
10
 ** http://www.apache.org/licenses/LICENSE-2.0
11
 ** Unless required by applicable law or agreed to in writing, software
12
 ** distributed under the License is distributed on an "AS IS" BASIS,
13
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 ** See the License for the specific language governing permissions and
15
 ** limitations under the License.
16
 *
17
 **********************************************************************/
18
19
#include "errcode.h"
20
21
#include <cstdarg>
22
#include <cstdio>
23
#include <cstdlib>
24
#include <cstring>
25
#include <iostream> // for std::cerr
26
#include <sstream>  // for std::stringstream
27
28
namespace tesseract {
29
30
constexpr ERRCODE BADERRACTION("Illegal error action");
31
#define MAX_MSG 1024
32
33
/**********************************************************************
34
 * error
35
 *
36
 * Print an error message and continue, exit or abort according to action.
37
 * Makes use of error messages and numbers in a common place.
38
 *
39
 **********************************************************************/
40
void ERRCODE::error(         // handle error
41
    const char *caller,      // name of caller
42
    TessErrorLogCode action, // action to take
43
    const char *format, ...  // special message
44
0
    ) const {
45
0
  va_list args; // variable args
46
0
  std::stringstream msg;
47
48
0
  if (caller != nullptr) {
49
    // name of caller
50
0
    msg << caller << ':';
51
0
  }
52
  // actual message
53
0
  msg << "Error:" << message;
54
0
  if (format != nullptr) {
55
0
    char str[MAX_MSG];
56
0
    va_start(args, format); // variable list
57
    // print remainder
58
0
    std::vsnprintf(str, sizeof(str), format, args);
59
    // ensure termination
60
0
    str[sizeof(str) - 1] = '\0';
61
0
    va_end(args);
62
0
    msg << ':' << str;
63
0
  }
64
65
0
  std::cerr << msg.str() << '\n';
66
67
0
  switch (action) {
68
0
    case DBG:
69
0
    case TESSLOG:
70
0
      return; // report only
71
0
    case TESSEXIT:
72
0
    case ABORT:
73
#if !defined(NDEBUG)
74
      // Create a deliberate abnormal exit as the stack trace is more useful
75
      // that way. This is done only in debug builds, because the
76
      // error message "segmentation fault" confuses most normal users.
77
#  if defined(__GNUC__)
78
      __builtin_trap();
79
#  else
80
      *reinterpret_cast<int *>(0) = 0;
81
#  endif
82
#endif
83
0
      abort();
84
0
    default:
85
0
      BADERRACTION.error("error", ABORT);
86
0
  }
87
0
}
88
89
} // namespace tesseract