CLog << "text" ???

Started by
2 comments, last by rick_appleton 20 years, 3 months ago
I''m writing a logging routine, and I would love to just be able to do something like this:

CLog myLogger;
myLogger << "out put this text to logfile" << endl;
 
I''ve searched all over the net, but can''t find anything on doing this. All the tutorials I''ve found are about doing

cout << myLogger
 
but that is of no use to me. Can this be done or should I forget about overloading the << ?
Advertisement
class CLog{	public:		CLog& operator<<(const std::string& string)		{			std::cout << "hmmn, what should I do with this string of length " << string.length() << " that I've got?\n";			return *this;		}};  


Enigma

EDIT: change to same name as your class

[edited by - Enigma on January 25, 2004 11:58:22 AM]
Sure, you can do that. Just define a suitable operator<<.

CLog& operator<<(CLog &log, const char *str)    { log.Log(str); return log; }  


Something like that. Note that it is not a member function
and it needs to call a member function of the CLog class
to do the actual logging.

Repeat for other data types (or go for a template if you wish).

[Edit]: Added a return.


Kami no Itte ga ore ni zettai naru!

[edited by - tangentz on January 25, 2004 12:20:09 PM]
神はサイコロを振らない!
Yup, that works.

Thanks.

I was doing something else wrong, so this wouldn''t compile, but it works now.

This topic is closed to new replies.

Advertisement