tesseract v5.3.3.20231005
errcode.cpp
Go to the documentation of this file.
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
28namespace tesseract {
29
30constexpr 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 **********************************************************************/
40void ERRCODE::error( // handle error
41 const char *caller, // name of caller
42 TessErrorLogCode action, // action to take
43 const char *format, ... // special message
44 ) const {
45 va_list args; // variable args
46 std::stringstream msg;
47
48 if (caller != nullptr) {
49 // name of caller
50 msg << caller << ':';
51 }
52 // actual message
53 msg << "Error:" << message;
54 if (format != nullptr) {
55 char str[MAX_MSG];
56 va_start(args, format); // variable list
57 // print remainder
58 std::vsnprintf(str, sizeof(str), format, args);
59 // ensure termination
60 str[sizeof(str) - 1] = '\0';
61 va_end(args);
62 msg << ':' << str;
63 }
64
65 std::cerr << msg.str() << '\n';
66
67 switch (action) {
68 case DBG:
69 case TESSLOG:
70 return; // report only
71 case TESSEXIT:
72 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 abort();
84 default:
85 BADERRACTION.error("error", ABORT);
86 }
87}
88
89void ERRCODE::error(const char *caller, TessErrorLogCode action) const {
90 error(caller, action, nullptr);
91}
92
93} // namespace tesseract
#define MAX_MSG
Definition: errcode.cpp:31
constexpr ERRCODE BADERRACTION("Illegal error action")
TessErrorLogCode
Definition: errcode.h:27
@ ABORT
Definition: errcode.h:31
@ TESSLOG
Definition: errcode.h:29
@ TESSEXIT
Definition: errcode.h:30
action
Definition: upload.py:408
void error(const char *caller, TessErrorLogCode action, const char *format,...) const __attribute__((format(gnu_printf
Definition: errcode.cpp:40