tesseract
3.03
|
00001 /********************************************************************** 00002 * File: errcode.c (Formerly error.c) 00003 * Description: Generic error handler function 00004 * Author: Ray Smith 00005 * Created: Tue May 1 16:28:39 BST 1990 00006 * 00007 * (C) Copyright 1989, Hewlett-Packard Ltd. 00008 ** Licensed under the Apache License, Version 2.0 (the "License"); 00009 ** you may not use this file except in compliance with the License. 00010 ** You may obtain a copy of the License at 00011 ** http://www.apache.org/licenses/LICENSE-2.0 00012 ** Unless required by applicable law or agreed to in writing, software 00013 ** distributed under the License is distributed on an "AS IS" BASIS, 00014 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 ** See the License for the specific language governing permissions and 00016 ** limitations under the License. 00017 * 00018 **********************************************************************/ 00019 00020 #include <stdio.h> 00021 #include <stdlib.h> 00022 #include <stdarg.h> 00023 #include <string.h> 00024 #ifdef __UNIX__ 00025 #include <signal.h> 00026 #endif 00027 #include "tprintf.h" 00028 #include "errcode.h" 00029 00030 const ERRCODE BADERRACTION = "Illegal error action"; 00031 #define MAX_MSG 1024 00032 00033 /********************************************************************** 00034 * error 00035 * 00036 * Print an error message and continue, exit or abort according to action. 00037 * Makes use of error messages and numbers in a common place. 00038 * 00039 **********************************************************************/ 00040 void ERRCODE::error( // handle error 00041 const char *caller, // name of caller 00042 TessErrorLogCode action, // action to take 00043 const char *format, ... // special message 00044 ) const { 00045 va_list args; // variable args 00046 char msg[MAX_MSG]; 00047 char *msgptr = msg; 00048 00049 if (caller != NULL) 00050 //name of caller 00051 msgptr += sprintf (msgptr, "%s:", caller); 00052 //actual message 00053 msgptr += sprintf (msgptr, "Error:%s", message); 00054 if (format != NULL) { 00055 msgptr += sprintf (msgptr, ":"); 00056 va_start(args, format); //variable list 00057 #ifdef _WIN32 00058 //print remainder 00059 msgptr += _vsnprintf (msgptr, MAX_MSG - 2 - (msgptr - msg), format, args); 00060 msg[MAX_MSG - 2] = '\0'; //ensure termination 00061 strcat (msg, "\n"); 00062 #else 00063 //print remainder 00064 msgptr += vsprintf (msgptr, format, args); 00065 //no specific 00066 msgptr += sprintf (msgptr, "\n"); 00067 #endif 00068 va_end(args); 00069 } 00070 else 00071 //no specific 00072 msgptr += sprintf (msgptr, "\n"); 00073 00074 // %s is needed here so msg is printed correctly! 00075 fprintf(stderr, "%s", msg); 00076 00077 int* p = NULL; 00078 switch (action) { 00079 case DBG: 00080 case TESSLOG: 00081 return; //report only 00082 case TESSEXIT: 00083 //err_exit(); 00084 case ABORT: 00085 // Create a deliberate segv as the stack trace is more useful that way. 00086 if (!*p) 00087 abort(); 00088 default: 00089 BADERRACTION.error ("error", ABORT, NULL); 00090 } 00091 }