Tesseract  3.02
tesseract-ocr/ccutil/errcode.cpp
Go to the documentation of this file.
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          "mfcpch.h"     //precompiled headers
00021 #include          <stdio.h>
00022 #include          <stdlib.h>
00023 #include          <stdarg.h>
00024 #include          <string.h>
00025 #ifdef __UNIX__
00026 #include          <signal.h>
00027 #endif
00028 #include          "tprintf.h"
00029 #include          "errcode.h"
00030 
00031 const ERRCODE BADERRACTION = "Illegal error action";
00032 #define MAX_MSG       1024
00033 
00034 /**********************************************************************
00035  * error
00036  *
00037  * Print an error message and continue, exit or abort according to action.
00038  * Makes use of error messages and numbers in a common place.
00039  *
00040  **********************************************************************/
00041 void ERRCODE::error(             // handle error
00042 const char *caller,              // name of caller
00043 TessErrorLogCode action,         // action to take
00044 const char *format, ...          // special message
00045 ) const {
00046   va_list args;                  // variable args
00047   char msg[MAX_MSG];
00048   char *msgptr = msg;
00049 
00050   if (caller != NULL)
00051                                  //name of caller
00052     msgptr += sprintf (msgptr, "%s:", caller);
00053                                  //actual message
00054   msgptr += sprintf (msgptr, "Error:%s", message);
00055   if (format != NULL) {
00056     msgptr += sprintf (msgptr, ":");
00057     va_start(args, format);  //variable list
00058     #ifdef _WIN32
00059                                  //print remainder
00060     msgptr += _vsnprintf (msgptr, MAX_MSG - 2 - (msgptr - msg), format, args);
00061     msg[MAX_MSG - 2] = '\0';     //ensure termination
00062     strcat (msg, "\n");
00063     #else
00064                                  //print remainder
00065     msgptr += vsprintf (msgptr, format, args);
00066                                  //no specific
00067     msgptr += sprintf (msgptr, "\n");
00068     #endif
00069     va_end(args);
00070   }
00071   else
00072                                  //no specific
00073     msgptr += sprintf (msgptr, "\n");
00074 
00075   fprintf(stderr, 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 }