-
report messages
Hello,
C99
gcc 4.4.3
I want to report some error messages to the user of my program. I have found that using Variadic function is the most scalable method to use, but I am not sure how to put it together, this is what I have so far.
Many thanks for any suggestions,
Code:
typedef enum
{
DEV_FAILED, /* Device failed to open */
EX_FAILED, /* Cannot open library */
HOST_INFO_ERR, /* Cannot get host information */
} report_msg;
Code:
#include <stdarg.h>
void report_error(FILE *out, const char *fmt, ...)
{
va_list ap;
va_start(ap_fmt);
vfprintf(out, fmt, ap);
va_end(ap);
}
Code:
my_error(stderr,
"%s: Invalid range of %ld near line %d", __func__, range, __LINE__);
-
I'll assume that 'my_error' was really supposed to be 'report_error'? You really only need "..." functions if your errors will have assorted numbers of arguments. Do you really need a bunch of various arguments?
Quzah.
-
Hello,
Yes, my_error was supposed to be report_error. I just changed the name to make it more readable.
However, I was thinking it was better to do it this way. As I have heard many programmers use this form of error reporting. And at the moment I am just at the beginning of writing this application. So the errors will grow.
I am just trying to get started with this, any help would be gratefull.
Many thanks,
-
So basically you're looking to reimplement fprintf?
Quzah.
-
I guess I am. However, I am not too sure. Do you have any samples,
Thanks,
-
I was hoping you'd sort of get the point there... why are you trying to reimplement something that already exists as a standard library function?
Quzah.
-
Sorry, the penny didn't drop.
I am coming from C# and just looking for some samples that use this in C.
Thanks,