All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
errcode.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: errcode.c (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 <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #ifdef __UNIX__
25 #include <signal.h>
26 #endif
27 #include "tprintf.h"
28 #include "errcode.h"
29 
30 const 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 ) const {
45  va_list args; // variable args
46  char msg[MAX_MSG];
47  char *msgptr = msg;
48 
49  if (caller != NULL)
50  //name of caller
51  msgptr += sprintf (msgptr, "%s:", caller);
52  //actual message
53  msgptr += sprintf (msgptr, "Error:%s", message);
54  if (format != NULL) {
55  msgptr += sprintf (msgptr, ":");
56  va_start(args, format); //variable list
57  #ifdef _WIN32
58  //print remainder
59  msgptr += _vsnprintf (msgptr, MAX_MSG - 2 - (msgptr - msg), format, args);
60  msg[MAX_MSG - 2] = '\0'; //ensure termination
61  strcat (msg, "\n");
62  #else
63  //print remainder
64  msgptr += vsprintf (msgptr, format, args);
65  //no specific
66  msgptr += sprintf (msgptr, "\n");
67  #endif
68  va_end(args);
69  }
70  else
71  //no specific
72  msgptr += sprintf (msgptr, "\n");
73 
74  // %s is needed here so msg is printed correctly!
75  fprintf(stderr, "%s", msg);
76 
77  int* p = NULL;
78  switch (action) {
79  case DBG:
80  case TESSLOG:
81  return; //report only
82  case TESSEXIT:
83  //err_exit();
84  case ABORT:
85  // Create a deliberate segv as the stack trace is more useful that way.
86  if (!*p)
87  abort();
88  default:
89  BADERRACTION.error ("error", ABORT, NULL);
90  }
91 }
#define MAX_MSG
Definition: errcode.cpp:31
Definition: errcode.h:30
Definition: errcode.h:27
const ERRCODE BADERRACTION
Definition: errcode.cpp:30
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
Definition: errcode.cpp:40
#define NULL
Definition: host.h:144
TessErrorLogCode
Definition: errcode.h:26