error.c - error reporting code for Imager
// user code: int new_fatal; // non-zero if errors are fatal int old_fatal = i_set_failure_fatal(new_fatal); i_set_argv0("name of your program"); extern void error_cb(char const *); i_error_cb old_ecb; old_ecb = i_set_error_cb(error_cb); i_failed_cb old_fcb; extern void failed_cb(char **errors); old_fcb = i_set_failed_cb(failed_cb); if (!i_something(...)) { char **errors = i_errors(); } // imager code: undef_int i_something(...) { i_clear_error(); if (!some_lower_func(...)) { return i_failed("could not something"); } return 1; } undef_int some_lower_func(...) { if (somethingelse_failed()) { i_push_error("could not somethingelse"); return 0; } return 1; }
This module provides the C level error handling functionality for Imager.
A few functions return or pass in an i_errmsg *, this is list of error structures, terminated by an entry with a NULL msg value, each of which contains a msg and an error code. Even though these aren't passed as i_errmsg const * pointers, don't modify the strings or the pointers.
The interface as currently defined isn't thread safe, unfortunately.
This code uses Imager's mymalloc() for memory allocation, so out of memory errors are always fatal.
These functions form the interface that a user of Imager sees (from C). The Perl level won't use all of this.
Returns a pointer to the first element of an array of error messages, terminated by a NULL pointer. The highest level message is first.
Also callable as i_errors().
i_errors()
These functions are called by Imager to report errors through the above interface.
It may be desirable to have functions to mark the stack and reset to the mark.
Clears the error stack.
Called by any Imager function before doing any other processing.
Also callable as i_clear_error().
i_clear_error()
Called by an Imager function to push an error message onto the stack.
No message is pushed if the stack is full (since this means someone forgot to call i_clear_error(), or that a function that doesn't do error handling is calling function that does.).
Intended for use by higher level functions, takes a varargs pointer and a format to produce the finally pushed error message.
Does not support perl specific format codes.
Also callable as i_push_errorvf(code, format, args)
i_push_errorvf(code, format, args)
A version of i_push_error() that does printf() like formatting.
A version of im_push_error() that does printf() like formatting.
Called by Imager code to indicate that a top-level has failed.
msg can be NULL, in which case no error is pushed.
Calls the current failed callback, if any.
Aborts the program with an error, if failures have been set to be fatal.
Returns zero if it does not abort.
Called when an im_assert() assertion fails.
Only available when Imager is built with assertions.
This interface isn't thread safe.
Tony Cook <tony@develop-help.com>
Stack concept by Arnar Mar Hrafnkelsson <addi@umich.edu>
To install Imager, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Imager
CPAN shell
perl -MCPAN -e shell install Imager
For more information on module installation, please visit the detailed CPAN module installation guide.