I would like to do something like this:
writeLog(printf("This is the error: %s", error));
so i am looking for a function which returns a formatted string.
Given no such function exists, consider a slightly different approach: make writeLog
printf-like, i.e. take a string and a variable number of arguments. Then, have it format the message internally. This will solve the memory management issue, and won't break existing uses of writeLog
.
If you find this possible, you can use something along these lines:
void writeLog(const char* format, ...)
{
char msg[100];
va_list args;
va_start(args, format);
vsnprintf(msg, sizeof(msg), format, args); // do check return value
va_end(args);
// write msg to the log
}
There is no such function in the standard library and there will never be one in the standard library.
If you want one, you can write it yourself. Here's what you need to think about:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With