ostream and Logging, operator

Started by
1 comment, last by kulik 18 years, 10 months ago
Hi, I made a simple logging subsystem called LogManager, which can redirect all logged messages onto other logs or listeners, etc. Now I want to overload operator << so I can then write:

LogManager << "Something bad happened, error number: " << 1;
I tried it in numberous ways but still can't get result. This is my current code: Don't care about singleton and logMessage, these just works.

class LogManager : public Singleton<LogManager>
{
public:
	inline friend void operator <<
	( ostream& o, ostream& o2 )
	{
		stringstream s;
		s << o2;
		LogManager::getSingleton().logMessage(s.str());
	}
};
But when I include LogManager << "Hello"; anywhere, GCC says "error: expected primary-expression before '<<' token". What am I doing bad, I have zero experience with << operators, but why the hell do I need two paramaters? I guess first one is for predessor like "first << LogManager << second" but I am not sure.
Advertisement
Okay the way the IOstreams do it has little to do with operator overloadding itself, the trick is called cascading functions, you could do it either by:

struct LogManager {  // ....  // doesn't have to be a friend functions.  template < typename Tp >  friend LogManager& operator<<(LogManager& l, const Tp& t) {       //....      return l;  }};


or:

struct LogManager {  // ....  template < typename Tp >  LogManager& operator<<(const Tp& t) {      //....      return *this;  }};


cascading functions can be done for normal (member) functions aswell.
Man I owe you a beer or two :D

You were right, thank you

This topic is closed to new replies.

Advertisement