469,935 Members | 1,983 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

constructing own error functions vs something like fprinf()

Why would someone go through the trouble of constructing an error
handling function using variable-length argument lists? Why not just
use something like printf()?

Nov 15 '05 #1
5 1592
grocery_stocker wrote:
Why would someone go through the trouble of constructing an error
handling function using variable-length argument lists? Why not just
use something like printf()?


What if you want the message to go to something other
than an output stream -- a dialog box, perhaps?

What if you want to add "decorations" to the message
as provided -- line number, call stack, or the like?

What if you want to assign each error a "severity" and
route the message (or take other action) based on that
severity?

What if you want to do *any* kind of extra processing
when an error occurs, and would prefer to write the code
in just one place rather than in all two hundred spots where
an error might occur?

... and the beat goes on.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #2
On Sat, 25 Jun 2005 12:35:15 -0700, grocery_stocker wrote:
Why would someone go through the trouble of constructing an error
handling function using variable-length argument lists? Why not just
use something like printf()?


printf() isn't an error handling function, it just writes data to standard
output. This may not be appropriate for an error handling function, or the
error handling function may do other things too. Iven if you wanted only
an error reporting function that logs data to a stream, you might want
output formatted in a standardised way (e.g. with a timestamp in each
entry), you might want to control what is output (e.g. some sort of
logging level), and so on.

Also an error handling function can use printf(), or more specifically
vprintf() and similar functions. So it doesn't have to reimplement what
printf() does.

Lawrence
Nov 15 '05 #3
In article <11**********************@g43g2000cwa.googlegroups .com>,
grocery_stocker <cd*****@gmail.com> wrote:
Why would someone go through the trouble of constructing an error
handling function using variable-length argument lists? Why not just
use something like printf()?


Perhaps it's not as critical to others, but I dislike using varags
functions for error reporting. It's too easy to supply things that
don't match the format, or to dereference NULL or freed pointers. It's
difficult to create testing scenarios for error conditions, so you end
up with a situation where there is lots of error reporting, but none of
it is tested. When an error actually occurs, you run a great risk of
making the problem worse by using an untested, fancy interface that
encourages printing out corrupted data.

My own philosophy is to log contextual information well before the
error occurs (on a regular basis, so it is well tested; and before the
bulk of processing so it has not yet been corrupted), then at
the point of discovering the error just print out a constant string
along the lines of "there was a problem with the current blah's foobar
pointer."
--
7842++
Nov 15 '05 #4
In article <tJYve.784$HV1.685@fed1read07>,
Anonymous 7843 <an******@example.com> wrote:
My own philosophy is to log contextual information well before the
error occurs (on a regular basis, so it is well tested; and before the
bulk of processing so it has not yet been corrupted), then at
the point of discovering the error just print out a constant string
along the lines of "there was a problem with the current blah's foobar
pointer."


This only applies to a very limited kind of error handling. How would
you use it to report a syntax error in user input for example?

-- Richard
Nov 15 '05 #5
In article <d9**********@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <ri*****@cogsci.ed.ac.uk> wrote:

In article <tJYve.784$HV1.685@fed1read07>,
Anonymous 7843 <an******@example.com> wrote:
My own philosophy is to log contextual information well before the
error occurs (on a regular basis, so it is well tested; and before the
bulk of processing so it has not yet been corrupted), then at
the point of discovering the error just print out a constant string
along the lines of "there was a problem with the current blah's foobar
pointer."


This only applies to a very limited kind of error handling. How would
you use it to report a syntax error in user input for example?


User-input errors are easy to reproduce and test, so I would have no
quarrel with a sophisticated varargs thingy to print out a detailed
description of a syntax error.

Whether the remainder is "very limited" depends on the nature and
requirements of what you're doing. At the very least, I would
encourage people who choose to use a varargs error reporting thingy to
either use printf or fprintf directly, or to use their favorite
compiler's printf-like attribute (if available) on their custom error
reporting function so that lame argument mismatches don't crash your
program before you can even print the message.

For a more rigorous environment, combine that with some defensive
coding like implementation-specific checks on pointer locations, fflush
log files char by char (or setbuf), stop processing the va_arg list on
the first NULL or bad pointer, don't include any pointed-to data in
output unless all pointers pass the sanity checks, print and fflush the
constant part of the error message before processing dodgy arguments,
check pointers to strings (including the format string) to see if they
have non-printable chars or are more than 80 characters, and log the
date and time of the message, maybe even __FILE__ and __LINE__. Check
for low addresses on things passed in by value: the int you get might
be coming from something like (NULL)->struct.value. Check aligment
while you're at it. In environments which exceed the scope of ANSI C,
also consider printing out things like the thread number. Finally,
*test* the error reporting function by giving it deliberately garbaged
data of all kinds and make sure it doesn't choke.

Sure, this is overkill for a lot of programs, but if this makes the
difference between a truncated/garbaged log file and something that
helps you diagnose and fix a problem on a server in Kreplakistan at
3:00AM on Saturday night, you won't regret it.
--
7842++
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Benjamin Scott | last post: by
1 post views Thread by cainlevy | last post: by
7 posts views Thread by Hyoung Lee | last post: by
3 posts views Thread by Alberto Giménez | last post: by
1 post views Thread by nabil | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.