You can use variadic templates and recursion.
Note: Since you mention GCC 4.4, this feature is available for that compiler, but it is not enabled by default. You have to add either the
-std=c++0x
or-std=gnu++0x
option to the compiler to enable the feature.
A solution could be implemented as something along these lines:
// Does the actual logging of the formatted message
void mylog_r (int severity, boost::format &fmt) {
std::cout << "[" << severity << "] "
<< fmt
<< std::endl;
}
// Unpacks the variadic arguments one at a time recursively
template <typename T, typename... Params>
void mylog_r (int severity, boost::format &fmt, T arg, Params... parameters) {
mylog_r(severity, fmt % arg, parameters...); // recursively unpack
}
// This version of mylog() checks severity and converts fmt to boost::format
template <typename... Params>
void mylog (int severity, std::string fmt, Params... parameters) {
if (severity < LEVEL) return;
boost::format bfmt(fmt);
mylog_r(severity, bfmt, parameters...);
}