many years ago I wrote a custom print function declared like this:
void my_printf(char *format_string, ... )
{
// too complex to list here
}
and I could call it like this:
my_printf("Number of apples = %d\n",apple);
This function has always worked perfectly. I now wish to make a wrapper function that takes an extra integer at the start like this:
void my_printf_extra(int extra,char *format_string, ... )
that could be called like this:
my_printf_extra(debug_level,"Number of apples = %d\n",apple);
and I would like the wrapper function to call the original in a way something like this:
void my_printf_extra(int extra,char *format_string, ... )
{
if (extra == some_test)
{
my_printf(** not quite sure what goes here **);
}
}
My guess was this:
void my_printf_extra(int extra,char *format_string, ... )
{
va_list vptr;
if (extra == some_test)
{
va_start(vptr,format_string);
my_printf(format_string,vptr);
va_end(vptr);
}
}
but it's not working. In my final output I see
Number of apples = -46467968
or some such garbage number (when the true value was 1). I see similar garbage when calling my_printf_extra with a string. I suspect that my processing of va_list is wrong, but I can not fathom how exactly.
EDIT: my_printf() is super flexible and can print to all sorts of different places. Sometimes it just appends the text to a rich-edit-control-window. It all depends on assorted flags as well as the contents of what's being printed.
va_listto another function does not fill in the arguments as if they had been passed. A reasonable solution here might be to make the existingmy_printfa simpler routine that initializes ava_listand passes it tomy_printf_core, which does the rest of the work. Then yourmy_printf_extracould callmy_printf_coreinstead ofmy_printf. (my_printf_corecould be equivalent to thevfprintfmentioned in the marked original, so essentially the same solution unless you need customizations inmy_printf_core.)vfprintfand 2) it works. So there isn't a real solution in the general case, but withprintffamily functions you can work it out usingvfprintf