diff --git a/include/Makefile.am b/include/Makefile.am index c0e7a1ef29d56d1e3a1d5562e54f59bb2efb99e9..ab67dcfe81af1ca8f4f3fe11ab9bbae0b34895e4 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 0000000000000000000000000000000000000000..df1d390c860d5b5674b416d24d1746993e064f53 --- /dev/null +++ b/include/aml/utils/error.h @@ -0,0 +1,41 @@ +#ifndef AML_ERROR_H +#define AML_ERROR_H + +/** + * 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/aml.c b/src/aml.c index 00ff7fb2226f2f30c49663c13f532cbd454f4575..caae56863fe112584cb1de05b9691b5765f88e93 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 8f2a0ba6cdfdf4ebf6ecfb17852e1c11b221a641..e27a9f63f67727615709492940a2ba2934ffb964 100644 --- a/src/utils/Makefile.am +++ b/src/utils/Makefile.am @@ -4,8 +4,9 @@ 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_LDFLAGS= diff --git a/src/utils/error.c b/src/utils/error.c new file mode 100644 index 0000000000000000000000000000000000000000..02024821d93fa9a89ae5c05e7bb93ef53b745925 --- /dev/null +++ b/src/utils/error.c @@ -0,0 +1,26 @@ +#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) +{ + if( errno < AML_ERROR_MAX || errno > 0 || aml_error_strings[errno] == NULL ) + return "Invalid aml error code."; + return aml_error_strings[errno]; +} + +void +aml_perror(const char * msg) +{ + fprintf(stderr, "%s:%s\n", msg, aml_strerror(aml_errno)); +} +