New error handling support

Russ Allbery rra at stanford.edu
Tue Aug 29 07:01:04 UTC 2000


Taking advantage of snprintf, I've just enhanced the capabilities of the
standard error handling functions (warn, syswarn, die, and sysdie).  They
now use vsnprintf to determine the total length of the message when
formatted with vsnprintf and pass that to any error handlers that are
registered.

In case you haven't looked at the error handling mechanisms in CURRENT,
the way it works is that a program can register handlers for warn and die,
and those handlers get the format and va_list and can do whatever they
want with it.  The default handler prints to stderr, but by registering
new handlers all of INN can use the same functions to report errors and
still do different things with them depending on what program is running.

Up until now, though, there hasn't been any good way to syslog a message
as one of the handlers; the problem is that there's no syslog function
that takes a va_list, which means that a handler would have to vsprintf
into a buffer and then log that, and there's no good way of knowing what
size the buffer has to be.  vsnprintf gives us a good way, since called
with NULL, 0 as its first two arguments, it just returns the amount of
space the formatted string would require.  I've now added two more
handlers that malloc an appropriately-sized buffer to syslog the message
(at either LOG_WARNING or LOG_ERR).

What this will eventually mean is that library code that needs to report
errors can just call warn or die, and if the running program wants
something other than an error message to stderr to happen, it can register
handlers that do anything appropriate.

For an example of how this works, see the current inndstart source.  It
calls:

    /* Set up the error handlers.  Always print to stderr, and for warnings
       also syslog with a priority of LOG_WARNING.  For fatal errors, also
       syslog with a priority of LOG_ERR. */
    warn_set_handlers(2, error_log_stderr, error_log_syslog_warning);
    die_set_handlers(2, error_log_stderr, error_log_syslog_err);
    error_program_name = "inndstart";

right at the beginning of the program and then just calls warn/syswarn for
warnings and die/sysdie for fatal errors.  The handlers take care of
logging both to syslog and to stderr, so that we'll get fewer complaints
of INN not starting for no apparent reason and having to tell people to
check syslog.

The new error handling mechanisms should be stable now, so anyone hacking
parts of INN should feel free to start using them.

-- 
Russ Allbery (rra at stanford.edu)             <http://www.eyrie.org/~eagle/>



More information about the inn-workers mailing list