Hello,
gcc 4.4.1 C99
I have written a function that I will use for displaying messages to the user. This works ok, and I will need it to be scalable as I will have more error codes to add later.
However, the inputs will be the priority, error_code, and a short description of the error. I have included the function, and the line number.
However, I want to wrap the function call into a macro, but not sure how to go about doing it?
Can anyone point me in the write directions?
Many thanks for any suggestions,
#include <stdio.h>
#include <stdarg.h>
typedef enum
{
ST_BIND_ERR = 1,
ST_SOCK_ERR,
ST_CONNECT_ERR,
ST_ACCEPT_ERR
}error_codes;
typedef enum
{
ST_CRITICAL = 1,
ST_WARNING,
ST_DEBUG,
ST_INFO
}priority;
#define REPORT(prio, err, msg) /* Defining macro here */
void report_msg(int prio, int err, const char *fmt, ...);
int main(void)
{
printf("=== Starting program ===\n");
report_msg(ST_WARNING, ST_CONNECT_ERR, "Error trying to make connection : FUNCTION [ %s ] : LINE [ %d ]",
__func__, __LINE__);
return 0;
}
void report_msg(int prio, int err, const char *fmt, ...)
{
va_list ap;
char priority_msg[512] = {0};
char error_code[256] = {0};
char format[256] = {0};
char output_msg[256] = {0};
switch(prio)
{
case ST_CRITICAL:
sprintf(priority_msg, "[ ST_CRITICAL ]");
break;
case ST_WARNING:
sprintf(priority_msg, "[ ST_WARNING ]");
break;
case ST_DEBUG:
sprintf(priority_msg, "[ ST_DEBUG ]");
break;
case ST_INFO:
sprintf(priority_msg, "[ ST_INFO ]");
break;
default:
sprintf(priority_msg, "[ UNKNOWN_PRIO ]");
break;
}
switch(err)
{
case ST_BIND_ERR:
sprintf(error_code, "[ ST_BIND_ERR ]");
break;
case ST_SOCK_ERR:
sprintf(error_code, "[ ST_SOCK_ERR ]");
break;
case ST_CONNECT_ERR:
sprintf(error_code, "[ ST_CONNECT_ERR ]");
break;
case ST_ACCEPT_ERR:
sprintf(error_code, "[ ST_ACCEPT_ERR ]");
break;
default:
sprintf(error_code, "[ UNKNOWN_ERR ]");
break;
}
va_start(ap, fmt);
vsprintf(format, fmt, ap);
va_end(ap);
sprintf(output_msg,"%s %s %s", priority_msg, error_code, format);
fprintf(stderr, output_msg);
}