I created my own std::cout
-like object that writes both to std::cout
and to a log file.
I'm currently defining it like this in a header file, but I'm getting unused variable warnings.
Header file <MyLib/Log.h>
static LOut { };
static LOut lo;
template<typename T> inline LOut& operator<<(LOut& mLOut, const T& mValue)
{
std::string str{toStr(mValue)};
std::cout << str;
getLogStream() << str;
return mLOut;
}
Usage:
#include <MyLib/Log.h>
...
lo << "hello!" << std::endl;
Should lo
be static
? Should lo
be extern
?
Kudos for explaining the correct way of declaring a cout
-like object and showing how the main standard library implementations do it.
Edit: by cout
-like object, I mean a global variable that is always available after including the corresponding header.