Managing error strings in C

Chip Salzenberg chip at tct.uucp
Sat Jan 12 05:09:12 AEST 1991


According to dave at cs.arizona.edu (Dave P. Schaumann):
>In some .h file, I have an enum type:
>typedef enum { NO_MEM, FOO_BARRED, BAR_FOOED, CODE_SPAMMED } error_t ;

We use a similar approach.

>Then, I can just say something like 'error( NO_MEM, <helpful strings> )'
>and the routine error will have a switch on every name in 'error_t'.
>
>Additionally, for a large program, it may be helpful to catagorize the
>errors.

Our errors are classified using an ErrClass structure, which contains
(among other things) the name of the error class and the address of a
function that, given the error code, returns a descriptive string.
The error() function has this prototype:

    typedef long ErrCode;
    void error(long action, const ErrClass *class,
               ErrCode code, const char *fmt, ...);

The remainder of the parameters are used a la printf() to generate
the error message.

The action is a bit map of things to do, such as writing the message
to a log (typically a circular file), writing it to stderr, and/or
exiting.

Applications can have application-specific error handlers that use and
modify the action value.

This error handling support has been used in two large applications;
it seems to work quite well.
-- 
Chip Salzenberg at Teltronics/TCT     <chip at tct.uucp>, <uunet!pdn!tct!chip>
       "If Usenet exists, then what is its mailing address?"  -- me
             "c/o The Daily Planet, Metropolis."  -- Jeff Daiell



More information about the Comp.lang.c mailing list