1 /**********************************************************************
2  * File:        errcode.h  (Formerly error.h)
3  * Description: Header file for generic error handler class
4  * Author:      Ray Smith
5  *
6  * (C) Copyright 1990, Hewlett-Packard Ltd.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 #ifndef ERRCODE_H
20 #define ERRCODE_H
21 
22 #include <tesseract/export.h> // for TESS_API
23 
24 namespace tesseract {
25 
26 /*Control parameters for error()*/
27 enum TessErrorLogCode {
28   DBG = -1,     /*log without alert */
29   TESSLOG = 0,  /*alert user */
30   TESSEXIT = 1, /*exit after error */
31   ABORT = 2     /*abort after error */
32 };
33 
34 /* Explicit Error Abort codes */
35 #define NO_ABORT_CODE 0
36 #define LIST_ABORT 1
37 #define MEMORY_ABORT 2
38 #define FILE_ABORT 3
39 
40 #if !defined(__GNUC__) && !defined(__attribute__)
41 # define __attribute__(attr) // compiler without support for __attribute__
42 #endif
43 
44 class TESS_API ERRCODE { // error handler class
45   const char *message;   // error message
46 public:
47   void error(                  // error print function
48       const char *caller,      // function location
49       TessErrorLogCode action, // action to take
50       const char *format, ...  // fprintf format
51   ) const __attribute__((format(printf, 4, 5)));
ERRCODE(const char * string)52   constexpr ERRCODE(const char *string) : message(string) {} // initialize with string
53 };
54 
55 constexpr ERRCODE ASSERT_FAILED("Assert failed");
56 
57 #define DO_NOTHING static_cast<void>(0)
58 
59 #define ASSERT_HOST(x) \
60   (x) ? DO_NOTHING : ASSERT_FAILED.error(#x, ABORT, "in file %s, line %d", __FILE__, __LINE__)
61 
62 #define ASSERT_HOST_MSG(x, ...)                                                \
63   if (!(x)) {                                                                  \
64     tprintf(__VA_ARGS__);                                                      \
65     ASSERT_FAILED.error(#x, ABORT, "in file %s, line %d", __FILE__, __LINE__); \
66   }
67 
68 } // namespace tesseract
69 
70 #endif
71