Reputation: 1765
This is my current function which works but is not type safe and can get annoying sometimes.
void Debug::Log(LogLevel level, const char* format, ...)
{
va_list args;
va_start(args, format);
cout << "[" << LogLevelToString(level) << "]\t";
vprintf(format, args);
va_end(args);
cout << endl;
}
As you can see I would like for the arguments passed in to be formatted by the format specified. I know std::cout has formatting capabilities but I haven't found anything which explains how it can be implemented with the C va_list.
Basically the main points are: I want to keep the the same behavior but with a type safe more modern method, I need to std::cout so I can easily redirect output to file or where ever I need to.
Helpful points: from the format I can determine how many parameters where passed in, is there a way to loop through va_list arguments so I can pass them to cout individually?
Thanks
Upvotes: 2
Views: 1092
Reputation: 371
For easy redirection, you can certainly use vfprintf()
. For individual control/cout of each argument, I think the only way is to va_arg()
through the list using the correct type, i.e. an_int = va_arg(the_va_list, int);
, a_double = va_arg(the_va_list, double);
, and so on. I haven't tried but I think va_arg
will do the proper increment on the memory using the type
passed to it.
Upvotes: 0