Use fmtlib for ERRCODE::error

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2021-12-22 21:03:36 +01:00
parent 85ebc18ec0
commit 19cf785bfb
3 changed files with 42 additions and 51 deletions

View File

@ -347,6 +347,7 @@ endif
# Rules for src/ccutil
libtesseract_ccutil_la_CPPFLAGS = $(AM_CPPFLAGS)
libtesseract_ccutil_la_CPPFLAGS += -I$(top_srcdir)/src/fmt/include
libtesseract_ccutil_la_CPPFLAGS += $(libarchive_CFLAGS)
if !NO_TESSDATA_PREFIX
libtesseract_ccutil_la_CPPFLAGS += -DTESSDATA_PREFIX='"@datadir@"'
@ -1162,6 +1163,7 @@ unittest_CPPFLAGS += -I$(top_srcdir)/src/classify
unittest_CPPFLAGS += -I$(top_srcdir)/src/cutil
unittest_CPPFLAGS += -I$(top_srcdir)/src/dict
unittest_CPPFLAGS += -I$(top_srcdir)/src/display
unittest_CPPFLAGS += -I$(top_srcdir)/src/fmt/include
unittest_CPPFLAGS += -I$(top_srcdir)/src/lstm
unittest_CPPFLAGS += -I$(top_srcdir)/src/textord
unittest_CPPFLAGS += -I$(top_srcdir)/unittest/base

View File

@ -18,61 +18,14 @@
#include "errcode.h"
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
namespace tesseract {
constexpr ERRCODE BADERRACTION("Illegal error action");
#define MAX_MSG 1024
/**********************************************************************
* error
*
* Print an error message and continue, exit or abort according to action.
* Makes use of error messages and numbers in a common place.
*
**********************************************************************/
void ERRCODE::error( // handle error
const char *caller, // name of caller
TessErrorLogCode action, // action to take
const char *format, ... // special message
) const {
va_list args; // variable args
char msg[MAX_MSG];
char *msgptr = msg;
if (caller != nullptr) {
// name of caller
msgptr += sprintf(msgptr, "%s:", caller);
}
// actual message
msgptr += sprintf(msgptr, "Error:%s", message);
if (format != nullptr) {
msgptr += sprintf(msgptr, ":");
va_start(args, format); // variable list
#ifdef _WIN32
// print remainder
msgptr += _vsnprintf(msgptr, MAX_MSG - 2 - (msgptr - msg), format, args);
msg[MAX_MSG - 2] = '\0'; // ensure termination
strcat(msg, "\n");
#else
// print remainder
msgptr += vsprintf(msgptr, format, args);
// no specific
msgptr += sprintf(msgptr, "\n");
#endif
va_end(args);
} else {
// no specific
msgptr += sprintf(msgptr, "\n");
}
// %s is needed here so msg is printed correctly!
fprintf(stderr, "%s", msg);
static void error_action(TessErrorLogCode action) {
switch (action) {
case DBG:
case TESSLOG:
@ -95,8 +48,31 @@ void ERRCODE::error( // handle error
}
}
/**********************************************************************
* error
*
* Print an error message and continue, exit or abort according to action.
* Makes use of error messages and numbers in a common place.
*
**********************************************************************/
void ERRCODE::error(const char *caller, TessErrorLogCode action) const {
error(caller, action, nullptr);
if (caller != nullptr) {
// name of caller
fprintf(stderr, "%s\n", fmt::format("{}:Error:{}", caller, message).c_str());
} else {
fprintf(stderr, "%s\n", fmt::format("Error:{}", message).c_str());
}
error_action(action);
}
void ERRCODE::verror(const char *caller, TessErrorLogCode action, fmt::string_view format, fmt::format_args args) const {
if (caller != nullptr) {
// name of caller
fprintf(stderr, "%s\n", fmt::format("{}:Error:{}:{}", caller, message, fmt::vformat(format, args)).c_str());
} else {
fprintf(stderr, "%s\n", fmt::format("Error:{}:{}", message, fmt::vformat(format, args)).c_str());
}
error_action(action);
}
} // namespace tesseract

View File

@ -19,6 +19,7 @@
#ifndef ERRCODE_H
#define ERRCODE_H
#include <fmt/format.h> // for fmt
#include <tesseract/export.h> // for TESS_API
namespace tesseract {
@ -34,15 +35,27 @@ enum TessErrorLogCode {
#if !defined(__GNUC__) && !defined(__attribute__)
# define __attribute__(attr) // compiler without support for __attribute__
#endif
#if 0
/* Explicit Error Abort codes */
#define NO_ABORT_CODE 0
#define LIST_ABORT 1
#define MEMORY_ABORT 2
#define FILE_ABORT 3
#endif
class TESS_API ERRCODE { // error handler class
const char *message; // error message
public:
void verror(const char *caller, TessErrorLogCode action, fmt::string_view format, fmt::format_args args) const;
template <typename S, typename... Args>
void error( // error print function
const char *caller, // function location
TessErrorLogCode action, // action to take
const char *format, ... // fprintf format
) const __attribute__((format(printf, 4, 5)));
const S *format,
Args&&... args
) const {
verror(caller, action, format, fmt::make_args_checked<Args...>(format, args...));
}
void error(const char *caller, TessErrorLogCode action) const;
constexpr ERRCODE(const char *string) : message(string) {} // initialize with string
};