4 error.c - error reporting code for Imager
9 int new_fatal; // non-zero if errors are fatal
10 int old_fatal = i_set_failure_fatal(new_fatal);
11 i_set_argv0("name of your program");
12 extern void error_cb(char const *);
14 old_ecb = i_set_error_cb(error_cb);
16 extern void failed_cb(char **errors);
17 old_fcb = i_set_failed_cb(failed_cb);
18 if (!i_something(...)) {
19 char **errors = i_errors();
23 undef_int i_something(...) {
25 if (!some_lower_func(...)) {
26 return i_failed("could not something");
30 undef_int some_lower_func(...) {
31 if (somethingelse_failed()) {
32 i_push_error("could not somethingelse");
40 This module provides the C level error handling functionality for
43 A few functions return or pass in an i_errmsg *, this is list of error
44 structures, terminated by an entry with a NULL msg value, each of
45 which contains a msg and an error code. Even though these aren't
46 passed as i_errmsg const * pointers, don't modify the strings
49 The interface as currently defined isn't thread safe, unfortunately.
51 This code uses Imager's mymalloc() for memory allocation, so out of
52 memory errors are I<always> fatal.
56 These functions form the interface that a user of Imager sees (from
57 C). The Perl level won't use all of this.
69 static i_error_cb error_cb;
70 static i_failed_cb failed_cb;
71 static int failures_fatal;
74 =item i_set_argv0(char const *program)
76 Sets the name of the program to be displayed in fatal error messages.
78 The simplest way to use this is just:
82 when your program starts.
84 void i_set_argv0(char const *name) {
88 /* if the user has an existing string of MAXINT length then
89 the system is broken anyway */
90 dupl = mymalloc(strlen(name)+1); /* check 17jul05 tonyc */
98 =item i_set_failure_fatal(int failure_fatal)
100 If failure_fatal is non-zero then any future failures will result in
101 Imager exiting your program with a message describing the failure.
103 Returns the previous setting.
107 int i_set_failures_fatal(int fatal) {
108 int old = failures_fatal;
109 failures_fatal = fatal;
115 =item i_set_error_cb(i_error_cb)
117 Sets a callback function that is called each time an error is pushed
118 onto the error stack.
120 Returns the previous callback.
122 i_set_failed_cb() is probably more useful.
126 i_error_cb i_set_error_cb(i_error_cb cb) {
127 i_error_cb old = error_cb;
134 =item i_set_failed_cb(i_failed_cb cb)
136 Sets a callback function that is called each time an Imager function
139 Returns the previous callback.
143 i_failed_cb i_set_failed_cb(i_failed_cb cb) {
144 i_failed_cb old = failed_cb;
155 Returns a pointer to the first element of an array of error messages,
156 terminated by a NULL pointer. The highest level message is first.
160 i_errmsg *im_errors(im_context_t ctx) {
161 return ctx->error_stack + ctx->error_sp;
164 i_errmsg *i_errors(void) {
165 return im_errors(im_get_context());
171 =head1 INTERNAL FUNCTIONS
173 These functions are called by Imager to report errors through the
176 It may be desirable to have functions to mark the stack and reset to
181 =item i_clear_error()
182 =synopsis i_clear_error();
183 =category Error handling
185 Clears the error stack.
187 Called by any Imager function before doing any other processing.
193 im_clear_error(im_context_t ctx) {
194 #ifdef IMAGER_DEBUG_MALLOC
197 for (i = 0; i < IM_ERROR_COUNT; ++i) {
198 if (ctx->error_space[i]) {
199 myfree(ctx->error_stack[i].msg);
200 ctx->error_stack[i].msg = NULL;
201 ctx->error_space[i] = 0;
205 ctx->error_sp = IM_ERROR_COUNT-1;
209 i_clear_error(void) {
210 im_clear_error(im_get_context());
214 =item i_push_error(int code, char const *msg)
215 =synopsis i_push_error(0, "Yep, it's broken");
216 =synopsis i_push_error(errno, "Error writing");
217 =category Error handling
219 Called by an Imager function to push an error message onto the stack.
221 No message is pushed if the stack is full (since this means someone
222 forgot to call i_clear_error(), or that a function that doesn't do
223 error handling is calling function that does.).
228 im_push_error(im_context_t ctx, int code, char const *msg) {
229 size_t size = strlen(msg)+1;
231 if (ctx->error_sp <= 0)
232 /* bad, bad programmer */
236 if (ctx->error_alloc[ctx->error_sp] < size) {
237 if (ctx->error_stack[ctx->error_sp].msg)
238 myfree(ctx->error_stack[ctx->error_sp].msg);
239 /* memory allocated on the following line is only ever released when
240 we need a bigger string */
241 /* size is size (len+1) of an existing string, overflow would mean
242 the system is broken anyway */
243 ctx->error_stack[ctx->error_sp].msg = mymalloc(size); /* checked 17jul05 tonyc */
244 ctx->error_alloc[ctx->error_sp] = size;
246 strcpy(ctx->error_stack[ctx->error_sp].msg, msg);
247 ctx->error_stack[ctx->error_sp].code = code;
251 i_push_error(int code, char const *msg) {
252 im_push_error(im_get_context(), code, msg);
256 =item i_push_errorvf(int C<code>, char const *C<fmt>, va_list C<ap>)
258 =category Error handling
260 Intended for use by higher level functions, takes a varargs pointer
261 and a format to produce the finally pushed error message.
263 Does not support perl specific format codes.
268 im_push_errorvf(im_context_t ctx, int code, char const *fmt, va_list ap) {
270 #if defined(IMAGER_VSNPRINTF)
271 vsnprintf(buf, sizeof(buf), fmt, ap);
272 #elif defined(_MSC_VER)
273 _vsnprintf(buf, sizeof(buf), fmt, ap);
275 /* is there a way to detect vsnprintf()?
276 for this and other functions we need some mechanism to handle
277 detection (like perl's Configure, or autoconf)
279 vsprintf(buf, fmt, ap);
281 im_push_error(ctx, code, buf);
285 i_push_errorvf(int code, char const *fmt, va_list ap) {
286 im_push_errorvf(im_get_context(), code, fmt, ap);
290 =item i_push_errorf(int code, char const *fmt, ...)
291 =synopsis i_push_errorf(errno, "Cannot open file %s: %d", filename, errno);
292 =category Error handling
294 A version of i_push_error() that does printf() like formatting.
296 Does not support perl specific format codes.
301 i_push_errorf(int code, char const *fmt, ...) {
304 i_push_errorvf(code, fmt, ap);
309 im_push_errorf(im_context_t ctx, int code, char const *fmt, ...) {
312 im_push_errorvf(ctx, code, fmt, ap);
316 #ifdef IMAGER_I_FAILED
317 #error "This isn't used and is untested"
320 =item i_failed(char const *msg)
322 Called by Imager code to indicate that a top-level has failed.
324 msg can be NULL, in which case no error is pushed.
326 Calls the current failed callback, if any.
328 Aborts the program with an error, if failures have been set to be fatal.
330 Returns zero if it does not abort.
334 int i_failed(int code, char const *msg) {
336 i_push_error(code, msg);
338 failed_cb(error_stack + error_sp);
339 if (failures_fatal) {
341 size_t total; /* total length of error messages */
342 char *full; /* full message for logging */
344 fprintf(stderr, "%s: ", argv0);
345 fputs("error:\n", stderr);
347 while (error_stack[sp].msg) {
348 fprintf(stderr, " %s\n", error_stack[sp].msg);
351 /* we want to log the error too, build an error message to hand to
353 total = 1; /* remember the NUL */
354 for (sp = error_sp; error_stack[sp].msg; ++sp) {
355 size_t new_total += strlen(error_stack[sp].msg) + 2;
356 if (new_total < total) {
357 /* overflow, somehow */
361 full = mymalloc(total);
363 /* just quit, at least it's on stderr */
367 for (sp = error_sp; error_stack[sp].msg; ++sp) {
368 strcat(full, error_stack[sp].msg);
371 /* lose the extra ": " */
372 full[strlen(full)-2] = '\0';
373 i_fatal(EXIT_FAILURE, "%s", full);
382 =item im_assert_fail(file, line, message)
384 Called when an im_assert() assertion fails.
390 im_assert_fail(char const *file, int line, char const *message) {
391 fprintf(stderr, "Assertion failed line %d file %s: %s\n",
392 line, file, message);
401 This interface isn't thread safe.
405 Tony Cook <tony@develop-help.com>
407 Stack concept by Arnar Mar Hrafnkelsson <addi@umich.edu>