From 2fc9887498ee31fbe47ee0a13f8123c4bba1defe Mon Sep 17 00:00:00 2001 From: Nicolas Denoyelle Date: Mon, 25 Mar 2019 15:39:36 -0500 Subject: [PATCH 1/2] add error codes --- include/Makefile.am | 3 ++- include/aml/utils/error.h | 43 +++++++++++++++++++++++++++++++++++++++ src/utils/Makefile.am | 7 ++++--- src/utils/error.c | 29 ++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 include/aml/utils/error.h create mode 100644 src/utils/error.c diff --git a/include/Makefile.am b/include/Makefile.am index c0e7a1e..ab67dcf 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -15,5 +15,6 @@ include_amlutilsdir=$(includedir)/aml/utils include_amlutils_HEADERS = \ aml/utils/vector.h \ aml/utils/bitmap.h \ - aml/utils/version.h + aml/utils/version.h \ + aml/utils/error.h diff --git a/include/aml/utils/error.h b/include/aml/utils/error.h new file mode 100644 index 0000000..15cce4e --- /dev/null +++ b/include/aml/utils/error.h @@ -0,0 +1,43 @@ +#ifndef AML_ERROR_H +#define AML_ERROR_H + +#include + +/** + * Variable set by aml function calls. aml_errno should be checked after an aml function call + * returning an error prior to any other aml call that may overwrite it. + **/ +extern int aml_errno; + +/** + * Get a string description of an aml error. + * "errno": the aml error number. + * Returns a on stack string with error description. + **/ +const char* aml_strerror(const int errno); + +/** + * Print error on standard error output. + * "msg": A message to prepend to error message. + **/ +void aml_perror(const char * msg); + +#define AML_SUCCESS 0 /* Generic value for success */ +#define AML_FAILURE -1 /* Generic value for failure */ + +/************************************ + * Area error codes -2 .. -32 + ************************************/ + +#define AML_AREA_EINVAL -2 /* Invalid argument provided */ +#define AML_AREA_ENOTSUP -3 /* Function not implemented for this type of area */ +#define AML_AREA_ENOMEM -4 /* Allocation failed */ +#define AML_AREA_EDOM -5 /* One arguent is out of allowed bounds */ + +/************************************ + * error bound + ************************************/ + +#define AML_ERROR_MAX -6 /* Last error */ + +#endif diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index 8f2a0ba..f71d1f7 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -4,9 +4,10 @@ AM_CPPFLAGS=$(AM_CFLAGS) noinst_LTLIBRARIES=libamlutils.la libamlutils_la_SOURCES=\ - vector.c \ - bitmap.c + vector.c \ + bitmap.c \ + error.c -libamlutils_la_CFLAGS=$(AM_CFLAGS) +libamlutils_la_CFLAGS=$(AM_CFLAGS) -Wno-overflow libamlutils_la_LDFLAGS= diff --git a/src/utils/error.c b/src/utils/error.c new file mode 100644 index 0000000..46fa9e1 --- /dev/null +++ b/src/utils/error.c @@ -0,0 +1,29 @@ +#include "aml/utils/error.h" + +const char* +aml_strerror(const int errno) +{ + switch(errno){ + case AML_SUCCESS: + return "aml success! If this is unexpected, check that this is called right after aml function returning an error."; + case AML_FAILURE: + return "aml function call failed (generic error)."; + case AML_AREA_EINVAL: + return "aml_area function called with invalid argument(s)."; + case AML_AREA_ENOTSUP: + return "aml_area function is not implemented."; + case AML_AREA_ENOMEM: + return "Not enough memory to fulfill aml_area function call."; + case AML_AREA_EDOM: + return "An argument is out possible bounds for this function call."; + default: + return "Invalid aml error code."; + } +} + +void +aml_perror(const char * msg) +{ + fprintf(stderr, "%s:%s\n", msg, aml_strerror(aml_errno)); +} + -- GitLab From 29ec05bdf121c69bf3d04efec69e95e6d29c6788 Mon Sep 17 00:00:00 2001 From: Nicolas Denoyelle Date: Mon, 25 Mar 2019 15:55:35 -0500 Subject: [PATCH 2/2] change error codes switch to array --- include/aml/utils/error.h | 2 -- src/aml.c | 2 ++ src/utils/Makefile.am | 2 +- src/utils/error.c | 27 ++++++++++++--------------- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/include/aml/utils/error.h b/include/aml/utils/error.h index 15cce4e..df1d390 100644 --- a/include/aml/utils/error.h +++ b/include/aml/utils/error.h @@ -1,8 +1,6 @@ #ifndef AML_ERROR_H #define AML_ERROR_H -#include - /** * Variable set by aml function calls. aml_errno should be checked after an aml function call * returning an error prior to any other aml call that may overwrite it. diff --git a/src/aml.c b/src/aml.c index 00ff7fb..caae568 100644 --- a/src/aml.c +++ b/src/aml.c @@ -17,6 +17,8 @@ const int aml_version_minor = AML_VERSION_MINOR; const int aml_version_patch = AML_VERSION_PATCH; const char* aml_version_string = AML_VERSION_STRING; +int aml_errno; + int aml_init(int *argc, char **argv[]) { return 0; diff --git a/src/utils/Makefile.am b/src/utils/Makefile.am index f71d1f7..e27a9f6 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -8,6 +8,6 @@ libamlutils_la_SOURCES=\ bitmap.c \ error.c -libamlutils_la_CFLAGS=$(AM_CFLAGS) -Wno-overflow +libamlutils_la_CFLAGS=$(AM_CFLAGS) libamlutils_la_LDFLAGS= diff --git a/src/utils/error.c b/src/utils/error.c index 46fa9e1..0202482 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -1,24 +1,21 @@ +#include #include "aml/utils/error.h" +static const char* aml_error_strings[-AML_ERROR_MAX] = { + [AML_SUCCESS] = "aml success! If this is unexpected, check that this is called right after aml function returning an error.", + [-AML_FAILURE] = "aml function call failed (generic error).", + [-AML_AREA_EINVAL] = "aml_area function called with invalid argument(s).", + [-AML_AREA_ENOTSUP] = "aml_area function is not implemented.", + [-AML_AREA_ENOMEM] = "Not enough memory to fulfill aml_area function call.", + [-AML_AREA_EDOM] = "An argument is out possible bounds for this function call.", +}; + const char* aml_strerror(const int errno) { - switch(errno){ - case AML_SUCCESS: - return "aml success! If this is unexpected, check that this is called right after aml function returning an error."; - case AML_FAILURE: - return "aml function call failed (generic error)."; - case AML_AREA_EINVAL: - return "aml_area function called with invalid argument(s)."; - case AML_AREA_ENOTSUP: - return "aml_area function is not implemented."; - case AML_AREA_ENOMEM: - return "Not enough memory to fulfill aml_area function call."; - case AML_AREA_EDOM: - return "An argument is out possible bounds for this function call."; - default: + if( errno < AML_ERROR_MAX || errno > 0 || aml_error_strings[errno] == NULL ) return "Invalid aml error code."; - } + return aml_error_strings[errno]; } void -- GitLab