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