Let's say I have a log function
So I can do something like
void my_log(char * msg, unsigned int arg)
snprintf(buf, 500, msg, arg);
And I'll get the output "Value = 3." But what if I want to log a floating point value? When I try
int val = 3;
my_log("Value = %d.\n", (unsigned int)val)
I get -0.000000 output. At the very least, I would have expected to get 46.0000.
float val = 46.666666667f;
my_log("Value = %f.\n", (unsigned int)val)
So how would I have a wrapper logging function that would let me pass both floats and non-floats to it? One thought is to build the string beforehand, but my logger also has log levels and will avoid string construction if the logger isn't at the correct level since building a string can be expensive, especially if it's not even going to end up getting logged.
Of course, another thought is to have a different prototype, but this example is simplified and my logger takes up to 10 args to be formatted into the string. So that would be a lot of combinations of prototypes, not to mention all the basically duplicated code it would create.
Anyone else run into this before? What did you do?