tesseract  5.0.0
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 
26 namespace tesseract {
27 
28 constexpr ERRCODE BADERRACTION("Illegal error action");
29 #define MAX_MSG 1024
30 
31 /**********************************************************************
32  * error
33  *
34  * Print an error message and continue, exit or abort according to action.
35  * Makes use of error messages and numbers in a common place.
36  *
37  **********************************************************************/
38 void ERRCODE::error( // handle error
39  const char *caller, // name of caller
40  TessErrorLogCode action, // action to take
41  const char *format, ... // special message
42  ) const {
43  va_list args; // variable args
44  char msg[MAX_MSG];
45  char *msgptr = msg;
46 
47  if (caller != nullptr) {
48  // name of caller
49  msgptr += sprintf(msgptr, "%s:", caller);
50  }
51  // actual message
52  msgptr += sprintf(msgptr, "Error:%s", message);
53  if (format != nullptr) {
54  msgptr += sprintf(msgptr, ":");
55  va_start(args, format); // variable list
56 #ifdef _WIN32
57  // print remainder
58  msgptr += _vsnprintf(msgptr, MAX_MSG - 2 - (msgptr - msg), format, args);
59  msg[MAX_MSG - 2] = '\0'; // ensure termination
60  strcat(msg, "\n");
61 #else
62  // print remainder
63  msgptr += vsprintf(msgptr, format, args);
64  // no specific
65  msgptr += sprintf(msgptr, "\n");
66 #endif
67  va_end(args);
68  } else {
69  // no specific
70  msgptr += sprintf(msgptr, "\n");
71  }
72 
73  // %s is needed here so msg is printed correctly!
74  fprintf(stderr, "%s", msg);
75 
76  switch (action) {
77  case DBG:
78  case TESSLOG:
79  return; // report only
80  case TESSEXIT:
81  case ABORT:
82 #if !defined(NDEBUG)
83  // Create a deliberate abnormal exit as the stack trace is more useful
84  // that way. This is done only in debug builds, because the
85  // error message "segmentation fault" confuses most normal users.
86 # if defined(__GNUC__)
87  __builtin_trap();
88 # else
89  *reinterpret_cast<int *>(0) = 0;
90 # endif
91 #endif
92  abort();
93  default:
94  BADERRACTION.error("error", ABORT, nullptr);
95  }
96 }
97 
98 } // namespace tesseract
#define MAX_MSG
Definition: errcode.cpp:29
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
void error(const char *caller, TessErrorLogCode action, const char *format,...) const __attribute__((format(printf
Definition: errcode.cpp:38