tesseract  4.0.0-1-g2a2b
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  * Created: Tue May 1 16:28:39 BST 1990
6  *
7  * (C) Copyright 1989, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstdarg>
23 #include <cstring>
24 #include "tprintf.h"
25 #include "errcode.h"
26 
27 const ERRCODE BADERRACTION = "Illegal error action";
28 #define MAX_MSG 1024
29 
30 /**********************************************************************
31  * error
32  *
33  * Print an error message and continue, exit or abort according to action.
34  * Makes use of error messages and numbers in a common place.
35  *
36  **********************************************************************/
37 void ERRCODE::error( // handle error
38 const char *caller, // name of caller
39 TessErrorLogCode action, // action to take
40 const char *format, ... // special message
41 ) const {
42  va_list args; // variable args
43  char msg[MAX_MSG];
44  char *msgptr = msg;
45 
46  if (caller != nullptr)
47  //name of caller
48  msgptr += sprintf (msgptr, "%s:", caller);
49  //actual message
50  msgptr += sprintf (msgptr, "Error:%s", message);
51  if (format != nullptr) {
52  msgptr += sprintf (msgptr, ":");
53  va_start(args, format); //variable list
54  #ifdef _WIN32
55  //print remainder
56  msgptr += _vsnprintf (msgptr, MAX_MSG - 2 - (msgptr - msg), format, args);
57  msg[MAX_MSG - 2] = '\0'; //ensure termination
58  strcat (msg, "\n");
59  #else
60  //print remainder
61  msgptr += vsprintf (msgptr, format, args);
62  //no specific
63  msgptr += sprintf (msgptr, "\n");
64  #endif
65  va_end(args);
66  }
67  else
68  //no specific
69  msgptr += sprintf (msgptr, "\n");
70 
71  // %s is needed here so msg is printed correctly!
72  fprintf(stderr, "%s", msg);
73 
74  switch (action) {
75  case DBG:
76  case TESSLOG:
77  return; //report only
78  case TESSEXIT:
79  //err_exit();
80  case ABORT:
81 #if !defined(NDEBUG)
82  // Create a deliberate segv as the stack trace is more useful that way.
83  // This is done only in debug builds, because the error message
84  // "segmentation fault" confuses most normal users.
85  *reinterpret_cast<int*>(0) = 0;
86 #endif
87  abort();
88  default:
89  BADERRACTION.error ("error", ABORT, nullptr);
90  }
91 }
const ERRCODE BADERRACTION
Definition: errcode.cpp:27
Definition: errcode.h:27
Definition: errcode.h:30
#define MAX_MSG
Definition: errcode.cpp:28
TessErrorLogCode
Definition: errcode.h:26
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:37