vf***@talktalk.net wrote:
If you want an automatic endl at the end of the calling full
expression, you need to make mycout() return a temporary
object that implements operator<< and does stuff in its destructor.
How do you implement that operator ?
Care to expand ?
Something like this:
struct LogHelper
{
LogHelper(std::ostream &os_): os(os_), active(true)
{
std::time_t t = std::time(NULL);
char buf[100];
std::strftime(buf, sizeof buf, "%d-%b-%Y %H:%M:%S",
std::localtime(&t));
os << "[" << buf << "] ";
}
LogHelper(LogHelper &h): os(h.os), active(true)
{
h.active = false;
}
template<typename T>
std::ostream &operator<<(T const &t)
{
return os << t;
}
operator std::ostream &() { return os; }
~LogHelper()
{
if (active)
os << std::endl;
}
private:
std::ostream &os;
bool active;
};