I have the following log function
void log_error(char * file_name, int line_num, int err_code) {
printf("%s:%d:%s\n", file_name, line_num, get_err_str(err_code));
}
I want to change this to have the option to pass a custom format string and args for additional information something like
void log_error(char * file_name, int line_num, int err_code, ...) {
va_list ptr;
char * fmt;
printf("%s:%d:%s", file_name, line_num, get_err_str(err_code));
va_start(ptr, err_code);
fmt = va_arg(ptr, char *);
vprintf(fmt, ptr);
printf("\n");
}
but I don't want to enforce passing this extra info, so I might not get fmt
and the required extra arguments which might cause the code to fail, is there a way to check if there are variadic args before trying to access them? Or the only option for my use case is defining 2 different function, like so
void log_error(char * file_name, int line_num, int err_code) {
printf("%s:%d:%s\n", file_name, line_num, get_err_str(err_code));
}
void log_error(char * file_name, int line_num, int err_code, const char * fmt, ...) {
va_list ptr;
printf("%s:%d:%s", file_name, line_num, get_err_str(err_code));
va_start(ptr, err_code);
vprintf(fmt, ptr);
printf("\n");
va_end(ptr);
}