So I use such Log class:
#include <stdio.h>
#include <iostream>
class Log
{
public:
int i;
Log()
{
i = 0;
}
template <class T>
Log &operator<<(const T &v)
{
i++;
std::cout << i << ":" << v << ";" <<std::endl;
return *this;
}
Log &operator<<(std::ostream&(*f)(std::ostream&))
{
i++;
std::cout << i << ":" << *f << ";" <<std::endl;
return *this;
}
~Log()
{
std::cout << " [end of message]" << std::endl;
}
};
Which I use like:
#include <log.h>
int main()
{
Log a;
a << "here's a message" << std::endl;
a << "here's one with a number: " << 5;
std::cin.get();
}
I want my log class to get when I put “;” meaning if I have a << "here's a message" << std::endl; I want it to be capable to get that it is oune log message and a << "here's one with a number: " << 5; is another.
crently it outputs next message:
1:here's a message;
2:
;
3:here's one with a number: ;
4:5;
I want to keep its sintax (unlimited number of <<, big range of value types, no ( and ) around in api) but make it output:
1:here's a message
;
2:here's one with a number: 5;
How to do such thing?
Make
operator<<return a temporary value which will placeendlupon destruction and forwards alloperator<<calls to the main object. This way,endlis guaranteed to be called exactly once.