Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!pdn!tscs!tct!chip From: chip@tct.uucp (Chip Salzenberg) Newsgroups: comp.lang.c Subject: Re: Managing error strings in C Message-ID: <278DFF48.DDE@tct.uucp> Date: 11 Jan 91 18:09:12 GMT References: <1991Jan10.122227@lotus.lotus.com> <636@caslon.cs.arizona.edu> Organization: Teltronics/TCT, Sarasota, FL Lines: 37 According to dave@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, )' >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 , "If Usenet exists, then what is its mailing address?" -- me "c/o The Daily Planet, Metropolis." -- Jeff Daiell