I'm writing a simple logger that is basically just a wrapper around the std c libs 'printf' function, that appends some additioinal information on to the string argument.
This all works fine when i call DEBUG_LOG("something %d", 3)
the problem is that i also want to be able to call it with just a string, like:
DEBUG_LOG("something")
I'm not sure how i should go about this.
Does anybody know of some "macro magic" to make this work ?
(like, maybe overload the DEBUG_LOG macro and parse NULL for the second parameter to Log(), or something along those lines)
private:
staticvoid write(std::ostream &stream){}
template <typename T, typename... Params>
staticvoid write(std::ostream &stream, T &&x, Params &&... p){
stream << x;
write(stream, std::forward<Params>(p)...);
}
public:
template <typename... Params>
void Log(const std::string &msg, Params &&... p)
{
//Note: __FILE__ and __TIME__ will write the filename that contains Log(),
// and the time at which Log() was compiled. NOT the filename that
// contains the function that called Log(), nor the time at which Log()
// was called.
std::cout << "LOG <" << __FILE__ << ", " << __TIME__ << "> : " << msg << std::endl;
write(std::cout, std::forward<Params>(p)...);
}
void example(){
DEBUG_LOG("hello!", 14, " ", 3.141592);
}