Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 "mfcpch.h" //precompiled headers
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #ifdef __UNIX__
26 #include <signal.h>
27 #endif
28 #include "tprintf.h"
29 #include "errcode.h"
30
31 const ERRCODE BADERRACTION = "Illegal error action";
32 #define MAX_MSG 1024
33
34 /**********************************************************************
35 * error
36 *
37 * Print an error message and continue, exit or abort according to action.
38 * Makes use of error messages and numbers in a common place.
39 *
40 **********************************************************************/
41 void ERRCODE::error( // handle error
42 const char *caller, // name of caller
43 TessErrorLogCode action, // action to take
44 const char *format, ... // special message
45 ) const {
46 va_list args; // variable args
47 char msg[MAX_MSG];
48 char *msgptr = msg;
49
50 if (caller != NULL)
51 //name of caller
52 msgptr += sprintf (msgptr, "%s:", caller);
53 //actual message
54 msgptr += sprintf (msgptr, "Error:%s", message);
55 if (format != NULL) {
56 msgptr += sprintf (msgptr, ":");
57 va_start(args, format); //variable list
58 #ifdef _WIN32
59 //print remainder
60 msgptr += _vsnprintf (msgptr, MAX_MSG - 2 - (msgptr - msg), format, args);
61 msg[MAX_MSG - 2] = '\0'; //ensure termination
62 strcat (msg, "\n");
63 #else
64 //print remainder
65 msgptr += vsprintf (msgptr, format, args);
66 //no specific
67 msgptr += sprintf (msgptr, "\n");
68 #endif
69 va_end(args);
70 }
71 else
72 //no specific
73 msgptr += sprintf (msgptr, "\n");
74
75 // %s is needed here so msg is printed correctly!
76 fprintf(stderr, "%s", msg);
77
78 int* p = NULL;
79 switch (action) {
80 case DBG:
81 case TESSLOG:
82 return; //report only
83 case TESSEXIT:
84 //err_exit();
85 case ABORT:
86 // Create a deliberate segv as the stack trace is more useful that way.
87 if (!*p)
88 abort();
89 default:
90 BADERRACTION.error ("error", ABORT, NULL);
91 }
92 }