I am just wondering what is the best way to make custom print error functions.
For example I have some #defines like this in header file:
#define SOCKET_ERR 0
#define BIND_ERR 1
#define LISTEN_ERR 2
etc
Then maybe using this like this:
if(/*something has gone wrong with socket*/)
{
print_error(SOCKET_ERR);
}
print_error(int error)
{
if(error == 0)
{
printf("Socket failure\n");
}
}
However, I don't think this perfect and want to do something much better. Maybe something a little bit more professional and maybe more scalable.
Many thanks for any advice,
You might consider using variadic functions for error reporting, they become so much more versatile.
For instance
#include <stdarg.h>
void my_error(FILE *out, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(out, fmt, ap);
va_end(ap);
}
Which could be invoked like this (note, I'm assuming a c99 compiler):
my_error(stderr,
"%s: Invalid range of %ld near line %d", __func__, range, __LINE__);
This could easily tie in with other answers suggesting that error codes could be defined in enumerated lists, with a constant array of strings to translate them. I'll leave that as an exercise for the reader. Its very easy to make the example above accept more arguments.
NB: If you use something like char buffer[LEN] to custom format the printed string, change it from void to unsigned int, have it return the number of bytes that vsnprintf() could not print, that might be useful to the caller. The above example is 'safe', wherein you don't have to worry about over flowing some stack allocated buffer with a formatted error message of undefined length. Or, leave it as void and have it print what it can (while noting it could not print everything), up to you. The draw back to this approach is not quite knowing the length of the variadic arguments once expanded. After all, you're reporting unexpected results :)
This approach lets you help yourself more by conveying meaningful and informative error messages, as well as simply logging them to any open file.
I know that this example basically describes printf() itself. I'm posting it to show how easy it is to adapt and expand.
Check out log4c for some ideas how to implement logging.
Here is some advice: Don't just print "there was an error". Give as much information as possible: Which IP address? Error-Code? What did your code try to achieve?
When you need to write an error message, ask yourself this question: What will I need to know when I see this error message? What will help me to fix the issue?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With