Surprisingly I would recommend to leave the debugging function as it is, unless you are willing to change the entire interface to the C++ way of IO, aka streams. If you are going to use printf
and printf
syntax then let it as it is. Otherwise modernize it all the way.
For instance let's stay your implementation is somewhere along the lines:
void dbg(char* txt, ...)
{
va_list args;
va_start(args, txt);
vprintf(txt, args);
va_end(arts);
}
Yes, there are options to get rid of the variadic, but there is 0 gain in doing so if you are going to keep the printf
family syntax:
template <class... Args>
auto dbg(const char* fmt, const Args&... args)
{
printf(fmt, args...);
}
Then you realize that char*
is more C
than C++
and you change to this:
template <class... Args>
auto dbg(const std::string& fmt, const Args&... args)
{
printf(fmt.c_str(), args...);
}
Then you realize that printf
is more C
than C++
and the option now is to get rid of printf
and to scrap this altogether.
This question How to make a variadic macro for std::cout? shows you a way you could do that if you are still set on a function:
template<typename ...Args>
void log(Args && ...args)
{
(std::cout << ... << args);
}
Another option is to make something like this:
log << "this is the " << i << " log";
But it's not trivial to make it add a newline at the end.
In the end, the best solution I think is to use a logging library.