Managing error strings in C

Mark Harrison harrison at necssd.NEC.COM
Fri Jan 18 04:21:37 AEST 1991


In article <1991Jan10.122227 at lotus.lotus.com>,
blambert at lotus.lotus.com (Brian Lambert) writes:

> I was wondering if anyone out there had any clever ways of handling
> error messages in C.

> I have seen code where people define an array of
> char pointers to error messages used in the program such as:
> 
> I have used this method
> in the past, but was never very happy with it.  (It's not very elegant,
> and is difficult to maintain as one must be sure to use the proper
> number in the #define.)

On several compiler related projects I have worked on, we did it
something like this:

1.  Create a text file that incorporates an identifier, error message,
    and help text:

ERR_NOSYM "Undefined symbol: %s"
          "Undefined symbol: \fIsymbol-name\fP"
{
	You have referenced a symbol which you have not defined.
	blah blah...
}

2.  Generate a .h file that has the #defines:

	#define ERR_NOSYM 101
	etc...

3.  Generate a .c file that has the initialized strings:

	char *errmessages[] = { "Undefined symbol: %s", ... };

4.  Use a standard error function to display error messages.  Use
    varargs to grab the parms passed.:

	err(ERR_NOSYM, symname);

5.  Generate a beautifully formatted troff document that can
    be put into the user docs.  (This is the good part.)
    Documentation people will like you, you will like not
    having to proofread some obsolete error appendix, etc.

One major benefit of this is that it makes it easy to move from
(human) language to language.  Just give the text file to your
translator, and when he gives it back, generate a new version
of your product.

We used nawk to generate the .h, .c, and .doc files.

Mark.
-- 
Mark Harrison             harrison at necssd.NEC.COM
(214)518-5050             {necntc, cs.utexas.edu}!necssd!harrison
standard disclaimers apply...



More information about the Comp.lang.c mailing list