From 01c8a8d3c1f621c56a704308037ee1b013e2eb7e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 31 May 2014 06:19:28 +0200 Subject: add libgamma_perror MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libgamma-error.c.gpp | 45 +++++++++++++++++++++++++++++++++++++++------ src/libgamma-error.h | 22 +++++++++++++++++++--- 2 files changed, 58 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/libgamma-error.c.gpp b/src/libgamma-error.c.gpp index 93f8542..916e387 100644 --- a/src/libgamma-error.c.gpp +++ b/src/libgamma-error.c.gpp @@ -22,6 +22,14 @@ #include #include #include +#include +#include + + + +£>set -u +£>cd info +£>export PATH=".:${PATH}" @@ -38,13 +46,38 @@ const char* libgamma_group_name = NULL; -£>set -u -£>cd info -£>export PATH=".:${PATH}" +/** + * Prints an error to stderr in a `perror` fashion, + * however this function will not translate the `libgamma` + * errors into human-readable strings, it will simply + * print the name of the error. If the value `error_code` + * is the value of `LIBGAMMA_ERRNO_SET`, `perror` will be + * used to print the current error stored in `errno`. + * If `value` is non-negative (a `errno` value`), that + * value will be stored in `errno` and `perror` will be + * used to print it. + * + * @param name The text to add at the beginning. + * @param value The error code, may be an `errno` value. + */ +void libgamma_perror(const char* name, int error_code) +{ + if (error_code >= 0) + { + errno = error_code; + perror(name); + } + else if (error_code == LIBGAMMA_ERRNO_SET) + perror(name); + else if (error_code < LIBGAMMA_ERROR_MIN) + fprintf(stderr, "%s: (?)\n", name); + else + fprintf(stderr, "%s: %s\n", name, libgamma_name_of_error(error_code)); +} /** - * Returns the name of the definition associated with a libgamma error code. + * Returns the name of the definition associated with a `libgamma` error code. * * @param value The error code. * @return The name of the definition associated with the error code, @@ -68,11 +101,11 @@ const char* libgamma_name_of_error(int value) /** - * Return the value of a libgamma error definition refered to by name. + * Return the value of a `libgamma` error definition refered to by name. * * @param name The name of the definition associated with the error code. * @return The error code, zero if the name does is `NULL` - * or does not refer to an libgamma error. + * or does not refer to a `libgamma` error. */ int libgamma_value_of_error(const char* name) { diff --git a/src/libgamma-error.h b/src/libgamma-error.h index fac5276..44ecec4 100644 --- a/src/libgamma-error.h +++ b/src/libgamma-error.h @@ -289,7 +289,23 @@ extern const char* libgamma_group_name; /** - * Returns the name of the definition associated with a libgamma error code. + * Prints an error to stderr in a `perror` fashion, + * however this function will not translate the `libgamma` + * errors into human-readable strings, it will simply + * print the name of the error. If the value `error_code` + * is the value of `LIBGAMMA_ERRNO_SET`, `perror` will be + * used to print the current error stored in `errno`. + * If `value` is non-negative (a `errno` value`), that + * value will be stored in `errno` and `perror` will be + * used to print it. + * + * @param name The text to add at the beginning. + * @param value The error code, may be an `errno` value. + */ +void libgamma_perror(const char* name, int error_code); + +/** + * Returns the name of the definition associated with a `libgamma` error code. * * @param value The error code. * @return The name of the definition associated with the error code, @@ -299,11 +315,11 @@ extern const char* libgamma_group_name; const char* libgamma_name_of_error(int value) __attribute__((const)); /** - * Return the value of a libgamma error definition refered to by name. + * Return the value of a `libgamma` error definition refered to by name. * * @param name The name of the definition associated with the error code. * @return The error code, zero if the name does is `NULL` - * or does not refer to an libgamma error. + * or does not refer to a `libgamma` error. */ int libgamma_value_of_error(const char* name) __attribute__((const)); -- cgit v1.2.3-70-g09d2