Managing error strings in C

Sakari Jalovaara sja at sirius.hut.fi
Sun Jan 20 06:14:51 AEST 1991


> a) "Error %{1}d on line %{2}d" and b)  "Line %{2}d: error %{1}d detected"

Pretty much the tracks I was thinking along - except that the type of
the object (here "d" for "int") being printed does not really belong
in the message format string: if a user can customize his own messages
imagine what happens with

	"Line %{2}s: error %{1}f detected"
		  ^ oops       ^ oops

A program should not crash if it is given bad input and a user should
not need to know things like the types used internally in a program.
Here is how I thought an error text might be printed:

	message (HELLO_WORLD, (char *) 0);
	message (ERROR_ON_LINE,
			/* error name / type / argument */
			"line",         "d",   line_number,
			"error",        "s",   error_text,
			(char *) 0);

and the error text database would look something like

	HELLO_WORLD Hello, world!
	ERROR_ON_LINE Error {error} on line {line}

with automatically generated indexes in the beginning of the file for
faster access.

Implementing that is left as an exercise for the reader.

(Even that isn't enough if you want really fancy messages - how can
you pluralize words or determine if you need "a" or "an" articles etc.
For a multi-lingual system doing that takes separate binaries for
different human languages or writing the message system in an
interpreted language.  Yuck.)

***

The MIT Athena project has some kind of an error reporting tool that
takes specifications like

	ec ZERR_AUTHFAIL, "Server could not verify authentication"

and turns them to .c and .h files (the messages are compiled in in the
a.out.)  It is called "et" and is distributed at least as a part of
Zephyr (ftp athena-dist.mit.edu).  I haven't tried it.
									++sja



More information about the Comp.lang.c mailing list