Commit 509cbef2 authored by Swann Perarnau's avatar Swann Perarnau
Browse files

Merge branch 'aml-error' into 'master'

[feature] add error codes and functions

See merge request !39
parents 34934bdb c4093b26
Pipeline #6758 passed with stage
in 18 minutes and 48 seconds
......@@ -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
#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
......@@ -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;
......
......@@ -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=
......
#include <stdio.h>
#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));
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment